Reputation: 15047
Trying to make a mutator in Laravel for date but it doesn't work.
This is my example
public function getEventEndAttribute($date)
{
return Carbon::parse($date)->format('d.m.Y H:i')
}
For example in my User model i have this mutator that is working perfectly:
// convert email address to lowercase
public function getEmailAttribute($value) {
return strtolower($value);
}
So even if a user write an email with uppercase letters in view it will be converted to lowercase....
And i want the same for my date...to convert from MySQL DB format to my local format
This was copied from SO and still doesn't work. Please help.
Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\UserRequest;
use Carbon\Carbon;
use Auth;
use DB;
class UserRequestsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//dd(DB::table('user_requests')->get());
return view('user-requests.index', ['userRequests' => DB::table('user_requests')->get()]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('user-requests.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//dd($request->event_end);
$this->validate($request, [
'event_name' => 'required|max:255',
'event_start' => 'required|date',
'event_end' => 'required|date',
]);
$userRequest = new UserRequest();
$userRequest->user_id = Auth::user()->id;
$userRequest->event_name = $request->event_name;
$userRequest->event_start = $request->event_start;
$userRequest->event_end = $request->event_end;
$userRequest->save();
return redirect('requests');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
When i hit my store method i get this error:
Upvotes: 2
Views: 6313
Reputation: 325
You can try Carbon\Carbon::createFromFormat($format, $input). As for the parse method, you can use it to create a Carbon instance from a string.
Upvotes: 1
Reputation: 32704
You can actually have laravel automatically mutate your dates to a Carbon
object by adding the dates array to your model:
protected $dates = [
'created_at',
'updated_at',
'event_end'
];
You can then just format it inside your blade template:
{{ $myModel->event_end->format('d.m.Y H:i') }}
Or you can add the $dateFormat
property to automcatically convert all dates on this model to your chosen format:
protected $dateFormat = 'd.m.Y H:i';
See date mutators: https://laravel.com/docs/5.3/eloquent-mutators#date-mutators
Upvotes: 6