当我从邮递员测试我的REST API时,它给了我错误
Trying to get property of non-object
因为邮递员没有会话。如果有浏览器,则会创建会话但不会在邮递员中创建。任何人都可以提供Auth::user()
这是我的路线
路线::帖子('播放列表/ {id} /关注','播放列表控制器@关注');
这是我的代码段。
public function follow($id)
{
$user = $this->model->findOrFail($id);
if ($user->id != Auth::user()->id) {
Auth::user()->followedUsers()->attach($id);
}
}
答案 0 :(得分:0)
在api_token
表中创建一个名为User
的列,并在中间件auth:api
下添加所有路由。
然后尝试从api_token
中的邮递员发送Headers
值 -
Authorization Bearer: <api_token_here>
Authorization
是标题名称,其值为Bearer: <api_token_here>
然后您的Auth::user()->id
将按预期工作。
答案 1 :(得分:-1)
尝试更改您的功能: (我假设你的模型是用户)
public function follow(User $id)
{
if ($id->id != Auth::user()->id) {
Auth::user()->followedUsers()->attach($id);
}
}