I'm trying to get a polymorphic relationship working on laravel but every test I make returns either null or ax exception on laravel's pretended SQL query...
FYI: My laravel version is 5.8
running on a MySQLserver 5.7
(WAMP package)
According to what I read on laravel's docs, I think I must use morphTo()
in the polymorphic model's class and morphMany()
on it's children tables, as below:
My database is:
companies migration
Schema::create($this->tableName, function (Blueprint $table) {
$table->bigIncrements('id');
$table->softDeletes();
...
bank_accounts migration
Schema::create('bank_accounts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('bank_id')->unsigned()->nullable();
$table->bigInteger('currency_id')->unsigned();
$table->string('name', 50);
$table->index(["bank_id"], 'fk_credit_cards_banks1_idx');
$table->index(["currency_id"], 'fk_credit_cards_currencies1_idx');
$table->foreign('bank_id', 'fk_credit_cards_banks1_idx')
->references('id')->on('banks')
->onDelete('no action')
->onUpdate('no action');
$table->foreign('currency_id', 'fk_credit_cards_currencies1_idx')
->references('id')->on('currencies')
->onDelete('no action')
->onUpdate('no action');
$table->timestamps();
$table->softDeletes();
});
company_has_accounts migration
Schema::create('company_has_accounts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('company_id')->unsigned();
$table->morphs('accountable');
$table->timestamps();
$table->softDeletes();
$table->index(["company_id"], 'fk_company_has_accounts_accounts1_idx');
$table->foreign('company_id', 'fk_company_has_accounts_accounts1_idx')
->references('id')->on('currencies')
->onDelete('no action')
->onUpdate('no action');
});
and the models:
App\Models\Company
public function accounts(){
return $this->morphMany(Accountable::class, 'accountable');
}
App\Models\Account\Accountable
public function company()
{
return $this->belongsTo(Company::class);
}
public function accountable()
{
return $this->morphTo();
}
Errors:
when I try to debug:
BankAccount::find(1)->company
I keep getting:
Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'company_has_accounts.company_type' in 'where clause' (SQL: select * from `company_has_accounts` where `company_has_accounts`.`c
ompany_id` = 1 and `company_has_accounts`.`company_id` is not null and `company_has_accounts`.`company_type` = Financeiro/Models/Account/BankAccount and `company_has_accounts`.`deleted_at` is null)'
and when I try to debug:
Company::find(1)->accounts
I keep getting:
Illuminate\Database\Eloquent\Collection {#3324
all: [],
}
My objectives are:
App\Models\Account\BankAccount
public function company(){
//To return a App\Models\Company Object
}
App\Models\Account\Company
public function accounts(){
//To return a collection of mixed "App\Models\Account\BankAccount" and "App\Models\Account\CreditCard" Objects, all belonging to the current App\Models\Account\Company Model, as filtered by the "company_id" column on "company_has_accounts" table
}
I already ran all migrations and there's data in all the tables ('companies'
, 'bank_accounts'
and 'company_has_accounts'
. I have already double checked and the 'accountable_type' column got the string 'App\Models\Account\BankAccount'
in all rows). My intention is to get BankAccounts
to work so I can make more children to the 'Accounts'
namespace, like 'CreditCard'
or 'Cash'
.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire