laravel 试图获取非对象的属性(视图:C:\xampp\htdocs\enginepoker2\resources\views\pages\player_edit.blade.php)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52420061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:04:32  来源:igfitidea点击:

Trying to get property of non-object (View: C:\xampp\htdocs\enginepoker2\resources\views\pages\player_edit.blade.php)

phplaraveleloquent

提问by Muhammad Romi Muhtarom

I'm beginner in laravel, when I put stats table in blade I got this warning

我是 Laravel 的初学者,当我将统计表放在刀片中时,我收到了这个警告

Trying to get property of non-object (View: C:\xampp\htdocs\enginepoker2\resources\views\pages\player_edit.blade.php)

试图获取非对象的属性(视图: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

这是在 Stat 模型中

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', 'botsController@edit')->name('editBot');

this is in player_edit.blade that i get warning of non-object

这是在 player_edit.blade 中,我收到非对象警告

<p>
   <span style="display: inline-block; min-width: 150px;">Rank</span>
   <span class="badge black">{{ $player->stats->rank }}</span>
</p>

回答by Nitheesram Rajes

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.

通常,在尝试将数组作为对象访问时会发生“尝试获取非对象的属性”错误。确保从表中检索到的所有数据都不是数组。