我在处理关系时遇到了问题,出现了以下错误:
Call to undefined method Illuminate\Database\Query\Builder::ftpAccounts()
以下是我的模型:
客户模型
class Customer extends Model
{
protected $table = 'customers';
protected $fillable = [
'name',
'email',
'phone',
'status',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function ftpAccounts()
{
return $this->hasMany(FTPAccounts::class);
}
}
FTP账户模型
class FTPAccounts extends Model
{
protected $table = 'ftp-accounts';
protected $fillable = [
'host',
'user',
'password',
'status',
];
public function customer()
{
return $this->belongsTo(Customer::class);
}
}
我的控制器:
class AdminFTPAccountsController extends AdminBaseController
{
private $repository;
public function __construct(FTPAccountsRepository $repository)
{
parent::__construct();
$this->repository = $repository;
}
public function index()
{
return view('admin.pages.admin.clientes.ftp.index', [
'customers' => $this->repository->allFTPAccounts(),
'title' => 'TESTE'
]);
}
public function getCreate()
{
return view('admin.pages.admin.clientes.ftp.create', [
'title' => 'Nova conta FTP'
]);
}
public function postCreate(CustomerFTPAccountCreateRequest $request)
{
$request->user()->customers()->ftpAccounts()->create([
'host' => $request->host,
'user' => $request->user,
'password' => $request->password,
]);
return redirect('admin/clientes');
}
}
这是什么?我做错了什么吗?
## 编辑 01 ##
用户模型:
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'phone', 'password', 'status'];
protected $hidden = ['password', 'remember_token'];
// protected $casts = ['status' => 'boolean'];
public function articles()
{
return $this->hasMany(Article::class);
}
public function customers()
{
return $this->hasMany(Customer::class);
}
}
User
模型没有名为ftpAccounts()
的方法。 - Jilson Thomas