Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fluentd's avatar

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.

0 likes
11 replies
fluentd's avatar

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

mstnorris's avatar

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:

  1. dd($request->all())
  2. Your full Controller method
fluentd's avatar
$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.

mstnorris's avatar

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);
fluentd's avatar

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.

mstnorris's avatar
$request->session()->save($data);

doesn't make sense.

fluentd's avatar

Im not exactly sure how to go about saving the information. I even had tried

Session()->save($data);

Still didn't save.

mstnorris's avatar
Level 55

@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();

fluentd's avatar

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?

fluentd's avatar

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?

1 like
mstnorris's avatar

Yes. You might consider extracting that to a method but besides that, it looks good to me.

Please or to participate in this conversation.