gpt4 book ai didi

php - 找不到 Laravel 5 模型类 - hasMany

转载 作者:可可西里 更新时间:2023-11-01 07:17:43 26 4
gpt4 key购买 nike

我正在尝试让公司员工在模型中使用 has many 方法。但是我得到了这个错误。

Fatal error: Class 'Employee' not found (View: /home/vagrant/Code/laravel/resources/views/frontpage.blade.php)

这是我的 Controller :

namespace App\Http\Controllers;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Company;
use App\Employee;

public function index()
{
$data = Company::get();
$data = array(
'companies' => $data,
);


return view('frontpage', $data);
}

这是我的模型:第一个是 Company.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Employee;

class Company extends Model{

protected $table = 'company';

public function employee()
{
return $this->hasMany('Employee', 'company_id', 'id');
}
}

这是另一个模型,Employee.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model{

public $table = "employee";

}

这是 View

@extends('layouts.master')

@section('content')

<div>
@foreach ($companies as $company)
<p>This is company {{ $company->name }}</p>
<p>{{ print_r($company->employee) }}</p>
@endforeach
</div>

@stop

最佳答案

关系定义需要传递完全限定的类名。

替换

return $this->hasMany('Employee', 'company_id', 'id');

无论是

return $this->hasMany('App\Employee', 'company_id', 'id');

return $this->hasMany(Employee::class, 'company_id', 'id');

关于php - 找不到 Laravel 5 模型类 - hasMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37741796/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com