0

I have a User model and Event model.

A User can have/create many Events

An Event belongs to User.

I have made this form just for demo:

enter image description here

And i get this output when i dd($request);

enter image description here

How to save this in my database table events. These array fields are problem...do i need to make some new table except users and events??

3
  • What is the problem with the array? Commented Oct 28, 2016 at 22:44
  • How do you save an array in one line? Commented Oct 29, 2016 at 4:18
  • laracasts.com/series/laravel-5-fundamentals Commented Oct 29, 2016 at 10:29

1 Answer 1

2

Yes, it looks like you need another table. I've just taken an educated guess at the table names because I'm not 100% sure what these times and professions refer to, it looks like you already have a table storing the professions, but here are the relationships I can see:

  • A user can have many events

  • An event belongs to a user

  • An event has many timeslots

  • A timeslot belongs to an event

  • A profession has many timeslots

  • A timeslot has many professions

So your events and professions (don't worry if you've called it something else) table should look like:

events(id, user_id, name, start, end);

professions(id, profession,..);

And you need to add an extra table:

time_slots(id, event_id, profession_id, amount, from, to, hours)

Then set those relationships up inside your models.

EDIT

The basic way to make a store method is to use the relationships like so:

public function store(Request $request){
   // Get the request as an array
   $request = $request->all();
   // create a new event
   $event = Event::create($request);
   
   // Map timeslots to an array of Timeslot objects:
  $timeslots = array_map(function($personalId, $amount, $from, $to, $hours) {
      return new App\Timeslot($personalId, $amount, $from, $to, $hours);
  }, $request['personal_id'], $request['amount'], $request['from'], $request['to'], $request['hours']);

   // Save all timeslots for the event
   $event->timeslots()->saveMany($timeslots)
}

I haven't tried that code, but it should point you inb the right direction. You should take a look at laravel relationships to see how this works:

https://laravel.com/docs/5.3/eloquent-relationships#the-save-method

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

4 Comments

Hi craig, i tried to send you a message on twitter but i don't know if you got it....but anyway i need to see the store method in controller for this form...that is the thing i don't know how to make!
i need your help please if you are there?
I did all as you said....now the only thing that is missing is how to save the data from form into DB. The store method
You are incredible....if i had a company i would hire you right away!

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.