php - Error creating children relationship in Laravel 4 Eloquent ORM -
I have found a structure with 2 models like this:
spreads class contenuti eloquent Has been {public event dettaglio () {return $ this-> Hone ('ContenutiDettaglio', 'contenuto_id'); }} Class ContenutiDettaglio Expands the legendary {public celebration contenuto ()) {Return $ this- & gt; BelongTo ('Contenuti', 'contenuto_id', 'Id'); }}
When I am creating a containant, I am trying to extend the content of the container on the content element to always add content.
I have written my expanse like this:
Public Fixed Work findOrNew ($ id, $ coloumns = array ()) {$ obj = parent :: findOrNew $ Id); If (! Isset ($ obj-> Ditaglio (- -> ID)) {$ obj- & gt; Save (); $ Dettaglio = ContenutiDettaglio :: Creating (array ('titolo' => 'Nuovo contenuto')); $ Dettaglio-> Contenuto () - & gt; Associate ($ obj); $ Obj = Contenuti :: findOrFail ($ obj-> ID); Dd ($ obj-> dettaglio ()); } Return $ obj; }
The parent ID is correctly in the database in ContenutiDettaglio but when I empty $ obj-> dettaglio () am I doing something wrong?
Edit:
These are my migration:
#Contenuti public function) {schema :: Create ( 'Container', Function ($ t) {$ t-> Engine = 'MySam'; // Vectia Tablet is called "Container" $ t-> Increment ('ID'); $ T- & Gt; Large Integer (' User Id ') - & gt; Tapable (); $ t- & gt; Boolean (' Pubblica '); // Wikiquia tablele "Contain_IT" $ t-> Timestamp (' Publica_del ') -> Faucet ); $ T- & gt; Timestamp ('pubblica_al') - & gt; Nullable (); $ T- & gt; Timestamp (); $ T- & gt; Index ('pubblica_dal'); $ T- & gt; Index ('pubblica_al'); $ T- & gt; Index ('created_at'); $ T- & gt; Index ('updated_at');}); } #contentDataGalioPublic Function Up () {Schema :: Create ('ContainTi_DataGlio', Function (BluePrint $ Table) {$ table-> Engine = 'MySass'; $ Table- & gt; Increment ('ID'); $ Table - & gt; integer ('contenuto_id'); $ table- & gt; string ('keyword', 255); $ table- & gt; string ('titolo', 255); $ table- & gt; text ('Sottotitolo'); $ table-> text ('occhiello'); $ table- & gt; text ('reassoon'); $ table- & gt; text ('descrizione'); $ table- & gt; ; Index ('contenuto_id'); $ table - & gt; index ('titolo');}); }
Where contenuto_id is the key between contenuti_dettaglio 2 tables.
First of all, here is the documentation on relationships in the lecture:
, The basic syntax for Hane is:
return $ this-> Hone ('classname', 'foreign_key', 'local_key');
Where, by default, foreign_key
and local_key
are equal to 'id' foreign_key
relationships Is the primary key.
For related it is:
return $ this-> Retail $ (- 'classname' 'local_key', 'foreign_key');
Where, by default, local_key
and foreign_key
again is equal to 'id' define child's key in Contenuti model I do not know your exact DB schema with hasOne
method of foreign_key , but I think you can fix it by changing it:
< Code> return $ this- & gt; Related $ ('Contenuti', 'contenuto_id', 'id');
with
return $ this-> Retail ('Contenuti', 'id', 'contenuto_id');
Let me know if it works, and if not, then add your migration files to those tables.
Comments
Post a Comment