lundi 21 septembre 2015

Can I require a one-to-one relationship in Laravel Eloquent?

I've got a Product class and a Detail class. I wanted to separate the big, long description from the basic data (sku, price) just for speed. However there will always be a big long description. So each item in the store will always have two records.

Is there a way to force or require both records? Is there a way to require that the 1-to-1 relationship exists - so that this would always work...

$P = new App\Product(['sku'=>'123','price'=>69]);
$P->Detail->html = '<p>Lorem ipsum.</p>';
$P->push();

My hasOne/belongsTo stuff is all fine. But if the details record does not exist yet ~ I get errors. I'm wondering if Detail can auto-magically exist.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire