property[stats]

im9ewurl  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(225)

我是拉雷维尔的新人,我犯了这个错误
此集合示例上不存在属性[stats](视图:c:\xampp\htdocs\enginepoker2\resources\views\pages\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

这是在控制器中

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]);

    }
}

这是玩家模型

namespace App;

use Illuminate\Database\Eloquent\Model;

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

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

}

这是在统计模型中

namespace App;

use Illuminate\Database\Eloquent\Model;

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

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

vm0i2vca1#

stats是一个关系,默认情况下不加载关系;您应该在控制器中更改查询以加载关系。使用此选项:

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

编辑:根据注解,您应该告诉laravel要为关系使用哪些列。而且这种关系的定义是相反的。试试这个:
也要改变你的关系:

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');
    }     
}
qv7cva1a

qv7cva1a2#

首先,我认为你应该把你的模型的名字改为大写。我的意思是把等级玩家改成等级玩家,把等级统计改成等级统计。
第二,尝试在laravel中添加关系时使用名称空间。尝试改变。

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

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

同时更改

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

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

相关问题