php - Laravel 关系 - 尝试获取非对象的属性

标签 php mysql laravel enums

我正在编写一个 RP cms,我正在尝试执行以下操作。 所以在我的 MySQL 数据库中,我有两个对这个问题很重要的表。

srp_user_statistics - 保存每个用户的角色扮演数据,它有一个 user_id 作为链接到用户表的主键。该表还有一个 government_id int 列链接到 government_roles 中的 id

srp_government_roles - 包含用户可以拥有的政府角色列表。它有以下列: http://image.prntscr.com/image/335f9fc5f1174236a1a7bed6eb8d7d7e.png

那么我到底想在这里实现什么?我正在尝试获取所有链接到 srp_government_roles 记录的 roleplay_statistic 实例,其中 government_type 是某个值,例如我正在尝试编写政府页面代码并且它有一个面板框用于每种政府类型,并且在该面板内框显示具有该政府类型的每个用户的信息。

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

这是我的政府页面的 Controller :

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

但我实际上希望获得正确类型的玩家,而不仅仅是获得上面代码中显示的所有角色扮演统计数据。

最佳答案

我可能是错的,因为我正在研究 Lumen,这有点不同,但首先 - 为什么要扩展 Eloquent 而不是 Model? (例如使用 Illuminate\Database\Eloquent\Model; class Roleplay extends 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();

正如我所说,这将在 Lumen 中工作,但我认为它也应该在 Laravel 中工作

关于php - Laravel 关系 - 尝试获取非对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41452499/

相关文章:

php - Mysql 执行两个查询

javascript - 如何清空数组?

mysql - 如果 WHERE 子句适用于多行,是否测试所有行?

mysql - 带 SUM 的 CASE 语句

laravel - vue-i18n 输出用作类星体数据表标题值

laravel - 无法修复 laravel 上的顺风组件下拉列表

javascript - 如何禁用循环内的文本框?

php - WordPress 帖子元赞查询无法识别第二个赞语句

php - 自定义 preg_replace 函数的任何更有效的方法?

php - Laravel 5.7 验证带星号的字段,required_if