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.

AAT
  • 411
  • 7
  • 16
rob
  • 2,136
  • 8
  • 29
  • 37

2 Answers2

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

Ken
  • 2,654
  • 3
  • 26
  • 29
  • 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 – rob Aug 08 '17 at 06:14
  • 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 – rob Aug 08 '17 at 08:47
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;
    }



}
rob
  • 2,136
  • 8
  • 29
  • 37