Simona Buga
Simona Buga

Reputation: 349

Laravel paginate

The model works well. The controller works well. The only place I'm having an error is in the view.

class Course extends Model
{
    use SoftDeletes, FilterByUser;

    protected $fillable = ['title', 'description', 'course_image', 'start_date', 'active', 'mandatory', 'created_by_id'];
    protected $hidden = [];
    public static $searchable = [
        'title',
        'description',
    ];


    public static function boot()
    {
        parent::boot();

        Course::observe(new \App\Observers\UserActionsObserver);
    }

    /**
     * Set attribute to date format
     * @param $input
     */
    public function setStartDateAttribute($input)
    {
        if ($input != null && $input != '') {
            $this->attributes['start_date'] = Carbon::createFromFormat(config('app.date_format'), $input)->format('Y-m-d');
        } else {
            $this->attributes['start_date'] = null;
        }
    }

    /**
     * Get attribute from date format
     * @param $input
     *
     * @return string
     */
    public function getStartDateAttribute($input)
    {
        $zeroDate = str_replace(['Y', 'm', 'd'], ['0000', '00', '00'], config('app.date_format'));

        if ($input != $zeroDate && $input != null) {
            return Carbon::createFromFormat('Y-m-d', $input)->format(config('app.date_format'));
        } else {
            return '';
        }
    }

    /**
     * Set to null if empty
     * @param $input
     */
    public function setCreatedByIdAttribute($input)
    {
        $this->attributes['created_by_id'] = $input ? $input : null;
    }

    public function created_by()
    {
        return $this->belongsTo(User::class, 'created_by_id');
    }

    public function trainers()
    {
        return $this->belongsToMany(User::class, 'course_user');
    }

    public function lessons()
    {
      return $this->hasMany('\App\Lesson');
    }




}

I seem to have an issue with pagination. Here is the code I have for the controller and that works well.

public function index()
{
  $course =Course::paginate(15);
  return view('admin.courses.learn', compact('course'));
}

Here is what I have for the view:

{{$course->links()}} 

this is where I get an error Call to undefined method App\Course::link()

Does anyone know what I'm doing wrong?

Upvotes: 0

Views: 148

Answers (2)

Bhoomi Patel
Bhoomi Patel

Reputation: 775

The Controller Code :

public function index()
{
    $course =Course::paginate(15);
    return view('admin.courses.learn', compact('course'));
}

Here is for the view:

@foreach($course as $row)
    //Whatever you wanted to display will be written here
@endforeach
{!! $course->render() !!}

OR

@foreach($course as $row)
 //Whatever you wanted to display will be written here
@endforeach
 {{$course->links()}

Upvotes: 2

Jithesh Jose
Jithesh Jose

Reputation: 1814

The Controller code is fine.

 public function index()
 {
 $course =Course::paginate(15);
 return view('admin.courses.learn', compact('course'));
 }

Now let's take a look at view.

 @foreach($course as $row)
 //Whatever action you wanted to do will be written here
 @endforeach
 {{$course->links()}} //The name should be differ than the name we used inside the foreach loop.

Upvotes: 0

Related Questions