0

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?

Niraj Shah
  • 15,087
  • 3
  • 41
  • 60
Simona Buga
  • 341
  • 6
  • 22
  • The error message says the method `link()` is not defined, shouldn't it be `links()`? – newUserName02 Feb 19 '19 at 20:37
  • how are you passing it to the view? – Toby Okeke Feb 19 '19 at 20:42
  • 1
    Can you show us the content of $course? – JoaoGRRR Feb 19 '19 at 20:43
  • I must have accidentally erase the links in the error message. I'll post the entire controller function and the content of the course. – Simona Buga Feb 19 '19 at 21:01
  • 2
    Are you looping `$course` anywhere before calling `$course->links()`? I.e are you doing `@foreach($course AS $course)`? Cause that would override `$course` to be a Model instance, and `links()` isn't a method on an instance. You should call your variable `$courses` (plural), so when you loop it you use `$course` (singular). That or you're calling `->link()` when you should be calling `->links()`. I think you need to provide more code. – Tim Lewis Feb 19 '19 at 21:07
  • Yes and everything works great until I add the pagination links. – Simona Buga Feb 19 '19 at 21:15
  • Share full code we can't predict what's going on from hints! Share view full code – Iftikhar uddin Feb 19 '19 at 21:20
  • OMG you are a genius. It worked. It was overriding each other. Nice catch. I worked on this for 14 hours and grew 3 white hairs. Thank you so much Tim. – Simona Buga Feb 19 '19 at 21:22

2 Answers2

2

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()}
Bhoomi Patel
  • 777
  • 10
  • 32
0

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.
Jithesh Jose
  • 1,781
  • 1
  • 6
  • 17