vendredi 29 décembre 2017

Laravel: method to set the value of a custom primary key

My Document model has a custom primary key name, so I typed:

class Document extends Model {

    protected $primaryKey = 'fluxo_mensagem_id';

After this, I suppose Laravel to know my primary key name and then I wouldn't expected to have to type it manually every time I need to set its value. Something like:

$document = new Document;
$document->setPrimary($pivot->id);

Instead of:

$document = new Document;
$document->fluxo_mensagem_id = $pivot->id;

For more details, my partial ER diagram:

enter image description here

Question 1

Laravel provides some method to set primary key values dynamically after custom names defined on model class?

Question 2

On document table, the column fluxo_mensagem_id is a foreign key that references id fluxo_mensagem pivot (intermediate) table. There's some way to "associate" (as we can do with Belongs To relationships) Pivot object to a Document model?

For example:

  $pivot = $mensagem->fluxos()->first()->pivot;
  $document = new Document;
  $document->fluxoMensagem()->associate($pivot); 

This would fit the problem because his intention is to set the foreign key on the child model.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lp0T67
via IFTTT

Aucun commentaire:

Enregistrer un commentaire