简体   繁体   中英

Laravel - One to many relationship

I'm having some problems with my relationship, being returned the following error:

Call to undefined method Illuminate\Database\Query\Builder::ftpAccounts()

Here is my models:

Customer Model

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 Accounts model

class FTPAccounts extends Model
{
    protected $table = 'ftp-accounts';

    protected $fillable = [
        'host',
        'user',
        'password',
        'status',
    ];

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }
}

My controller:

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');
    }
}

What can it be? Did i do something wrong?

## Edit 01 ##

User model:

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);
    }
}

Yes, you treating Request object like Model object here.

You need to create new object Customer object and use $request->get('user') just as variable in it.

I can't test your code, but you can try something like this:

$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();

You can do something like this:

$customers = $request->user()->customers;


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

}

This will give you a head start.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM