如何解决在空路由模型绑定Laravel 7上调用成员函数where
之前对我的英语不好对不起。什么是那个错误? “调用成员函数where()为null”。在我的代码中,需要获取每个类别的所有应用程序。
使用模型路由绑定很有趣,当我插入一个模型时,它很好用,例如:Application。示例:
Route::get(applications/{application},'ApplicationController@show')->name('apps'));
但是当我尝试在路由中使用2模型时,会出现此问题。这是我的代码:
Route::get('categories/{category}/applications/{application:category_id}','ApplicationController@show')->name('apps');
我的模特:
class Application extends Model{
protected $fillable = ['name','price','category_id','vote','image_src'];
public function users()
{
return $this->belongsToMany(User::class,'applications_users_states','application_id','user_id')
->withPivot('state_id')
->withTimestamps();
}
public function categories()
{
return $this->belongsTo(Category::class);
}
public function logs()
{
return $this->belongsToMany(Log::class)->withTimestamps();
}
}
class Category extends Model
{
protected $fillable = ['name','description'];
public function applications()
{
$this->hasMany(Application::class);
}
}
我的控制器:
public function show(Category $category,Application $application)
{
return $application;
}
感谢您的时间和知识!!!!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。