Laravel - 一对多关系

3

我在处理关系时遇到了问题,出现了以下错误:

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
@JilsonThomas 我想将FTP账户与客户关联,而不是用户,我不知道如何做。 - Caio Kawasaki
2个回答

0

是的,你在这里把Request对象当作Model对象来处理。

你需要创建一个新的Customer对象,并将$request->get('user')仅仅作为变量使用。

我无法测试你的代码,但你可以尝试像这样做:

$customer = Customer::where('user', $request->get('user');

$ftpAccount = new FTPAccount;
$ftpAccount->host = $request->get('host');
$ftpAccount->user = $request->get('user');
$ftpAccount->password = $request->get('password');

$customer->ftpAccounts()->save();

看看更新后的代码。我无法测试它,但我认为你明白了思路。 - Alexey Mezenin
是的,但我说过我不能测试你的代码。看看代码,试着理解它是如何工作的,并与之互动。例如,我不知道在$request->user中获取了什么(id、名称或其他),我也不知道它应该是一个user还是user_id甚至是id。所以,只需尝试理解它是如何工作的,我相信你可以通过进行一些小修复来使我的代码正常运行。 - Alexey Mezenin
如果$requestRequest类的实例,那么$request->user()将返回当前已验证的用户实例。如果你想从表单请求中访问一个名为user的字段,那么可以使用$request->input('user')。我在我的代码中使用了$request->user(),它工作得非常完美。 :) - Jilson Thomas
$request->user() 总是返回认证用户,这是 Laravel 的默认设置。 - Caio Kawasaki
@AlexeyMezenin 调用未定义的方法 Illuminate\Database\Query\Builder::ftpAccounts() - Caio Kawasaki
显示剩余3条评论

0
你可以像这样做:
$customers = $request->user()->customers;


foreach($customers as $customer)
{
    $customer->ftpAccounts()->create([
            'host' => $request->host,
            'user' => $request->user,
            'password' => $request->password,
        ]);

}

这将为您提供一个良好的起点。


好的,你试过我给你的那个了吗? - Jilson Thomas
是的,什么都没有发生。 - Caio Kawasaki
那意味着您没有为该特定用户的任何客户。 - Jilson Thomas
我只想将 ftpAccount() 分配给 customer(),就像我将 customer() 分配给 user() 一样,不希望 user()ftpAccount() 有任何关联。 - Caio Kawasaki
当您获取与用户关联的所有客户时,您会得到null。这意味着该用户没有客户。因此,在null上调用方法是不可能的。 - Jilson Thomas
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接