1

I am trying to get data from one form and save it to another db table with modifications. But, I am getting error:

trying to get property of non-object

on the last line from the given block of codes.

$ply->team_name = $request->get('team_name');

    $team_name=$ply->team_name;
    $team=Team::find($team_name);
    $team->balance=$team->balance-$ply->sold_amount;
2
  • Most probably $team is null because it was not found in the previous line. Commented Sep 25, 2017 at 7:54
  • 1
    the problem is here $team=Team::find($team_name); find is equivalent to where id = $var so it will return null to fix it try this $team=Team::where('name', $team_name)->get(); Commented Sep 25, 2017 at 7:54

1 Answer 1

1

The find method expects an id as parameter, when you are giving it a string.

Model::find($id);

You can use the where clause :

Model::where('name', $name)->first();
/*
 * Or
*/
Model::where('name', $name)->get();

Aftet that, you should check if your $team is not null before trying to access its properties.

Finally, I strongly recommend to use the dd($var) for debugging purpose, it is very helpful.

Sign up to request clarification or add additional context in comments.

6 Comments

there is no problem here $team->balance=$team->balance-$ply->sold_amount; this is equivalent to $team->balance -= $ply->sold_amount; ;)
It is okay. I also forgot to change the primary key.
Still getting property balance not found. With dd($team) getting 0 inside array as output.
Collection {#188 ▼ #items: array:1 [▼ 0 => Team {#187 ▶} ] }
You have your team inside the array, check its property names. Dumb question : The balance field exists on the database right ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.