我正在编码一个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
我到底想在这里实现什么?我正在尝试获取链接到srp_government_roles记录的所有roleplay_统计实例,其中government_type是一个特定值,例如,我正在尝试编写一个government页面,它为每个government类型都有一个面板框,在该面板框中,它显示每个具有该government类型的用户的信息。

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

但我想让球员们有合适的类型,而不仅仅是得到上面代码中显示的所有角色扮演统计数据。

最佳答案

我可能是错的,因为我正在研究流明,这是有点不同,但首先-为什么你延长雄辩而不是模型?(例如,使用Illuminate\Database\Eloquent\Model;类角色扮演扩展模型)。我还看到了一个问题,如果我使用关系而没有显式地将其添加到模型中。所以在你的情况下:

use Illuminate\Database\Eloquent\Model;

class GovernmentRole extends Model
{
    protected $primaryKey   = 'id';
    protected $table        = 'srp_government_roles';
    public $timestamps      = false;
    protected $fillable     = [];

    protected $with = [ 'stats' ];

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

您还可以在查询中添加关系“with”:
$government_type = GovernmentRole::where('government_type', 'higher_government')->with( 'stats' )->first();

如我所说,这在流明中有效,但我认为它也应该在拉瓦维尔中有效

09-30 23:56