程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Laravel 找不到类?

发布于2023-12-27 22:24     阅读(776)     评论(0)     点赞(9)     收藏(4)


今天我试图修复以下错误。它出于某种奇怪的原因告诉我,我在 laravel 中的关系类不存在?我不知道为什么,因为代码对我来说看起来非常好。

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

发生地点:

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

完整代码:

@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

这是我的 Roleplay Stat 类,$governmentMember 是它的一个实例:

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

这是我的 GovernmentRole 类:

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

这是刀片页面的控制器:

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

解决方案


很抱歉告诉您这一点,但您的关系方法中存在“输入错误”

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

您正在查找“GovermentRole”,而类名称为“GovernmentRole”。注意“r”后面额外的“n”字符



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.phpheidong.com/blog/article/550919/368526a6109f2a158699/

来源:php黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

9 0
收藏该文
已收藏

评论内容:(最多支持255个字符)