First step - create a new a Gallery (Name and Type).
Click 'Next' button.
Second step - upload multiple images to that gallery.
What can I do in order to achieve that in Laravel Nova admin panel? I can't follow documentation and just add HasMany::make('Images') to a Gallery resource, I need a two steps form.
If I understand right first step you need to make from view input <input type="file" multiple>
than send request and in your controller you need something like this:
$date = Carbon::now()->format('Y-m-d-hh-mm-ss');
$files = request('images');
$pluss = 1;
foreach ($files as $file) {
$imageName = $date . '.' . $pluss . $file->getClientOriginalExtension();
$file->move(public_path('/images/products'), $imageName);
$pluss++;
$data = [
'image' => $imageName,
'product_id' => $product->id
];
Image::create($data);
}
Related
I am using Image Intervention to merge two images in Controller.
Now I want to show this image in view file but I get this error.
Intervention\Image\Exception\NotReadableException Image source not readable
Here are my codes.
Controller
use App\Models\User;
use InterventionImage;
public function qrcode(User $user)
{
$qrcode_path = ('public/storage/qrcode/' . $user->random . '.png');
$qrcode_img = InterventionImage::make($qrcode_path);
$qrcode_template_path = ('public/storage/home/qrcode_template.png');
$qrcode_template_img = InterventionImage::make($qrcode_template_path);
$qrcode_merged = $qrcode_template_img->insert($qrcode_img, 'center');
return view('test.myqrcode')->with([
'user' => $user,
'qrcode_template_img' => $qrcode_merged
]);
}
view
<img src="{{$qrcode_merged}}">
I appreciate if you could tell me what I'm doing wrong or what I should do. Any help would be appreciated as I have tried multiple methods with no success.
you need to save the image first
and the value of the src attribute must be the path to your image, not an instance of Intervention\Image\Image
make sure you saved the first image in 'your_root_project/storage/app/public/qrcode'
, the second in 'your_root_project/storage/app/public/home'
And already run command "php artisan storage:link"
$qrcode_path = public_path('/storage/qrcode/' . $user->random . '.png');
$qrcode_img = \Intervention\Image\Facades\Image::make($qrcode_path);
$qrcode_template_path = public_path('/storage/home/qrcode_template.png');
$qrcode_template_img = \Intervention\Image\Facades\Image::make($qrcode_template_path);
$your_image_name = "merged_image_name";
$path_show_in_view = 'storage/qrcode/' . $your_image_name . '.png';
$qrcode_merged->save(public_path("/storage/qrcode/" . $your_image_name . ".png"));
return view('test.myqrcode')->with([
'user' => $user,
'path_show_in_view' => $path_show_in_view
]);
view
<img src="{{asset($path_show_in_view)}}">
Hi there, Is it absolutely necessary to save the image? I want to do it without saving the image.
you can encode to base64 if you don't want to save it
$qrcode_path = public_path('/storage/qrcode/' . $user->random . '.png');
$qrcode_img = \Intervention\Image\Facades\Image::make($qrcode_path);
$qrcode_template_path = public_path('/storage/home/qrcode_template.png');
$qrcode_template_img = \Intervention\Image\Facades\Image::make($qrcode_template_path);
$qrcode_merged = $qrcode_template_img->insert($qrcode_img, 'center');
return view('test.myqrcode')->with([
'user' => $user,
'qrcode_merged' => $qrcode_merged
]);
view
<img src="{{$qrcode_merged->encode('data-url')}}"/>
When I click on submit, it's only storing last selected file where it suppose to store all the images and how to retrieve those files? Any suggestion or example? Do I need to have different attributes for multiple files?
Controller
if (is_array($request->carEvidence)) {
foreach ($request->carEvidence as $key => $file) {
$destinationPath = public_path('image/');
$profileImage = $key . "-" . date('YmdHis') . "." . $file->getClientOriginalExtension();
$file->move($destinationPath, $profileImage);
$post['carEvidence'] = "$profileImage";
}
}
Views
<input type="file" name="carEvidence[]" multiple>
MYSQL
https://ibb.co/9sPLw8C
File Input
https://ibb.co/VHkxRwc
you are passing multiple files in array, so you have to use loop to retrieve all the files in controller.. something like
if (is_array($request->carEvidence))
{
foreach ($request->carEvidence as $key => $file) {
$destinationPath = public_path('image/');
$profileImage = $key."-".date('YmdHis') . "." . $file->getClientOriginalExtension();
$file->move($destinationPath, $profileImage);
// code to save in your db table
}
}
I am working on multiple image upload in laravel. Uploading is successful but, i got an issue. For later use i should store in database. For single column it easy but for different column for different images i couldnot do it.
Here is my image upload code
$images = [];
$destDir = "public/uploads/";
if($request->file('image1')) $images[]=$request->file('image1');
if($request->file('image2')) $images[]=$request->file('image2');
if($request->file('image3')) $images[]=$request->file('image3');
foreach($images as $image)
{
if(!empty($image))
{
$imageName = $image->getClientOriginalName();
$extension = $image->getClientOriginalExtension();
$temPath = $image->getRealPath();
$enImg = mt_rand(1111,9999).".".$extension;
$newPath = $destDir.$enImg.".".$extension;
move_uploaded_file($temPath, $newPath);
}
}
I have a feeling you are approaching this in a way that is making it more difficult for you. Instead of creating multiple separate file inputs in your form you can only set a multiple file input and set the name of the input to an array like so:
<form action="demo_form.asp">
Images: <input type="file" name="images[]" multiple>
<input type="submit">
</form>
Then in your php you can do something like this:
if (isset($request->all()['images'])) {
$this->persistUserImages($request->all()['images'], $userId);
}
/**
* Persists user images to storage and DB
*
* #param array $userImages
* #param int $userId
* #return array
*/
private function persistUserImages(array $userImages, $userId = 1)
{
if (empty($userImages)) {
return [];
}
$uploadedUserImagesFilenames = $this->uploadUserImagesToCloud($userImages, $userId);
foreach ($uploadedUserImagesFilenames as $filename) {
$userImage = new UserImage([
'user_id' => $userId,
'filename' => $filename,
'visible' => 1
]);
$userImage->save();
}
}
You can do like this this. In your blade,
<form action="{{url('path')}}" methos="post" enctype="multipart/form-data">
Images: <input type="file" name="images[]" multiple>
<input type="submit">
</form>
In you method,
public function store(Request $request){
foreach ( $request->images as $image) {
Model::create([
'image_url' => $image,
]);
}
}
Or
follow below link. It's very easy way to solve this problem.
Multiple Image Upload In Laravel
Hope you can solved your problem.
create a pivot table and store images in separate rows..
Table schema:- `images` table
columns `id`,`user_id`(foreign key),`image_path`
store your $newPath in this table every time..
I am trying to renaming uploaded file to same as ID of table Slider for unique name
public function postAddSlider(){
$title = Input::get('title');
$image = Input::file('image');
$link = Input::get('link');
$description = Input::get('description');
$filename = $image->getClientOriginalName(); //<-- i want to change that filename to be id of table Slider
$uploadSuccess = Input::file('image')->move(base_path().'/public/assets/slider/', $filename);
if($uploadSuccess){
$slider = Slider::create(array(
'image' => $uploadSuccess->getRealPath(),
'title' => $title,
'link' => $link,
'description' => $description
));
if($slider){
return Redirect::route('slider-add-get')
->with('message','Slider Added Successfully');
}
}
else{
return Redirect::route('slider-add-get')
->with('message','File Upload Error');
}
}
or do you have other solution to renaming for unique name?
I have no idea what you're schema is like, but if you want to get the id of a model, you have to save it first.
Perhaps you could do something like this:
$slide = new Slide();
$slide->title = Input::get('title');
$slide->url = $temporaryurl;
...etc...
$slide->save();
//once the slide is saved, we can access its id
$slide->url = 'slide-' . $slide->id . $file->getClientOriginalExtension();
$slide->save(); //updates the new url
Or, if you're just looking to generate unique file names, you could use the original filename + the timestamp... Could possibly result in duplicates... very unlikely though (that 2 images would be uploaded with the same filename within the same second).
Or, perhaps, the slide title (stripped of illegal characters, spaces replaced with dashes, etc), plus the timestamp.
The goal should be following:
I have an edit view with a custom field (dropdown) that depends on another table. There I can choose from a list of adresses (the second table) to save the id of the data row. I started with this:
Custom field code:
jimport('joomla.form.helper');
JFormHelper::loadFieldClass('list');
JHTML::_('behavior.modal');
class JFormFieldInvoiceAdress extends JFormFieldList
{
protected $type = 'invoiceadress';
protected function getInput() {
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id,zip,city,adress');
$query->from('#__pb_invoiceadresses');
$db->setQuery((string)$query);
$types = $db->loadObjectList();
$options = array();
foreach($types as $type) {
$options[] = JHTML::_('select.option', $type->id, $type->zip . " " . $type->city . ", " .$type->adress);
}
$dropdown = JHTML::_('select.genericlist', $options, $this->name, 'class="inputbox"', 'value', 'text', $this->value);
$link = 'index.php?option=com_mycomponent&view=invoiceadresseedit&layout=edit&id=0';
$dropdown .= "<a href=\"" . JRoute::_($link) . "\" class=\"modal\" rel=\"{handler: 'iframe', size: {x: 875, y: 550}, onClose: function() {}}\" >Neue Adresse</a>";
return $dropdown ;
}
}
This works so far but I have to update the content of the dropdown on closing this modal window and not getting the list view of the invoiceadresses in the modal window.
My second attempt was to add 'tmpl=component' in the link but then I don't have a save button. I have no idea how to accomplish that. Anyone have already solved this?
Found the solution and I am answering it for the next person who comes along with the same question.
Call the edit view with this link:
$link = 'index.php?option=com_mycomponent&view=invoiceadresseedit&layout=edit&id=0&tmpl=component';
This will display only the form without the rest of the administration gui and toolbar.
Add a save button to your edit form like this:
<input class="button" type="submit" value="<?php echo JText::_('SAVE');?>" onClick="window.parent.location.reload();" />
And thats it. The data will be saved and after this the modal windows closes and the current page reloads, the dropdown will be updated with the new data.