jeudi 20 juillet 2017

Laravel - Multiple models in one controller

I have the following database structure:

Enquiries

id

total_widgets

total_amount

customer_id

Customers

id,

first_name,

last_name

Using the form when you are creating an Enquiry you can enter the Customers details into the section and this will store using firstOrCreate and then get the id in order to link to Enquiry to the Customer

Now, the issue is that this is all done inside the store method within the Customers controller, like the following:

public function store(Request $request)
{
     $rules = array(
         "first_name" => "required", 
         "last_name" => "required", 
         "total_widgets" => "required"
     );

     // Handle validation 

     // Create customer
     $customer = \App\Customers::firstOrCreate(['first_name' => $request-
                 >get('first_name')]); 

     $enquiry = new \App\Enquiries(); 
     $enquiry->customer_id = $customer->id; 
     $enquiry->save(); 

}

The issue with doing it like this is that it's not separated out and that if the process of creating a customer changes, then I would need to change this a lot of times. (Everytime I have a section which requires a customer)..

Is there a better way to do this? For example, should the customer be created separately and then the id is passed into the $request?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2gNDFr4
via IFTTT

Aucun commentaire:

Enregistrer un commentaire