casts property is not working to change date format - laravel

I am trying to change date format while fetching data in this way
protected $casts = [
'due_date' => 'date:d-m-Y',
];
Blade
{{$ticket->due_date}}
It is showing is like
2022-03-13

Casts are for json serialization (json_encode($ticket)). To get the format you want in blade, use Carbon's format method.
{{ $ticket->due_date->format('d-m-Y') }}

Related

Reading a raw array stored in mysql with blade

I have some data i have stored in mysql and it looks like this
["1109255736625955df9fbef.jpg","707361535625955dfa0564.jpg","1126563303625955dfa0c5c.jpg"]
i stored this data as an array in a field called product_images that holds strings.
Is there a way of iterating through this data without making it a php array at first but instead iterate it in blade straight from the database?.
This doesn't work
<div class="row">
#foreach ($product->product_images as $image)
<div class="col-3">
{{$image}}
</div>
#endforeach
</div>
you should use mutators to cast array
Product in model add:
protected $casts = [
'product_images' => 'array',
];
The cast from string to array is done under the hood for you
Your data is stored in database as string, so you should transform it to array. Convenient way to do that is creating Cast in your model. Thanks this you transform data from this column to array on fetch and transforming to json when stored.
protected array $casts = [
'product_images' => 'array'
]

How to save the json array data in database in laravel?

does anyone know how to store the data in this format ["123"] in PHPMyAdmin?
Now the data I store into the database is only the value which is 123. I want to store the data like this ["123"]. Does anyone know how to do this?
Here is my code:
Company::create([
'companyID' => $people->people_id,
]);
To store data the way you want to, you need to change the datatype for the column of the table to json. It can be done in your migration file like:
$table->json('column_name');
After migrating your table you have to add casting for the specific column in your model file. It can be done as:
protected $casts = [
'column_name' => 'array',
];
Once you have done this, you can then store data as json in your database.
You can find more information about casting at Laravel Docs here.
https://laravel.com/docs/8.x/eloquent-mutators#array-and-json-casting
You can save text data in mysql table column. For this you should change your column type to text and follow the following code as reference.
You will have to change your array to json data and pass variable to create method as value. Remember that, for storing string value you will have to wrap value with quotations.
$data = json_encode([
123, "124", 134.4, "450"
]);
Company::create([
'companyID' => $data
]);

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',
];

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.

Datetime on blade laravel

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'
];

Resources