Laravel Passport API Response - laravel

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

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.

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

How do I "json_decode" a single column data after fetch from database in Laravel?

I am saving an array in one column of my database using json_encode as follows and it works:
$service->description = $request->service_description;
$service->image = json_encode($url);
$service->duration = $request->service_delivery_time;
When I fetch the data I get a string. I am fetching using $service = Service::findOrFail($id);. I can decode the individual column as done below and pass it to the view.
$service = Service::findOrFail($id);
$images = json_decode($service->image);
return view('services.show',['service'=>$service , 'images'=>$images]);
What I am asking is, can I decode the images in one query?
Well this is a single query, json_decode runs after the SQL query returned your desired result.
What you can do is add a $casts property to your Service model so Laravel encodes/decodes it automatically for you, then you don't need to store these values with json_encode, just do $service->image = $url, and when you run findOrFail, the image property will already be a decoded json.
protected $casts = [
'image' => 'array',
];
Here's the documentation
You can use $cast or Accessor
1: $cast:
protected $casts = [
'image' => 'array'];
2: Accessor:
public function getImageAttribute()
{
return json_decode($this->attributes['image']);
}

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