简体   繁体   English

此集合实例上不存在属性[stats]

[英]Property [stats] does not exist on this collection instance

I am new in laravel and I got this error 我是laravel的新手,但出现此错误

Property [stats] does not exist on this collection instance. 此集合实例上不存在属性[stats]。 (View: C:\\xampp\\htdocs\\enginepoker2\\resources\\views\\pages\\players.blade.php) (查看:C:\\ xampp \\ htdocs \\ enginepoker2 \\ resources \\ views \\ pages \\ players.blade.php)

this is in players.blade.php 这在players.blade.php中

@foreach ($player_stats->stats as $player_stat)

                        <tr class="gradeX">
                        <td><a href="">{{ $player_stat->username }}</a></td>
                        <td>{{ $player_stat->country }}</td>
                        <td>{{ $player_stat->rank }}</td>
                        <td class="center"><a href="#" class="name" data-name="winpot" data-pk="{{ $player_stat->ID }}" data-type="text" data-url="{{ route('updatePlayer') }}">{{ number_format($player_stat->winpot, 2, ',', '.') }}</a></td>
                        <td class="center"><a href="#" class="name" data-name="freeGames" data-pk="{{ $player_stat->ID }}" data-type="text" data-url="{{ route('updatePlayer') }}">{{ $player_stat->freeGames }}</a></td>
                            <td class="center"><a href="#" class="name" data-name="transferLimit" data-pk="{{ $player_stat->ID }}" data-type="text" data-url="{{ route('updatePlayer') }}">{{ number_format($player_stat->transferLimit, 2, ',', '.') }}</a></td>

                        </tr>

                        @endforeach

this is in controller 这在控制器中

use App\players;
use Illuminate\Http\Request;

class playerController extends Controller
{

    public function index()
    {       
        $player_stats = players::where('bot', '=', '0')->orderBy('ID', 'DESC')->limit('500')->get();
        return view('pages.players', [ 'player_stats'=>$player_stats]);

    }
}

this is in players model 这是在玩家模型中

namespace App;

use Illuminate\Database\Eloquent\Model;

class players extends Model
{
    protected $player = 'players';

    public function stats()
    {
        return $this->belongsTo(stats::class);
    }

}

this is in stats model 这是统计模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class stats extends Model
{
    protected $player = 'stats';

    public function players()
    {
        return $this->hasMany(players::class);
    }    
}

First i think you should change first naming of your model with Capitalize name. 首先,我认为您应该先使用大写名称更改模型的命名。 I mean change class player to class Player and class stats to class Stats. 我的意思是将类玩家更改为类玩家,并将类统计更改为类统计。

Second, use namespacing when try to add relation in laravel. 第二,尝试在laravel中添加关系时使用namespacing。 Try change. 尝试改变。

public function stats()
{
   return $this->belongsTo(stats::class);
}

to

public function stats()
{
    return $this->belongsTo('App\Stats');
}

also change the 也改变了

public function players()
{
   return $this->hasMany(players::class);
}

to

public function players()
{
    return $this->hasMany('App\Players');
}

stats is a relationship and relationships are not loaded by default; stats是一种关系,默认情况下不会加载关系; you should change your query in the controller to load the relationship. 您应该在控制器中更改查询以加载关系。 use this: 用这个:

$player_stats = players::where('bot', '=', '0')->with('stats')
->orderBy('ID', 'DESC')->limit('500')->get();

EDIT: based on comments, you should tell laravel which columns to use for the relationship. 编辑:基于注释,您应该告诉laravel哪些列用于关系。 and also the relationship was defined in reverse. 而且关系是相反定义的。 try this: 尝试这个:

change your relationships too: 也改变你的关系:

class players extends Model
{
    protected $player = 'players';

    public function stats()
    {
        return $this->hasMany(stats::class, 'player', 'username');
    }
}

class stats extends Model
{
    protected $player = 'stats';

    public function players()
    {
        return $this->belongsTo(players::class, 'player', 'username');
    }     
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM