splendidkeen
11 months ago
116
6
Laravel

UserController:storeUserInfo: 1 passed and exactly 2 expected

Posted 11 months ago by splendidkeen

Use case:

I am currently on a User profile, as a logged in Partner. I now want to add an Info(infos()) to $user as the Partner.

User don't need to access the Info.php Model.

As we visit the User through the $username variable, I am facing problems to get the $user->id on the post method to create an Info for this user.

Currently I am getting this error on the post method: 1 passed and exactly 2 expected, it is not getting the $username. 

How can I get $user->id?

Web.php

   Route::get('/user/{username}', '[email protected]')->name('partner.user.profile')->middleware('auth:partner');

   Route::post('/user/info/create', '[email protected]')->name('partner.user.info.create')->middleware('auth:partner');

UserController


   public function profile($username){

      // Route: {{route('partner.user.profile', ['username' => $user->username])}}

      $user = User::where('username', $username)->first();
      return view('partner.user.profile', compact('user'));

   }

   public function storeUserInfo(Request $request, $username){


       // Route (form): {{route('partner.user.info.create')}}
        
       $partner = Auth::user();
       $user = User::where('username', $username)->first(); 

       $this->validate($request, [ 
           'info'=> 'max:25', 
       ]);

       // Partner.php hasMany(Info)  / Info.php belongsTo(Partner)
       
       if($note = Auth::user()->infos()->create([
           'partner_id' => auth()->user()->id,
           'user_id' => $user->id, // How can I get the id of the User?
           'info' => $request->info,  
       ])){

           // More
       };
       
       return redirect()
           ->route('partner.user.profile')
           ->with('info', 'Info created.');
   }

Kindest Regards

Please sign in or create an account to participate in this conversation.