Laravel Vue.Js中外键为空时的关系错误
这是我的新项目Laravel Vue.Js中外键为空时的关系错误,laravel,vue.js,Laravel,Vue.js,这是我的新项目 public function user() { return $this->hasOne(User::class,'id' , 'user_id'); } 这是我的函数用户 public function newproject() { return $this->belongsTo(Newproject::class,'user_id','id'); } 这是我的控制器 public functio
public function user()
{
return $this->hasOne(User::class,'id' , 'user_id');
}
这是我的函数用户
public function newproject()
{
return $this->belongsTo(Newproject::class,'user_id','id');
}
这是我的控制器
public function show($id)
{
$newproject = Newproject::findOrFail($id);
$data=[
Newproject::with('User')->find($id)
];
return $data;
}
我的vue
<tr v-for="show in detail" :key="show.id">
<td>{{show.id}}</td>
<td>{{show.user.name}}</td>
</tr>
{{show.id}
{{show.user.name}
为什么外键为null时总是出现这样的错误
[Vue warn]: Error in render: "TypeError: Cannot read property 'id' of null"
found in
---> <Projectwork> at resources/js/components/projectwork.vue
<Root>
[Vue warn]:呈现中出错:“TypeError:无法读取null的属性“id”
发现于
--->在resources/js/components/projectwork.vue上
请帮忙,谢谢。我想你应该把unsigned()放在这样的模式中
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('role_id')->index()->unsigned()->nullable();
$table->rememberToken();
$table->timestamps();
});
}
您可以查看下面的文档以了解更多信息