该模型运行良好。控制器运行良好。我唯一出错的地方是视图。
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()
有人知道我在做什么错吗?
答案 0 :(得分:1)
控制器代码:
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() !!}
OR
@foreach($course as $row)
//Whatever you wanted to display will be written here
@endforeach
{{$course->links()}
答案 1 :(得分: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.