0

该模型运行良好。控制器运行良好。我唯一有错误的地方是在视图中。

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()}} 

这是我收到错误调用未定义方法 App\Course::link() 的地方

有谁知道我做错了什么?

4

2 回答 2

2

控制器代码:

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()}
于 2019-04-29T06:52:45.677 回答
0

控制器代码很好。

 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.
于 2019-04-29T06:35:42.253 回答