Reputation: 2763
I am using Laravel 5.2 Package dimsav laravel-translatable 6.*
This is my worked controller
$about = new About();
$about->link = $request->link;
$about->save();
$localization = $request['localization'];
//dd($localization);
// check if locales arabic and english only
$locales = array_keys($localization);
foreach ($locales as $locale) {
if (!in_array($locale, array('en', 'ar'))) {
Session::flash('message', 'Lang Error');
return Redirect::back();
}
}
foreach ($localization as $locale => $parameters) {
$about->translateOrNew($locale)->title = $parameters['title'];
$about->translateOrNew($locale)->details = $parameters['details'];
}
$about->save();
but when I try to use mass-assignment
$input = $request->all();
$localization = $input['localization'];
// check if locales arabic and english only
$locales = array_keys( $localization );
foreach ( $locales as $locale ) {
if ( ! in_array( $locale, array( 'en', 'ar' ) ) ) {
Session::flash( 'message', 'Lang Error' );
return Redirect::back();
}
}
foreach ( $localization as $locale => $parameters ) {
$input->translateOrNew( $locale )->title = $parameters['title'];
$input->translateOrNew($locale)->details = $parameters['details'];
}
dd($input);
About::create( $input );
Got an error
Call to a member function translateOrNew() on a non-object
any help please what I am doing wrong here.
Edit :
My about Model
use Dimsav\Translatable\Translatable;
use Illuminate\Database\Eloquent\Model;
class About extends Model {
use Translatable;
public $translatedAttributes = [ 'title', 'details' ];
protected $fillable = [ 'link' ];
}
my abouttranslation model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AboutTranslation extends Model
{
public $timestamps = false;
protected $fillable = ['title', 'details', 'about_id'];
}
Upvotes: 0
Views: 650
Reputation: 39
You forgot to include the following in the model class.
Use \Dimsav\Translatable\Translatable;
in your Model file, which is here: \App\Article.php
Upvotes: -1
Reputation: 1328
In your mass assignment version you are getting an array from using
$input = $request->all();
You want to use the function translateOrNew()
on that array which is not working because the array has no function. You have to call the function from somewhere else and save an array with create. I provide a simple example:
$input = $request->all();
$localization = $input['localization'];
// check if locales arabic and english only
$locales = array_keys( $localization );
foreach ( $locales as $locale ) {
if ( ! in_array( $locale, array( 'en', 'ar' ) ) ) {
Session::flash( 'message', 'Lang Error' );
return Redirect::back();
}
}
$input = array();
foreach ( $localization as $locale => $parameters ) {
$input[About::translateOrNew( $locale )->title] = $parameters['title'];
$input[About::translateOrNew($locale)->details] = $parameters['details'];
}
About::create( $input );
I have to assume many things, since I do not know how the About
model looks like. Additionally I assume that there is a static method translateOrNew()
available in this model. Please provide more information about your script, that will improve the answer.
Update:
On second thought, why not use this much simpler approach?
<?php
$input = $request->all();
$localization = $input['localization'];
// check if locales arabic and english only
$locales = array_keys( $localization );
foreach ( $locales as $locale ) {
if ( ! in_array( $locale, array( 'en', 'ar' ) ) ) {
Session::flash( 'message', 'Lang Error' );
return Redirect::back();
}
}
$model_about = new About();
$input = array();
foreach ( $localization as $locale => $parameters ) {
$model_about->translateOrNew($locale)->title = $parameters['title'];
$model_about->translateOrNew($locale)->details = $parameters['details'];
}
$model_about->save();
Just create a model object, use its function and save it afterwards.
Upvotes: 2