Datetime on blade laravel - laravel-5

How to get month name on blade laravel ?
i tried this but the result is the month number
$training->date_activity->month

as per the documentation of Carbon, your approach is correct.
http://carbon.nesbot.com/docs/#api-getters
Yet it could be the property is not considered a Carbon DateTime.
Can you make sure that in the Training Model you have 'date_activity' as a date mutator?
protected $dates = [
'created_at',
'updated_at',
'date_activity'
];

Related

Is the "Casting" feature ($casts) valid only using Eloquent in Laravel [duplicate]

This question already has answers here:
How to change format of created_at and updated_at in laravel Model?
(2 answers)
Closed 12 months ago.
I try to use $casts to change date format in my Model :
protected $casts = [
'created_at' => 'datetime:Y-m-d H:i',
'updated_at' => 'datetime:Y-m-d H:i'
];
It works when I retrieve the data with Eloquent :
$clients = Client::all();
return $clients;
but it doesn't with Query Builder !
$clients = DB::table('clients')->get();
return $clients;
In query builder you are using DB::table it does not use Laravel Model and you are writing this casts code to your Laravel Model. It obviously never work. They are apples and oranges.
If you use casts code, Laravel only runs this code when you get the model and use this field. Before that it does not change the values.
What are you trying to achieve there? If you give us more detailed code I will help you.

Laravel model item set carbon item but returns string

I have added a datetime in the database schema:
$table->dateTime('send_at')->nullable();
I have set the attribute to a Carbon instance in the seeder:
$invoice->send_at = Carbon::now();
When I try to get the type of the attribute inside the controller it returns a string:
dd(gettype($data['invoices'][0]->send_at));
What is going on? And how can I be sure that it's a Carbon Object instead of a string?
On your model, you need to define $dates property to create Carbon instance automatically for the column :
protected $dates = ['send_at'];
On Since Laravel 8:
protected $casts = [
'send_at' => 'datetime',
];

Laravel Passport API Response

When I requested by Laravel Passport API then my datetime data has been changed like first Photo.
Second Photo is Laravel Model Casting.
Third one Photo is my table column.
How I get my original data without Laravel Mutator (Accessor)
I need always return my app timezone wise data with format Y-m-d H:i:s
Have any way to handle it easily?
Try Like below. It will solve your problem.
protected $casts = [
'email_verified_at' => 'datetime:Y-m-d H:i:s',
'mobile_verified_at' => 'datetime:Y-m-d H:i:s',
];

How does laravel/lumen dateformat 'U' actually works?

I'm not able get the timestamps with dateformat 'U' working in lumen.
In migration:
$table->timestamps();
In Model:
protected $dateFormat = 'U';
protected $dates = [
'created_at',
'updated_at',
'deleted_at'
];
public function getDateFormat()
{
return 'U';
}
Insert row from controller:
$model = new ApiKey;
$model->random= rand();
$model->name = $name;
$model->scope = $scope;
$model->save();
It does insert the row in the database but with 0000-00-00 00:00:00 values for created_at and updated_at columns.
Also, while retrieving a model via toArray or toJson it thrown exception:
I want lumen to autoupdate the timestamps and retrive timestamps as unixtimestamp format i.e. number of seconds from 1st Jan 1970.
Also, $table->timestamps() didn't create deleted_at column. What do I need to do get this column created via laravel.
Is there any other option than $table->timestamp('deleted_at');?
I've found a solution bay changing timestamps columns to int. But I want the things to be done in laravel way.
Unix timestamps are integers, not compatible with SQL datetime/timestamp fields. If you want to use unix timestamps, use integer field types for storage.
The timestamps() method only creates created_at and updated_at, soft deletes are not enabled by default. This method should not be used if you want integer storage.
If you only want to change the output format when serializing data, use casting:
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'created_at' => 'datetime:U',
];
This is likely caused by the fact that Laravel/Lumen created the date/time columns as the type timestamp not int so you're trying to save wrong type of data in the field, causing the 0000-00-00 00:00:00.
This would also cause the carbon issue as you are trying to createFromFormat with the wrong format compared to the content.
You can use $table->integer('deleted_at'); in your migration to create a deleted_at column.
TL;DR:
Manually create your date time columns with $table->integer('updated_at').
<?php
namespace App\Traits;
use Illuminate\Support\Carbon;
trait sqlServerDateFormat
{
public function fromDateTime($value)
{
return Carbon::parse(parent::fromDateTime($value))->format('d-m-Y H:i:s');
}
}

In laravel how do I fetch date as date instead of datetime format?

In my database some fields are set as date, while others are datetime or timestamp formats. However when fetching data, the "date" fields are also shown with the 00:00:00 time string attached at the end. How can I prevent this?
You can use date/datetime casting with an additional format parameter as described in the Eloquent: Mutators documentation.
class User extends Model
{
protected $casts = [
'created_at' => 'datetime:Y-m-d',
];
}
This will apply the given format when serializing the model to an array or json. If you want to use a different format in your blade templates, you can use the format() function on the datetime property you want to change:
{{ $user->created_at->format('Y-m-d') }} or {{ $user->created_at->toDateString() }}
You will have to add the created_at column to the $dates property of your model though:
protected $dates = [
'created_at',
];
This basically adds 'created_at' => 'datetime' to your $casts.

Resources