问题描述
我正在尝试使用laravel中的关系来获取对象。在我的项目中,1个安装属于1个项目,并且1个项目可以具有多个安装。在迁移中,我的编码如下:
项目:
Schema::create('projects',function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->unsigned();
$table->string('name');
$table->boolean('removed');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
安装:
Schema::create('installations',function (Blueprint $table) {
$table->id();
$table->bigInteger('country_id')->unsigned();
$table->bigInteger('project_id')->unsigned();
$table->bigInteger('building_admin_id')->unsigned()->nullable();
$table->string('name');
$table->boolean('removed');
$table->timestamps();
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('project_id')->references('id')->on('projects');
$table->foreign('building_admin_id')->references('id')->on('users');
在模型中……
安装:
public function project(){
return $this->belongsTo(Project::class);
}
项目:
public function installations(){
return $this->hasMany(Installation::class);
}
这种情况是我有一个中间件,可以使用以下方法检查安装的作者是否是经过身份验证的用户:
if($request->installation->project->user->id != Auth::user()->id){
return back()->with('notAuthor','Access denied. You are not the owner of this installation.');
}
return $next($request);
Laravel给我一个错误。我已经检查了原因,并在进行dd($request->installation)
时收到installation_id
而不是整个对象。
这种情况是我在删除和修改安装方法上使用了这种中间件,并且可以正常工作,为什么在这种情况下不行?
我知道知道安装的ID后就可以通过查询来获取它,但是我想知道为什么它不起作用,对我来说毫无意义。
如果您需要查看更多代码,请给我发短信。
关于..
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)