简体   繁体   中英

Paginate Results of Model Laravel

I have a problem with pagination in laravel 5.3

The code:

public function deals()
{
    return $this->belongsToMany('App\Models\ListsDeals', 'list_has_deals' , 'list_id', 'deal_id')->withPivot('list_id');
}


public function form_edit_list( $id ){

    $list = Lists::find( $id );

    PAGINATE THIS -----> $deals = $list->deals;

    $user = User::find( $list->id_user );

    $categoriesArray = ListsCategories::all();

    $featuresArray = ListsFeatures::all();

    $images = ListsGalleries::all();

    return view( "admin.forms.form_edit_list" )
                    ->with( "list", $list )
                    ->withCategoriesArray( $categoriesArray )
                    ->withFeaturesArray( $featuresArray )
                    ->withImages( $images )
                    ->with( "user", $user );                          
    }

I have tried this,

$deals = $list->deals->paginate(5);

How can I paginate the results of deals ?

Because paginate is not a method of that.

I believe you can add the paginate() call to the deals() relationship definition (which will paginate all uses of it).

That may not be ideal, so you can also do $deals = $list->deals()->paginate();

$list->deals is a collection of items, while $list->deals() is an Eloquent query builder instance you can make further adjustments to before fetching the reuslts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM