简体   繁体   English

Laravel:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ id

[英]Laravel:Undefined property: Illuminate\Database\Eloquent\Collection::$id

i want the current user when add user in a team will save in database user id for this user not id for current user but when i use $user=User::all(); 我希望当前用户在团队中添加用户时会在数据库中为该用户保存用户ID,而不是当前用户的ID,但是当我使用$user=User::all(); or $user=User::find($id); $user=User::find($id); don't work and have error: 不起作用并且有错误:

 Undefined property: Illuminate\Database\Eloquent\Collection::$id

and when use $user=Auth::user(); 以及何时使用$user=Auth::user(); save the current user id 保存当前用户ID

public function addUser($id)
{

            $teams =TeamWork::find($id);

            $user =User::all();

            $teammember = new TeamMember;
            $teammember->user()->associate($user->id);
            $teammember->Teamwork()->associate($teams->id);
            $teammember->save();

            $users = User::pluck('username');

        return View('teams.adduser' , compact('users'))->with('teams', $teams);


      }

Salam Alikom, Salam Alikom,
You have two $id here, $user->id and $team->id , 您在这里有两个$id$user->id$team->id
Firstly, you can't use $user->id because all() method returns array not object . 首先,您不能使用$user->id因为all()方法返回的array not object So you have to use $userId = Auth::user()->id instead. 因此,您必须改用$userId = Auth::user()->id and var_dump($team) to make assure that it has property id . var_dump($team)以确保它具有属性id

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 laravel 未定义属性:Illuminate\\Database\\Eloquent\\Collection::$id - laravel Undefined property: Illuminate\Database\Eloquent\Collection::$id 未定义属性:Illuminate \ Database \ Eloquent \ Collection :: $ id Laravel 4 - Undefined property: Illuminate\Database\Eloquent\Collection::$id Laravel 4 Laravel 错误 leftJoin:未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$id - Laravel Error leftJoin : Undefined property: Illuminate\Database\Eloquent\Collection::$id 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ name Laravel 5.3 - Undefined property: Illuminate\Database\Eloquent\Collection::$name Laravel 5.3 Laravel 5未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ basicdetails - Laravel 5 Undefined property: Illuminate\Database\Eloquent\Collection::$basicdetails laravel 5.3:未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection错误 - laravel 5.3 : Undefined property: Illuminate\Database\Eloquent\Collection Error Laravel关系错误:未定义属性:第1行上的Illuminate \\ Database \\ Eloquent \\ Collection :: $ id - Laravel relationship error: Undefined property: Illuminate\Database\Eloquent\Collection::$id on line 1 未定义的属性:Illuminate\\Database\\Eloquent\\Collection::$likes laravel - Undefined property: Illuminate\Database\Eloquent\Collection::$likes laravel 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection :: Laravel 5.2 - Undefined property: Illuminate\Database\Eloquent\Collection:: Laravel 5.2 未定义的属性:Illuminate \\ Database \\ Eloquent \\ Collection - Undefined property: Illuminate\Database\Eloquent\Collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM