Reading a raw array stored in mysql with blade - laravel

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

Related

casts property is not working to change date format

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') }}

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

How to display single encrypt record?

I use encrypt. I can display all table data. However I can'T get one record. I don't encrypt 'id'. I tried another column but same.
Here is my error
Trying to get property 'id' of non-object
Here is my code
blade file (I click this link first )
{{ $val->id }}
blade file (display page I got error at this page)
#foreach ($data as $val)
{{ $val->id }}
#endforeach
Trying to get property 'id' of non-object
web.php
Route::get('/one','MailController#onerecord');
Route::post('/one','MailController#onerecord');
Controller
public function onerecord(Request $request)
{
$id = $request['id'];
$data = Contact::where('id',$id)->get();
return view('mail.one', ['data' => $data]);
}
Could you teach me what is wrong my code please?
Use dd($data) after retrieving your Contact with Contact::where('id',$id)->get(); in order to investigate the content of $data. Most likely an object is returned and not an array, so looping through $data inside your blade #foreach will loop through the object's properties. Therefore $val->id isn't valid but the direct access of $data->id would be, without the need of looping through $data.

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