Updating data with converted date format For my update method I tried the following:
$data = $request->all();
$data['start'] = Carbon::createFromFormat('m/d/Y', $request->start)->format('Y-m-d');
$data['end'] = Carbon::createFromFormat('m/d/Y', $request->end)->format('Y-m-d');
// $session = Session::findOrFail($id);
// $session->update($request->all());
Session::update($data);
The commented out lines are what I use to have before you showed me how to change the format. This doesnt seem to be doing anything once I click the edit button.
I am trying to update the records with an edit method.
Its giving me this error.
ErrorException in SessionsController.php line 102:
Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically, assuming $this from incompatible context
In your update() method you should have an $id as a parameter
$session = Session::find($id);
$session->update($data);
If that doesn't help, can you show me the following:
dd($request->all())
Your full Controller method
$data = $request->all();
$data['start'] = Carbon::createFromFormat('m/d/Y', $request->start)->format('Y-m-d');
$data['end'] = Carbon::createFromFormat('m/d/Y', $request->end)->format('Y-m-d');
dd($request->all());
Dumps out
array:6 [▼
"_method" => "PATCH"
"_token" => "lDWqaK2aFDaaWgSsdeSv4RF9BZnxDm09DFgB6Pjp"
"name" => "Nick"
"description" => "Nack"
"start" => "05/28/2015"
"end" => "07/31/2015"
]
Which looks like its not formatting it.
Well that depends on where you put the dd(); command.
You did the following which is incorrect as you dd($request->all()); instead of dd($data);
$data = $request->all();
$data['start'] = Carbon::createFromFormat('m/d/Y', $request->start)->format('Y-m-d');
$data['end'] = Carbon::createFromFormat('m/d/Y', $request->end)->format('Y-m-d');
dd($request->all()); // this is the same unmodified data that you were passed, you need to use $data
Try
$data = $request->all();
$data['start'] = Carbon::createFromFormat('m/d/Y', $request->start)->format('Y-m-d');
$data['end'] = Carbon::createFromFormat('m/d/Y', $request->end)->format('Y-m-d');
dd($data);
This is where i'm at with my store method
$data = $request->all();
$data['start'] = Carbon::createFromFormat('m/d/Y', $request->start)->format('Y-m-d');
$data['end'] = Carbon::createFromFormat('m/d/Y', $request->end)->format('Y-m-d');
$request->session()->save($data);
It doesnt seem to be saving the data though.
$request->session()->save($data);
doesn't make sense.
Im not exactly sure how to go about saving the information. I even had tried
Session()->save($data);
Still didn't save.
@fluentd that isn't the correct way of doing it, that's why. You'd need to grab the Session
$session = Sesssion::find(1);, and then update it $session->propertyToUpdate($data); $session->save();
I'm still not seeing how to do this.
First I grab all the fields that were sent from the form
$data = $request->all();
Then I convert the data to the correct format
$data['start'] = Carbon::createFromFormat('m/d/Y', $request->start)->format('Y-m-d');
$data['end'] = Carbon::createFromFormat('m/d/Y', $request->end)->format('Y-m-d');
Now where do I go from here to update the data to the database?
Got it working! @mstnorris
$data = $request->all();
$data['start'] = Carbon::createFromFormat('m/d/Y', $request->start)->format('Y-m-d');
$data['end'] = Carbon::createFromFormat('m/d/Y', $request->end)->format('Y-m-d');
$session = Session::find($id);
$session->update($data);
$session->save();
Does this look like the correct way of doing it?
Yes. You might consider extracting that to a method but besides that, it looks good to me.
Please sign in or create an account to participate in this conversation.