[英]Laravel 5.2: dimsav Call to a member function translateOrNew() on a non-object
I am using Laravel 5.2 Package dimsav laravel-translatable 6.* 我正在使用Laravel 5.2软件包dimsav laravel-translateable 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
在非对象上调用成员函数translateOrNew()
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'];
}
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. 您想在该数组上使用函数
translateOrNew()
,该函数不起作用,因为该数组没有功能。 You have to call the function from somewhere else and save an array with create. 您必须从其他地方调用该函数,然后使用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. 我必须承担许多事情,因为我不知道
About
模型的样子。 Additionally I assume that there is a static method translateOrNew()
available in this model. 另外,我假设此模型中有一个可用的静态方法
translateOrNew()
。 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. 只需创建一个模型对象,使用其功能并随后保存即可。
You forgot to include the following in the model class. 您忘记在模型类中包括以下内容。
Use \\Dimsav\\Translatable\\Translatable;
使用
\\Dimsav\\Translatable\\Translatable;
in your Model file, which is here: \\App\\Article.php
在模型文件中,该文件位于:
\\App\\Article.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.