dildo_beggins's avatar

How to paginate a collection?

Probably what I am doing in my code is weird, but eventually I need to paginate my output. And in this case I am not sure how...

Here's my code ;

public function uploads() {

    $user_id = Auth::id();
    $uploads = Upload::whereHas('user', function ($query) use($user_id) {
        $query->where('id', $user_id);
    })->get();

    foreach ( $uploads  as  $upload) {
        $author = Avtor::where('id', $upload->avtor_id)->get();
        foreach ( $author  as  $auth) {
            $upload->avtor_id = $auth->name;
        }
    }
    return view('uploads')->with('uploads',$uploads);
}

Any tips on how to apply pagination to $uploads with the current code I have(or what do I need to change in the current code?)?

0 likes
6 replies
rawilk's avatar

You should be able to do something like this:


$perPage = 25; // Or whatever you need
$uploads = Upload::whereHas('user', function ($query) {
    $query->where('id', auth()->id());
})->paginate($perPage);

// continue with code as you were

1 like
Snapey's avatar

Its easy to add pagination to the eloquent query, but the rest of your code has me puzzled

public function uploads() {

//    $user_id = Auth::id();
//    $uploads = Upload::whereHas('user', function ($query) use($user_id) {
//        $query->where('id', $user_id);
//    })->get();

    $uploads = Auth::user()->uploads()->paginate(10);


//next I don't understand. You want to set a property avtor_id in each upload?

    foreach ( $uploads  as  $upload) {
        $author = Avtor::where('id', $upload->avtor_id)->get();

// why iterate over author when you can only store one value?

        foreach ( $author  as  $auth) {
            $upload->avtor_id = $auth->name;
        }
    }
    return view('uploads')->with('uploads',$uploads);
}

Can you explain what you want to do?

dildo_beggins's avatar

Hey, I am doing this, because I couldn't figure out how to have the author name in this view otherwise.

In ASP.NET MVC you have something called view models that are used in similar cases but in laravel I couldn't find something similar. So to solve this, I am just querying the author id and then I am assigning the author name to where the author id was, so I can access the name in the view. Stupid, I know, but I couldn't figure out anything else.

Snapey's avatar

so with eloquent you define a relationship between upload and author. you can then eager load the author in the controller and access the name in the view

your controller then becomes

$uploads = Auth::user()->uploads()->with('avtor')->paginate(10);

then in the view, something like

{{ $upload->avtor->name }}
dildo_beggins's avatar

Thanks for the reply, Snapey. I getting an error though.

The error says : (1/1) BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::uploads()

Now my controller method is looking like this :

public function uploads() {

    $uploads = Auth::user()->uploads()->with('avtor')->paginate(10);

    return view('uploads')->with('uploads',$uploads);
}

And my models :

class Avtor extends Model { public function uploads(){ return $this->hasMany('App\Upload'); } }

class Upload extends Model {

public function user(){
    return $this->belongsTo('App\User');
}
public function author(){
    return $this->belongsTo('App\Avtor');
}

}

Do you know why the error?

sutherland's avatar

You need to add an uploads() relationship method to your user model.

Please or to participate in this conversation.