我使用名为infyom lab的生成器与laravel合作一个项目(这对我来说是新的)。我对这个生成器和一个错误有了新的体验。它说“试图获取非对象的属性'name‘”。我习惯于在没有生成器的情况下手动完成所有这些工作,并且做得很好。希望能得到一些帮助
UserController.php
public function index(Request $request) {
$users = $this->userRepository->all();
return view('users.index')
->with('users', $users);
}UserRepository
<?php
namespace App\Repositories;
use App\Models\User;
use App\Repositories\BaseRepository;
/**
* Class UserRepository
* @package App\Repositories
* @version August 7, 2019, 5:11 pm UTC
*/
class UserRepository extends BaseRepository
{
/**
* @var array
*/
protected $fieldSearchable = [
'name',
'email',
'telephone',
'username',
'password',
'company_id'
];
/**
* Return searchable fields
*
* @return array
*/
public function getFieldsSearchable()
{
return $this->fieldSearchable;
}
/**
* Configure the Model
**/
public function model()
{
return User::class;
}
}模型用户
public function company(){
return $this->belongsTo('App\Company');
}模范公司
public function users(){
return $this->hasMany('App\User');
}查看用户
<td>{{ $user->company->name }}</td>发布于 2019-08-09 10:06:38
这个问题的解决方案是在模型文件夹app\Models内的Model中编写关系hasMany和belongsTo,而不是在app\上。
https://stackoverflow.com/questions/57421918
复制相似问题