luanrodriguesp's avatar

[L5] Pagination Model

Hello guys, Im trying to paginate a eloquent relation and im getting a lot of problems.

This is my model...


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\SoftDeletes;

class Modelo extends Model {

use softDeletes;
public $errors;
protected $table = 'modelos';
protected $dates = ['deleted_at'];
protected $fillable = ['id_usuario', 'foto_perfil', 'nome', 'slug', 'idade', 'id_cidade', 'sexo', 'altura', 'peso', 'quadris', 'atendimento_tipo', 'atendimento_lugar', 'cache', 'telefone1', 'telefone2', 'telefone3', 'cartao', 'perfil', 'publicado', 'status'];



public function fotos()
{
    return $this->hasMany('App\Foto', 'id_modelo', 'id');
}

public function usuario()
{
    return $this->hasOne('App\User', 'id', 'id_usuario');
}

public function comentarios()
{
    return $this->hasMany('App\Comentario', 'id_modelo', 'id'); // i want to paginate this one...
}

public static function modeloBySlug($slug){
    return Modelo::where('slug', '=', $slug)
        ->where('publicado', '=', '1')
        ->where('status', '=', '2')
        ->get();
}

}

can you guys please help me to solve it?

thank you in advance

0 likes
4 replies
timokfine's avatar

Paginate it from your controller.

$comentarios = Comentario::paginate(20);
return view('example', compact('comentarios'));
Sonu's avatar

@luanrodriguesp Just use this @ your controller.

$someVariable = Comentario::paginate(15);
return view('example')->with('someVariable ',$someVariable );
1 like
luanrodriguesp's avatar

@Sonu @tmartinco this will not work because it will get all data from table comentarios, i dont need this.

What I need is load just the data that correspond with the relation bellow

public function comentarios()
{
    return $this->hasMany('App\Comentario', 'id_modelo', 'id'); // i want to paginate this one...
}
JarekTkaczyk's avatar

@luanrodriguesp This one is wrong:

public function usuario()
{
    return $this->hasOne('App\User', 'id', 'id_usuario');
}

should be hasOne('App\User', 'id_usuario').

Now, about pagination - describe the problem you're having.

Please or to participate in this conversation.