rob
rob

Reputation: 2296

Laravel add relation objects to new eloquent model

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.

Upvotes: 3

Views: 11999

Answers (2)

rob
rob

Reputation: 2296

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;
    }



}

Upvotes: 0

Ken
Ken

Reputation: 2744

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

Upvotes: 1

Related Questions