3

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.

6
  • so you want to save multiple products with the same category id? Commented Aug 8, 2017 at 6:10
  • @madalinivascu yes. but the category id does not exist currently in the database. it's a new category which includes some new products Commented Aug 8, 2017 at 6:12
  • so first create the category then create the new products Commented Aug 8, 2017 at 6:14
  • @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 valid Commented Aug 8, 2017 at 6:15
  • then store the products in a temporary category then create the valid category, after that you update /remove the products Commented Aug 8, 2017 at 6:18

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

does it also works for a new category which is not stored in the database? i want to validate and create a new category with some new products
thats a good idea but did not fit to my problem. for createMany() i need an existing Category. but this Category does not exist currently
0

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



}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.