lundi 16 mars 2020

laravel BelongsToMany pagination

i have 2 model

1 - category with parent and child

class Category extends Model
{


protected $primaryKey = 'id';
public $fillable = ['slug','title','icon','parent_id'];
/**
 * Get the index name for the model.
 *
 * @return string
*/
public function childs() {
    return $this->hasMany('App\Category','parent_id','id') ;
}
public function parent()
{
    return $this->belongsTo('App\Category', 'parent_id','id');
}
public function posts(){

    return $this->belongsToMany('App\Post', 'category_post');
}
public function latestposts(){

    return $this->belongsToMany('App\Post', 'category_post')->latest()->limit(3);
}


}

2 - post model with belongsToMany->Category

class Post extends Model 
{

protected $primaryKey = 'id';
public $fillable = ['user_id','slug','title','excerpt','content','keyword','img','status'];

protected $table = 'posts';


public function cat_posts(){
    return $this->belongsToMany('App\Category', 'category_post');

}


}

example data treeview:

1-category parent 

 |- 1-child category -> with 5 post

 |- 2-child category -> with 3 post

Now, when viewing the main section -> (category parent), I want to display all the posts in all of the children's category To be the result of the number of posts = 8

And by pagination How can I do that



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/39XhAxs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire