我创建了一个特性,如果它不存在,就会在数据库中创建一个表。 该表在DB中完美创建但我收到错误。错误是由Illuminate \ Database \ Eloquent \ Concerns \ HasAttributes.php抛出
特点:
namespace App\Traits;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
trait Translatable{
//CREATE LANGUAGES TABLE IF DOES NOT EXIST
public function createTranslationTable()
{
//ADD LANGUAGES TABLE IF DOES NOT EXIST
if(Schema::hasTable('languages') === false){
Schema::create('languages', function (Blueprint $table) {
$table->increments('id');
$table->string('language');
$table->string('slug');
});
}
//CREATE TRANSLATION TABLE IF DOES NOT EXIST
if(Schema::hasTable('translations') === false){
Schema::create('translations', function (Blueprint $table) {
$table->increments('id');
$table->string('model');
$table->string('language_id');
$table->foreign('language_id')->references('id')->on('languages')->onDelete('cascade');
$table->string('field_name');
$table->string('field_translation');
});
}
}
public function translateMe()
{
$this->createTranslationTable;
}
}
ERROR:
App\User::createTranslationTable must return a relationship instance.
我正在使用一个特性,因为我希望它能够在任何模型中使用它,例如:
class User extends Authenticatable
{
use Notifiable;
use Translatable;
/**
* Call Translatable.
*
* @return void
*/
public function __construct()
{
$this->translateMe();
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that are translatable.
*
* @var array
*/
protected $translatableItems = [
'name'
];
}
答案 0 :(得分:0)
我找到了解决方案,这是一个简单的解决方案。 在Translatable特性中,我将createTranslationTable方法称为属性,而不是方法。所以只需添加两个parentesis修复它。
在:
public function translateMe()
{
$this->createTranslationTable;
}
现在:
public function translateMe()
{
$this->createTranslationTable();
}