简体   繁体   中英

Class not found with 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'));
    }
}

错误是因为GovermentRole文件在指定的路径上不可用,即

App\Database\Website\Roleplay\GovermentRole

Make sure the class is in right folder. Then run this command:

composer dumpauto

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM