All we need is an easy explanation of the problem, so here it is.
I’m beginner in laravel, when I put stats table in blade I got this warning
Trying to get property of non-object (View:
C:\xampp\htdocs\enginepoker2\resources\views\pages\player_edit.blade.php)
this is in model Player
class Player extends Model
{
protected $player = 'players';
public function stats()
{
return $this->belongsTo(Stat::class, 'player');
}
}
this is in Stat model
class Stat extends Model
{
protected $stat = 'stats';
}
this is in player Controller
use App\Stat;
use App\Player;
use App\dealers;
use Illuminate\Http\Request;
class botsController extends Controller
{
public function edit(Player $player, Stat $stat, dealers $dealer)
{
return view('pages.player_edit', compact('player', 'stat', 'dealer'));
}
}
this is in route
Route::get('/index/bots/id={player}/edit', '[email protected]')->name('editBot');
this is in player_edit.blade that i get warning of non-object
<p>
<span style="display: inline-block; min-width: 150px;">Rank</span>
<span class="badge black">{{ $player->stats->rank }}</span>
</p>
How to solve :
I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.
Method 1
Generally this “Trying to get property of non-object” error is occurred while trying to access an array as an object.
Make sure all the retrieved data from table are not array.
Method 2
check database is filled with values some values
in my case Trying to get property of non-object this error is due to my database is emppty
Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0