0

Is is possible to modify a request before it gets inserted into the database?

  public function store(StoreRequest $request)
        {
           $request->date_posted = strtotime($request->date_posted);
           //insert data here.
        }
LeBlaireau
  • 17,133
  • 33
  • 112
  • 192
  • 1
    Requests have a `merge` function to add/modify data (and several other functions), but a raw request doesn't typically *get* inserted directly. Typically there's going to be a model of some sort that gets populated with the request's data via `fill`, so just after the `fill` call you'd update the model before saving. – ceejayoz May 05 '17 at 15:47
  • 1
    what you have above works perfectly fine. so yes it can be – ATechGuy May 05 '17 at 16:15

2 Answers2

0

Try this solution maybe can resolve your issue .
Add setter in model so it will be look like this

Class ModelX {
      public function setDateDatePosted Attribute($date_posted)
        {
            $this->attributes['date_posted'] = strtotime($date_posted);
        }
}

hope it useful for you .

A.khalifa
  • 2,366
  • 3
  • 27
  • 40
0

Yes it can be, what you have works perfectly fine

public function store(StoreRequest $request)
    {
       $request->date_posted = strtotime($request->date_posted);
       or
       $datePosted = $request->date_posted + 2;
       $datePosted = $request->date_posted . 'some other addons';
       //insert data here.
    }

these are just examples but i hope you get what im saying.

Fyi if you input into the db, the created_at and updated_at will update and so you don't need to insert the time the post was made manually.

ATechGuy
  • 1,240
  • 8
  • 13