我正在编码一个RP cms,我试图做以下事情。因此,在我的MySQL数据库中,有两个表对这个问题很重要。
srp_user_statistics -保存每个用户的角色扮演数据,它有一个user_id作为链接到users表的主键。此表还具有一个government_id int列,用于链接到government_roles中的id
srp_government_roles -保存用户可以拥有的政府角色的列表。它具有以下列:http://image.prntscr.com/image/335f9fc5f1174236a1a7bed6eb8d7d7e.png
那么我到底想要达到什么目的呢?我正在尝试获取链接到government_type为特定值的srp_government_roles记录的所有roleplay_statistic实例,例如,我正在尝试编写一个政府页面,它有一个对应于每个政府类型的面板框,在该面板框中,它显示了具有该政府类型的每个用户的信息。
<?php
namespace App\Database\Website\Roleplay;
use Eloquent;
use App\Database\Website\Roleplay;
class GovernmentRole extends Eloquent
{
protected $primaryKey = 'id';
protected $table = 'srp_government_roles';
public $timestamps = false;
protected $fillable = [];
public function stats(){
return $this->hasMany(Roleplay::class, 'government_id');
}
}角色扮演类:
use Eloquent;
class Roleplay extends Eloquent
{
protected $primaryKey = 'id';
protected $table = 'srp_user_statistics';
public $timestamps = false;
protected $fillable = ['user_id'];
public function user()
{
return $this->belongsTo('App\Database\Website\User\Player', 'user_id', 'id');
}
public function government_type()
{
return $this->belongsTo(GovernmentRole::class, 'government_id');
}
}这是我的政府页面的控制器:
<?php
namespace App\Http\Controllers\Frontend\User;
use Auth;
use Cache;
use Illuminate\Http\Request;
use App\Database\Website\User\Roleplay;
use App\Database\Website\Roleplay\GovernmentRole;
use App\Database\Website\Roleplay\Life\LifeEvents;
class GovernmentController
{
public function getView()
{
$government_type = GovernmentRole::where('government_type', 'higher_government')->first();
$stats = $government_type->stats;
foreach ($stats as $stat) {
echo $stat->user_id . '<br>';
}
exit();
return view('frontend.community.government');
}
}我添加的这段代码只是为了测试:
$government_type = GovernmentRole::where('government_type', 'higher_government')->first();
$stats = $government_type->stats;
foreach ($stats as $stat) {
echo $stat->user_id . '<br>';
}
exit();但是对于上面的一小段代码,我遇到了这个问题:
ErrorException in GovernmentController.php line 17:
Trying to get property of non-object当我解决了这个问题后,我想要实现如下所示:
namespace App\Http\Controllers\Frontend\User;
use Auth;
use Cache;
use Illuminate\Http\Request;
use App\Database\Website\Roleplay\Life\LifeEvents;
class GovernmentController
{
public function getView()
{
$higherGovernment = Cache::remember('government.higher_government', function() {
return Roleplay::get();
});
$seniorGovernment = Cache::Remeber('government.senior_government', function() {
return Roleplay::get();
});
$juniorGovernment = Cache::remember('government.junior_government', function () {
return Roleplay::get();
});
return view('frontend.community.government', compact('higherGovernment', 'seniorGovernment', 'juniorGovernment'));
}
}但是我想让玩家选择合适的类型,而不是像上面的代码那样获得所有的角色扮演统计数据。
https://stackoverflow.com/questions/41452499
复制相似问题