Hola
Hola

Reputation: 2233

How to take time as an input in Laravel?

I want to take input time with am/pm using my laravel controller to mysql. But the problem is I have taken the time from blade view but while seeing the output using dd() it's showing following value.

Carbon {#213 ▼
  +"date": "2016-05-09 00:00:00.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}

Means nothing save as start value.

Here is the Model:

class Room extends Model
{
    protected $table='allocate_rooms';             
}

Here is the view I've used:

<div class="form-group">
    <label>From</label>
    <input type="time" name="start" class="form-control" required >
</div>

Here is the controller I'm using.

public function AllocateRoom(Request $request)
{
    $room = new Room();  
    $room->start = Carbon::parse($request->input('start'));
    dd($room->start);
    $room->save(); 
}

In my database I have used time as datatype for start

Upvotes: 1

Views: 6482

Answers (2)

muhammad nabeel
muhammad nabeel

Reputation: 1

$room->start = Carbon::parse($request->input('start'))->format('h:i a');

Upvotes: 0

Daan
Daan

Reputation: 12246

In your model you need the date mutator:

class Room extends Model
{
     protected $table='allocate_rooms';             
     protected $dates = ['start'];
}

More information:

https://laravel.com/docs/5.1/eloquent-mutators#date-mutators

Upvotes: 2

Related Questions