簡體   English   中英

Laravel:未定義的屬性:Illuminate \\ Database \\ Eloquent \\ Collection :: $ id

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

我希望當前用戶在團隊中添加用戶時會在數據庫中為該用戶保存用戶ID,而不是當前用戶的ID,但是當我使用$user=User::all(); $user=User::find($id); 不起作用並且有錯誤:

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

以及何時使用$user=Auth::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,
您在這里有兩個$id$user->id$team->id
首先,您不能使用$user->id因為all()方法返回的array not object 因此,您必須改用$userId = Auth::user()->id var_dump($team)以確保它具有屬性id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM