【问题标题】:Class 'app\Models\Job' not found why ? What to do?找不到类 'app\Models\Job' 为什么?该怎么办?
【发布时间】:2021-06-18 19:10:43
【问题描述】:

我有这个不起作用的命令行,但我尝试了 namespace app\Models\Job;use app\Models\Job;namespace App\Models\Job;use App\Models\Job;

我也尝试过直接在命令行中添加App\Models\Job,但似乎不起作用。

namespace App\Http\Controllers;

use app\Models\Job;
use Illuminate\Http\Request;

class JobController extends Controller
{
    public function __construct(){
        $this->middleware(['employer','verified'],['except'=>array('index','show','apply','allJobs','searchJobs','category')]);
    }
    
    public function index(){
        $jobs = Job::latest()->limit(5)->where('status',1)->get();
        $categories = Category::with('jobs')->paginate(5);
        
        $companies = Company::get()->random(6);
       
        return view('welcome',compact('jobs','

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Job extends Model
{
    use HasFactory;

    protected $fillable = ['user_id','company_id','title','slug','description','roles','category_id','position','address','type','status','last_date','number_of_vacancy','experience','gender','salary'];
    
    public function getRouteKeyName(){
        return 'slug';
    }
    public function company(){
        return $this->belongsTo('App\Company');
    }

    public function users(){
        return $this->belongsToMany(User::class)->withTimeStamps();
    }

    public function checkApplication(){
        return \DB::table('job_user')->where('user_id',auth()->user()->id)->where('job_id',$this->id)->exists();
    }

    public function favorites(){
        return $this->belongsToMany(Job::class,'favourites','job_id','user_id')->withTimeStamps();
    }

    public function checkSaved(){
        return \DB::table('favourites')->where('user_id',auth()->user()->id)->where('job_id',$this->id)->exists();
    }
}

【问题讨论】:

  • 你能展示一下 Job 模型吗?
  • 命名空间不是 App\Models 它是命名空间 App\Http\Controllers;
  • 但我试过了。改变它。
  • Job::latest() 不起作用

标签: php laravel


【解决方案1】:

你应该这样做:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Job; // change app to App
class JobController extends Controller
{...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2017-06-07
    相关资源
    最近更新 更多