将多对多关系中的数组传递给控制器- laravel
Passing array in many to many relationship to the controller- laravel
我在 Category 和 posts 表之间有这样的多对多关系:
类别class:
class Category extends Model {
public function posts()
{
//return $this->hasMany('App\Posts','category_id');
return $this->belongsToMany('App\Posts', 'categories_post', 'category_id', 'post_id')
->withTimestamps();
}
}
帖子 class:
class Posts extends Model {
public function category()
{
//return $this->belongsTo('App\Category','category_id');
return $this->belongsToMany('App\Category', 'categories_post', 'post_id', 'category_id')
->withTimestamps();
}
}
当我只需要访问一个类别的帖子时,我在我的控制器中这样做:
public function cat($category_id)
{
$posts= $category_id->posts()->paginate(7);
return view('cat')->with('posts',$posts);
}
编辑
为了完成这项工作,我在 "RouteServiceProvider.php" 文件中添加了这个:
public function boot(Router $router)
{
parent::boot($router);
$router->model('categories','App\Category');
}
这非常有效。问题是,我有另一个控制器应该获取多个类别的帖子 :
public function index()
{
$title = 'news';
$categories= [1, 2, 10, 11];
// Here is the problem:
$posts= $categories->posts()->paginate(7);
return view('news')->with('posts',$posts)->with('title',$title);
}
这给了我这个错误:在非对象上调用成员函数 posts()
我知道调用数组有问题,但不知道如何解决。谁能帮帮我:)
解决方法
根据 Thomas Van der Veen 在他的回答中所说的,我想出了这个完美运行的控制器:
public function index()
{
$title = 'news';
$category_ids = [1, 2, 10, 11];
$posts = Posts::whereHas('category', function($query) use ($category_ids) {
$query->whereIn('id', $category_ids);
})->get();
return view('news')->with('posts',$posts)->with('title',$title);
}
你可以这样做:
$category_ids = [1, 2, 10, 11];
$posts = Post::whereHas('categories', function($query) use ($category_ids) {
$query->whereIn('id', $category_ids);
});
见this。
我在 Category 和 posts 表之间有这样的多对多关系:
类别class:
class Category extends Model {
public function posts()
{
//return $this->hasMany('App\Posts','category_id');
return $this->belongsToMany('App\Posts', 'categories_post', 'category_id', 'post_id')
->withTimestamps();
}
}
帖子 class:
class Posts extends Model {
public function category()
{
//return $this->belongsTo('App\Category','category_id');
return $this->belongsToMany('App\Category', 'categories_post', 'post_id', 'category_id')
->withTimestamps();
}
}
当我只需要访问一个类别的帖子时,我在我的控制器中这样做:
public function cat($category_id)
{
$posts= $category_id->posts()->paginate(7);
return view('cat')->with('posts',$posts);
}
编辑
为了完成这项工作,我在 "RouteServiceProvider.php" 文件中添加了这个:
public function boot(Router $router)
{
parent::boot($router);
$router->model('categories','App\Category');
}
这非常有效。问题是,我有另一个控制器应该获取多个类别的帖子 :
public function index()
{
$title = 'news';
$categories= [1, 2, 10, 11];
// Here is the problem:
$posts= $categories->posts()->paginate(7);
return view('news')->with('posts',$posts)->with('title',$title);
}
这给了我这个错误:在非对象上调用成员函数 posts()
我知道调用数组有问题,但不知道如何解决。谁能帮帮我:)
解决方法
根据 Thomas Van der Veen 在他的回答中所说的,我想出了这个完美运行的控制器:
public function index()
{
$title = 'news';
$category_ids = [1, 2, 10, 11];
$posts = Posts::whereHas('category', function($query) use ($category_ids) {
$query->whereIn('id', $category_ids);
})->get();
return view('news')->with('posts',$posts)->with('title',$title);
}
你可以这样做:
$category_ids = [1, 2, 10, 11];
$posts = Post::whereHas('categories', function($query) use ($category_ids) {
$query->whereIn('id', $category_ids);
});
见this。