Home > Software engineering >  Problems passing a variable to with()
Problems passing a variable to with()

Time:11-13

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.");  
    }

CodePudding user response:

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

CodePudding user response:

$name_fighter = DB::table('fighters')->where('id','=',$id)->value('name');

CodePudding user response:

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.");  
    }
  • Related