3
votes

I wanna use a form partial for a new and for an existing object.

I found similar questions:

but I can't and don't wanna save the parent object.

I came from the rails view with active record. I can do the following:

Let's say I have a category which includes many products:

category = Category.new
category.products << Product.new

Now I can iterate through the products like

category.products.each do ...

Now I want the same in laravel with an eloquent model

$category = new Category();
$category->products()->....

add does not exist for the Builder. save needs a stored category attach needs the same

is there a way to get my idea working? My goal is to use the same form partial for edit and creating a model with defined relations.

2
so you want to save multiple products with the same category id?madalinivascu
@madalinivascu yes. but the category id does not exist currently in the database. it's a new category which includes some new productsrob
so first create the category then create the new productsmadalinivascu
@madalinivascu i can't. it's possible thats the category is not valid. and also it could be there is a product in the collection which is not validrob
then store the products in a temporary category then create the valid category, after that you update /remove the productsmadalinivascu

2 Answers

1
votes

you can create many productions using $category->products()->createMany([]) Create Many

After you have a Category with many Product you can loop over them using

for ($category->products as $product) {
   // do something
}

or

$category->products->each(function ($product) {
    // do something
});

note the lack of () after products this will return a Collection

0
votes

The answer from Ken, does not fit for my special situation. So i have to create a service object which takes care of all the dependent cases. This service object stores the parent (my category) and stores the children (my products for each category) for each parent. When all data are valid, it will be persist into the database. If not, so save() returns false and i get the exception message and the validation errors.

So my service object contains the following:

namespace App\Services;


use Illuminate\Support\Facades\Validator;

class FormObject
{
    protected $children = [];
    protected $parent, $validator;
    protected $errorMessages = [];


    public function save(){
        if(!$this->isValid()){
            return false;
        }
        try{
            DB::beginTransaction();

            // save parent and all relations....

            DB::commit();

        }catch(\Exception $e){
            DB::rollBack();
            $this->addErrorMessage($e->getMessage());
            return false;
        }

        return true;

    }

    public function isValid(){
        return $this->getValidator()->passes();
    }

    public function add($identifier, array $collection){
        $this->children[$identifier] = $collection;
    }

    public function addErrorMessage($message){
        array_push($this->errorMessages, $message);
    }

    public function setParent(Model $parent){
        $this->parent = $parent;
    }

    public function setValidator(Validator $validator){
        $this->validator = $validator;
    }

    public function get($identifier){
        return $this->children[$identifier];
    }

    public function getErrorMessages(){
        return $this->errorMessages;
    }

    public function getParent(){
        return $this->parent;
    }

    public function getValidator(){
        return $this->validator;
    }



}