如何解决如何使Laravel关系正常工作?
我正在尝试使用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 举报,一经查实,本站将立刻删除。