This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Asinox
Asinox

Hi there!, right now i have a doubt, there is few models, where just one model is the "only" parent, the rest are "child" representing type of "data/articles", the cicle of life is: i save (first) the parent model and them the child (just one child in one saving process), but the question is:

How i can save the parent model "just if the children model is valid and ready to be save? " , i mean, right now if my child model fail, and the parent model is valid, the parent model is saving, i want to prevent saving the parent model if child model is failling the validation.

In my page (PHP Section), my code is like this:


//Parent
$classified = new Classified;
$classified->plan_id = $plan->id;
$classified->categories()->associate($categoria);
$classified->subcategory_id = post('subcategory');
$classified->ad_action = post('action');
$classified->cust_contactmode = post('cust_contact_type');
$classified->save();
....

//Child
$realstate = new Realstate;
$realstate->rooms = post("rooms");
$realstate->bath = post("bath");
...
...
$realstate->classified()->associate($classified); // saving PARENT
$realstate->save();

Any idea? thank you

Asinox
Asinox

Well, i dont know if in laravell o October there is a way for this, but i did in this way:

if($realstate->validate() && $classified->validate() && $classified->save()){
                $realstate->classified()->associate($classified);
                if($realstate->save()){
                    \Flash::success('Success!.');
                    return Redirect::to("mi-plaza");
                }else{
                   \Flash::error('Validation error!' );
                }
            }
            else{
               \Flash::error('Validation error!' );
            }

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.