I'm having a hard time to read multiple file paths from my product tables in laravel.
I'm using Voyager as my admin interface, I have a multiple file upload that saves my path image in a field, when i query the table i get these result:
how do i read the photo field?
Already tried:
json_decode($product->photo,true) and it says "htmlspecialchars() expects parameter 1 to be string, array given"
with #foreach ($product->photo as $img) it gives me "Invalid argument supplied for foreach() "
photo column must be defined as JSON in its migrations file.
$table->json('photo');
And cast it to array in Product Model class:
protected $casts = [
"photo" => "array"
];
Now you will access photo as array and do what you want.
Related
I want to delete multiple string data that is passed by url separated by ','.
Scenario is :
http://127.0.0.1:8888/remove/lui,kui
Through this route lui and kui data that is value of name field in database will be deleted.
My web.php file
Route::get("/remove/{name}",[MyController::class,"remove"]);
MyController.php file
use App\Models\City;
function remove($name){
}
Thanks,your suggestion will be helpful.
You can accomplish this with the following:
//The string of names
$names = "cat,cat,cat,mouse,dog,rabbit";
//We break it down, seperate them by comma, then add them to an array
//After we will get the unique values only of that array
$arrayOfNames = array_unique(explode(',', $names));
//search in the name column for any of these values
//delete if found
//note that you can change the word "name" to any other column name
City::whereIn('name', $arrayOfNames)->delete();
If you have the softdelete trait in your model, and would like to hard delete its: https://laravel.com/docs/9.x/eloquent#soft-deleting
//search in the name column for any of these values
//hard delete if found
City::whereIn('name', $arrayOfNames)->forceDelete();
You can also do an update as well if that is something you are interested in the future:
//search in the name column for any of these values
//update if found
City::whereIn('name', $arrayOfNames)->update(['age' => 123, 'updated_at' => now()]);
Hope it works well for you :)
This is the first time that I try to generate a Word document with Laravel.
I have in database columns that I should to generate in a Word document.
For example I have the following database:
I should generate a document file which contains hello John for example
So all the elements that is inside '<<>>' are a dynamic variables .
How can I get from database the value of the column preamble and set the variable name in the same time?
I found that exists a package names phpwordn but I found that I should have a template that is a word file with dynamic variables n but in my case I have no template I have all the components of my Word file in the database.
Is it possible to do that?
If you have any idea or an example to do that help me.
You can use preg_replace_callback() to convert all special tags to their corresponding values. In your case a tag has a form of <<foo>>, so here it goes:
$values = [
'me' => 'user14053977',
'saviour' => 'RoboRobok',
];
$template = 'My name is <<me>> and <<saviour>> saved me!';
$result = preg_replace_callback(
'#<<(\w+)>>#',
function (array $matches) use ($values): string {
[$tag, $tagName] = $matches;
return $values[$tagName] ?? $tag;
},
$template
);
// $result is now "My name is user14053977 and RoboRobok saved me!"
It's a pure PHP code. Now it's just the matter of using the values in Laravel and injecting $result to your Word document.
This question already has answers here:
Laravel Model Get Method Decoding JSON
(3 answers)
Closed 2 years ago.
I'm actually creating an administrative panel, all my users have a column 'permissions' on DB that store a JSON table in TEXT format.
Actually when a user log in this column is stored in 'permissions' variable on session, but in TEXT, i need to parse it to json in order to make an object before store it.
Someone know how can i do this ?
You can decode JSON with json_decode:
$permissions = json_decode($user->permissions);
Though if you are using Eloquent Model's I would just cast this field to an array so it handles the encoding and decoding to and from JSON for you:
protected $casts = [
'permissions' => 'array',
];
Now when you access this permissions property of the Model it will return the decoded data:
$user->permissions;
I have a table named settings, in it, there are several columns, id,company_id, key, value.
In my Laravel model for Setting, I have the following cast:
protected $casts = [
'value' => 'array',
];
But when I go to retrieve data that has been stored, I can't.
For example, I have a record with the following value: "{\"default_remit_address\":\"2395\"}"
And when I go to retrieve the record in a Blade, it does pull it up correctly, but I'm not sure how to grab a specific value from the value field (like default_remit_address).
If I print the return "{{$settings->value}}" directly in the Blade, this is what I get:
{"default_remit_address":"2395"}
So how can I go one level deeper?
As this json object is being cast to an Array, you can just use regular array syntax to access it's contents.
$default_remit_address = Settings::find(1)->value['default_remit_address'];
or in your blade template
{{ $settings->value['default_remit_address'] }}
I want to update a totcosty field in the User table but it is throwing this error everytime and it is not updating the field
this is the function for execution:
public static function cost(){
$user = User::find($user_id);
$total = Helper::totcost();
// dd($tot_amt);
$user->totcosty = $total;
$user->save();
}
array to string means you are sending an array to the database but db will not accept it you have to explode() the array before sending it to db...
Hope it will help!
If you really want to store an array in some table field, then better declare it as a JSON field. For this, your DB should have support for JSON type columns.
See here how to do this.
Once this is done, you can save arrays in that column, you can assign an array value to the model property and laravel will convert it to JSON while saving and also it will be converted to array while retrieving.