简体   繁体   中英

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(); or $user=User::find($id); don't work and have error:

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

and when use $user=Auth::user(); save the current user 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,
You have two $id here, $user->id and $team->id ,
Firstly, you can't use $user->id because all() method returns array not object . So you have to use $userId = Auth::user()->id instead. and var_dump($team) to make assure that it has property id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM