Initiate Relation widget on create page with items?

Hi,

I’m searching a way to render items in a Relation widget rendered on my parent create page,

i have two different model with a relation,

  1. Template
    hasMany TemplateProducts
  2. Booking
    hasMany BookingProducts

On my Booking index page, I can choose to create a Booking based on a Template Model, so that when I get to the create page, the BookingProducts relationship will already be pre-filled with TemplateProducts.

I have tried to do something like this with the formExtendModel function, but the relation widget is always empty. Someone could help me to understand where i’m wrong please ?

    public function formExtendModel($model)
    {

        if (Session::has('templateId') && !$model->exists) {
            $templateId= Session::get('templateId');
    
            $template= Template::find($templateId);
    
            foreach ($template->products as $product) {
                $BookingProduct = new BookingProducts;
                $BookingProduct->product_id = $product->id;
                $BookingProduct->declinaison_id = $product->declinaison_id;
                $BookingProduct->dossier_id = null;
                $BookingProduct->quantity = $product->quantity;
                $BookingProduct->price_vente_ht = $product->price_vente_ht;
                $BookingProduct->categorie_id = $product->categorie_id;
                $BookingProduct->piece_id = $product->piece_id;

                $BookingProduct ->save();
                $model->products()->add($BookingProduct);
            }

            Session::forget('packId');
        }
    
        return $model;
    }

best,
Lucas

I’ve find a way, it was not formExtendModel, but relationExtendManageWidget function

sometimes you have to think a little more :slight_smile:

2 Likes