duanmaduan1848 2017-01-04 06:56
浏览 40
已采纳

没有找到laravel的课程?

today I am trying to fix the following error. It is telling me for some weird reason that the class for my relationship in laravel does not exist? I am not sure why as the code looks perfectly fine to me.

Class 'App\Database\Website\Roleplay\GovermentRole' not found

Where it is happening:

{{ $governmentMember->government_role->government_title }}

Full code:

@if ($higherGovernment->count() > 0)
    @foreach ($higherGovernment as $governmentMember)
    <div class="col-md-10">
        <div class="col-md-12" style="margin-left:-40px;">
            <div class="col-md-1" style="margin-top:-16px;"><img src="http://mywebsite.com/images/get_character_look.php?look={{ $governmentMember->user->look }}&size=b&direction=3&head_direction=3"></div>
            <div class="col-md-9" style="margin-left:40px;">
                <h4>{{ $governmentMember->government_role->government_title }}<small>The Crown</small></h4>
                <p><font color="#aaa">Department here</font></p><br>
            </div>
        </div>
    </div>
    @endforeach
@else
    There are currently no candigates working in this category.
@endif

Here is my Roleplay Stat class, which $governmentMember is an instance of:

<?php
namespace App\Database\Website\User;

use Eloquent;

class Roleplay extends Eloquent
{
    protected $primaryKey   = 'id';
    protected $table        = 'srp_user_statistics';
    public $timestamps      = false;
    protected $fillable     = [];

    public function user()
    {
        return $this->belongsTo('App\Database\Website\User\Player', 'user_id', 'id');
    }

    public function government_role()
    {
        return $this->belongsTo('App\Database\Website\Roleplay\GovermentRole', 'government_id');
    }
}

Here is my GovernmentRole class:

<?php

namespace App\Database\Website\Roleplay;

use Eloquent;

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

    public function stats(){
       return $this->hasMany('App\Database\Website\User\Roleplay', 'government_id');
   }
}

Here is the controller for the blade page:

<?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()
    {
        $royalty = Cache::remember('government.royalty', 1, function() {
            return GovernmentRole::where('government_type', 'royalty')->first()->stats;
        });

        $higherGovernment = Cache::remember('government.higher_government', 1, function() {
            return GovernmentRole::where('government_type', 'higher_government')->first()->stats;
        });

        $seniorGovernment = Cache::remember('government.senior_government', 1, function() {
            return GovernmentRole::where('government_type', 'senior_ministers')->first()->stats;
        });

        $juniorGovernment = Cache::remember('government.junior_government', 1, function() {
            return GovernmentRole::where('government_type', 'junior_ministers')->first()->stats;
        });

        return view('frontend.community.government', compact('juniorGovernment', 'seniorGovernment', 'higherGovernment', 'royalty'));
    }
}
  • 写回答

3条回答 默认 最新

  • douzhu6149 2017-01-04 07:28
    关注

    Sorry to tell you this, but there is a 'typing error' in your relationship method

    public function government_role()        
    {
      return $this->belongsTo('App\Database\Website\Roleplay\GovermentRole', 'government_id');
    }
    

    You are looking for 'GovermentRole' while the class' name is 'GovernmentRole'. Notice the extra 'n' character after 'r'

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题