Do you have the "_id" and "_type" fields in your BankAccount table?
owner_id - integer, owner_type - string
http://laravel.com/docs/4.2/eloquent#polymorphic-relations then scroll down a little to the "Polymorphic Relation Table Structure"
Hope that helps.
TerrePorter said:
Do you have the "_id" and "_type" fields in your BankAccount table?
owner_id - integer, owner_type - string
http://laravel.com/docs/4.2/eloquent#polymorphic-relations then scroll down a little to the "Polymorphic Relation Table Structure"
Hope that helps.
Yes, I have. When I insert data for the relationship, the data are normally registered.
darrylkuhn said:
DDL please (table structure)
When I use this, it works:
$bankAccounts = $teacher->bank_accounts()->get();
dd($bankAccounts); // this output my result..
but this, not:
$bankAccounts = $teacher->bank_accounts;
dd($bankAccounts); // this output NULL
That all looks good - so to confirm the record is written correctly in the database (the _type and _id records are properly set on the record in the DB right)?
I've not used namespaces in my relations... I wonder if the issue is that the builder which sits in its own namespace can't find the model (because the namespace is relative - no leading slash)? Try setting the morph many to:
$this->morphMany('\Inbec\Portal\Storage\Financial\BankAccount', 'owner');
The result of my controller action:
$teacherInstance = $this->teacherRepo->getById(10);
dd($teacherInstance->bank_accounts()->create(["bank_id" => 237, "agency" => "2999", "account_number" => "0729060-8", "account_type" => "Corrente Individual", "account_holder" => "Ian da Silva Rodrigues", "document_holder" => "054.367.083-07"]));
is:
object(Inbec\Portal\Storage\Financial\BankAccount)[216]
protected 'softDelete' => boolean true
protected 'fillable' =>
array (size=6)
0 => string 'bank_id' (length=7)
1 => string 'agency' (length=6)
2 => string 'account_number' (length=14)
3 => string 'account_type' (length=12)
4 => string 'account_holder' (length=14)
5 => string 'document_holder' (length=15)
public 'validationErrors' =>
object(Illuminate\Support\MessageBag)[217]
protected 'messages' =>
array (size=0)
empty
protected 'format' => string ':message' (length=8)
public 'throwOnValidation' => boolean false
public 'autoHydrateEntityFromInput' => boolean false
public 'forceEntityHydrationFromInput' => boolean false
public 'autoPurgeRedundantAttributes' => boolean false
protected 'purgeFilters' =>
array (size=0)
empty
protected 'purgeFiltersInitialized' => boolean false
public 'autoHashPasswordAttributes' => boolean false
protected 'connection' => null
protected 'table' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=11)
'bank_id' => int 237
'agency' => string '2999' (length=4)
'account_number' => string '0729060-8' (length=9)
'account_type' => string 'Corrente Individual' (length=19)
'account_holder' => string 'Ian da Silva Rodrigues' (length=22)
'document_holder' => string '054.367.083-07' (length=14)
'owner_id' => int 10
'owner_type' => string 'Inbec\Portal\Storage\Teacher\Teacher' (length=36)
'updated_at' => string '2015-01-19 18:43:14' (length=19)
'created_at' => string '2015-01-19 18:43:14' (length=19)
'id' => int 2
protected 'original' =>
array (size=11)
'bank_id' => int 237
'agency' => string '2999' (length=4)
'account_number' => string '0729060-8' (length=9)
'account_type' => string 'Corrente Individual' (length=19)
'account_holder' => string 'Ian da Silva Rodrigues' (length=22)
'document_holder' => string '054.367.083-07' (length=14)
'owner_id' => int 10
'owner_type' => string 'Inbec\Portal\Storage\Teacher\Teacher' (length=36)
'updated_at' => string '2015-01-19 18:43:14' (length=19)
'created_at' => string '2015-01-19 18:43:14' (length=19)
'id' => int 2
protected 'relations' =>
array (size=0)
empty
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'guarded' =>
array (size=1)
0 => string '*' (length=1)
protected 'dates' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
protected 'morphClass' => null
public 'exists' => boolean true
protected 'forceDeleting' => boolean false
The result of:
$teacherInstance = $this->teacherRepo->getById(10);
dd($teacherInstance->bank_accounts); // output NULL...
but,the result of:
$teacherInstance = $this->teacherRepo->getById(10);
dd($teacherInstance->bank_accounts()->get()); // output my object data...
what if you do:
$this->teacherRepo->with('bank_accounts')->getById(10);
Return this:
{"id":10,"titration_id":2,"name":"Rebecca Oberbrunner","lattes":"http:\/\/koepp.com\/","cpf":"89401580890","identity":"85","phone":"+77(6)2156651136","mobile_phone":null,"email":"davis.brian@braun.com","resume":"Sapiente rerum esse quod fugit nihil animi. Doloribus id quos dolores vel nemo iste. Et repudiandae tempora voluptates vel ipsa.","cep":"62672240","address":"7613 Marta Spur Apt. 950","state_id":6,"city_id":625,"deleted_at":null,"created_at":"2015-01-19 18:41:17","updated_at":"2015-01-19 18:41:17","bank_accounts":[{"id":1,"bank_id":237,"owner_id":10,"owner_type":"Inbec\\Portal\\Storage\\Teacher\\Teacher","agency":"2999","account_number":"0729060-8","account_type":"Corrente Individual","account_holder":"Ian da Silva Rodrigues","document_holder":"054.367.083-07","deleted_at":null,"created_at":"2015-01-19 18:41:29","updated_at":"2015-01-19 18:41:29"},{"id":2,"bank_id":237,"owner_id":10,"owner_type":"Inbec\\Portal\\Storage\\Teacher\\Teacher","agency":"2999","account_number":"0729060-8","account_type":"Corrente Individual","account_holder":"Ian da Silva Rodrigues","document_holder":"054.367.083-07","deleted_at":null,"created_at":"2015-01-19 18:43:14","updated_at":"2015-01-19 18:43:14"}]}
Okay, thank you. But I'm still confused because that way is not working.
$teacherInstance = $this->teacherRepo->getById(10);
dd($teacherInstance->bank_accounts); // output NULL...
Yeah - I agree - it feels like a bug... don't know the bowels of eloquent to give you a solid answer... FYI if you already have a model loaded up you can also call
$teacherInstance->load('bank_accounts');
which has the same effect.
It`s resolved! I just replace this:
public function bank_accounts()
{
return $this->morphMany('Inbec\Portal\Storage\Financial\BankAccount', 'owner');
}
by this:
public function bankaccounts()
{
return $this->morphMany('Inbec\Portal\Storage\Financial\BankAccount', 'owner');
}
The underscore was the problem! But..why??? :)
Ian, I'm running into a similar problem with getting a 'class not found' while using morphMany...and a model/class name that is CamelCase. It's a 'favorite' model that is morphed to a few models, all of them work perfectly but the model with CamelCase...can't find the class. I have a feeling somewhere in the relationship 'sugar' it's got to turn the '_type' field into a class name and is making wrong assumptions?
(Even more odd is that this works perfectly fine on my homestead VM..but when pushed to a forge provisioned server it can't find the class...related to the compiling?)
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.