skip to Main Content

I’m having trouble passing a variable that stores a certain value to with(), it returns what I want, but it returns the following message.

[{"name":"Ryu"}] won updates in your informations. rather then Ryu won updates in your informations.

    public function update(FighterRequest $request, $id)
    {
        $validations = $request->validated();
        FighterModel::where('id',$id)->update($validations);
        $name_fighter = DB::table('fighters')->select('name')->where('id','=',$id)->get();
        return redirect('fighter')->with('success-update',"$name_fighter won updates in your informations.");  
    }

3

Answers


  1. Your $name_fighter returns a collection. So rather than using $name_fighter directly, you need to specify what you want to get $name_fighter->name

    Login or Signup to reply.
  2. $name_fighter = DB::table('fighters')->where('id','=',$id)->value('name');
    
    Login or Signup to reply.
  3. Would try this version because get return multiple objects but first only object

    
      public function update(FighterRequest $request, $id)
        {
            $validations = $request->validated();
            FighterModel::where('id',$id)->update($validations);
            $name_fighter = DB::table('fighters')->select('name')->where('id','=',$id)->first();
            return redirect('fighter')->with('success-update',"$name_fighter->name won updates in your informations.");  
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search