vendredi 4 mars 2016

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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire