Laravel paginate - PullRequest
       33

Laravel paginate

0 голосов
/ 19 февраля 2019

Модель хорошо работает.Контроллер работает хорошо.Единственное место, где у меня ошибка, это вид.

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');
    }




}

Кажется, у меня проблема с разбиением на страницы.Вот код, который у меня есть для контроллера, и он хорошо работает.

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

Вот то, что у меня есть для представления:

{{$course->links()}} 

это где я получаю ошибку.undefined method App \ Course :: link ()

Кто-нибудь знает, что я делаю не так?

Ответы [ 2 ]

0 голосов
/ 29 апреля 2019

Код контроллера:

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

Вот для просмотра:

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

ИЛИ

@foreach($course as $row)
 //Whatever you wanted to display will be written here
@endforeach
 {{$course->links()}
0 голосов
/ 29 апреля 2019

Код контроллера в порядке.

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

Теперь давайте посмотрим на представление.

 @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.
...