【发布时间】:2021-09-15 01:12:55
【问题描述】:
我已经学习了 2 个小时的教程,但现在我遇到了问题。本教程使用的是旧版本的 Laravel。我正在创建 Instagram 克隆,并想创建帖子。
web.php
Route::post('/p', 'PostsController@store');
PostsController
public function store()
{
$data = request()->validate([
'caption' => 'required',
'image' => ['required', 'image'],
]);
/* I got an error at this line saying undefined method post() */
auth()->user()->posts()->create($data);
}
用户模型
public function posts()
{
return $this->hasMany(Post::class)
->orderBy('created_at', 'DESC');
}
【问题讨论】:
标签: php laravel authentication