I have a problem when I add multiple images, they are saved in a file instead of the image being saved ...
Look
https://gyazo.com/de126b998c2967f421805e933b53b30b
My Function
public function createPage(Request $request)
{
$images = $request->input('images');
$pages = Pages::create([
'title' => $request->input('title'),
'meta_title' => $request->input('meta_title'),
'content' => $request->input('content')
]);
// Store each Images
foreach ($images as $image) {
$imagePath = Storage::disk('uploads')->put('test' .'/pages', $image);
Files::create([
'post_image_captation' => 'image',
'post_image_path' => '/uploads' . $imagePath,
'pages_id' => $pages->id
]);
}
return $pages;
}
my blade
<div class="modal-body">
<form method="post" action="{{ route('admin.pages') }}">
#csrf
<div class="form-group">
<label for="recipient-name" class="col-form-label">Title</label>
<input type="text" class="form-control" name="title" id="title">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Meta Title</label>
<input type="text" class="form-control" name="meta_title" id="meta_title">
</div>
<div class="form-group">
<label for="recipient-name" class="col-form-label">Content</label>
<textarea class="ckeditor form-control" name="content" id="content"></textarea>
</div>
<div class="form-group">
<label for="images">Imagini</label>
<input type="file" class="form-control-file" name="images[]" id="images">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
Is there a solution to do what I created here in the easiest way possible?
Or is the logic good but I didn't put it into practice?
I think you modify a bit .Instead of Storage put you can use storeAs
if(isset($images)&&count((array)$images)){
foreach ($images as $image) {
$fileName = time().Str::random(8).'_'. $image->getClientOriginalName();
$image->storeAs('test/pages/', $fileName, 'uploads');
Files::create([
'post_image_captation' => 'image',
post_image_path' => '/uploads' . $fileName,
'pages_id' => $pages->id
]);
}
}
Related
Hi I am new to laravel and javascript,
I have a multiple input forms for updating of product information including a image upload using Dropzone.js to update product image. However I cant see to update my image file as I keep getting a null value for my dropzone image when I have already dragged a file in.
Below are my codes in the product-edit page:
<form action="{{ route('merchant.product.update', $product->prodID) }}" method="POST" class="dropzone" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('PATCH') }}
<div class="form-group">
<h5>Code</h5>
<input type="text" name="prodCode" class="form-control" id="prodCode" placeholder="Product Code" value="{{ old('prodCode', $product->prodCode) }}" required>
<div class="spacer"></div>
</div>
<div class="form-group">
<h5>Description</h5>
<textarea id="prodDesc" name="prodDesc" class="form-control" id="prodDesc" placeholder="Description" rows="5" required> {{ $product->prodDesc }}</textarea>
<div class="spacer"></div>
</div>
<div class="form-group">
<h5>Price</h5>
<input type="text" name="price" class="form-control" id="price" placeholder="Price" value="{{ old('price', $product->price)}}">
<div class="spacer"></div>
</div>
<div class="form-group">
<h5>Quantity</h5>
<input type="number" name="quantity" class="form-control" id="quantity" placeholder="Quantity" value="{{ old('quantity', $product->quantity)}}">
<div class="spacer"></div>
</div>
<div class="form-group">
<h5>Feature Product</h5>
<div class="switch">
<input type="checkbox" name="featured" class="switch-input" value="1" {{ old('featured', $product->featured=="true") ? 'checked="checked"' : '' }}/>
<div class="circle"></div>
</div>
</div>
<div class="form-group">
<h5>Product Image</h5>
<div id="dropzoneDragArea" class="dz-default dz-message dropzoneDragArea">
<span>Upload Image</span>
</div>
<div class="dropzone-previews"></div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-large btn-primary">Update Product</button>
</div>
<div class="spacer"></div>
</form>
Below are the Javascript portion of the blade.php:
Below are my Controller Methods I have for the update:
public function storeImage(Request $request) {
if($request->file('file')){
// Upload path
$destinationPath = 'img/products/';
// Get File
$image = $request->file('file');
// Get File Name
$imageName = $image->getClientOriginalName();
// Uploading File to given path
$image->move($destinationPath,$imageName);
$product = new Product();
$product->where('prodID', '=', $request->{'prodID'}, 'AND', 'created_by_merchant_id', '=', $this->checkMerchantID())
->update([
'prodImage' => $imageName,
]);
}
}
//update Product details
public function update(Request $request, $prodID)
{
if ($this->isCodeExist($request->{'prodCode'}, $request->{'prodID'})) {
return back()->withErrors('This code already exists!');
}
try{
$db = new Product();
$db->where('prodID', '=', $prodID, 'AND', 'created_by_merchant_id', '=', $this->checkMerchantID())
->update([
'prodCode' => $request->{'prodCode'},
'prodImage' =>$this->storeImage($request),
'prodDesc' => $request->{'prodDesc'},
'price' => $request->{'price'},
'quantity' => $request->{'quantity'},
'featured' => $request->input('featured') ? true : false,
]);
return back()->with('success_message', 'Product updated successfully!');
} catch (\Illuminate\Database\QueryException $ex) {
Log::channel('merchant_error')->error($ex);
return back()->withErrors('There seems to be an error in updating');
}
}
im trying to edit an image on laravel 6, but but it does not advance to next view, stays on the form view.
I have seen many tutorials of laravel 5.8 and 6. I can't make it work in any way
This is de controller:
public function update(Request $request, $id)
{
$validator = $request->validate([
'titulo' => 'required | max:50', //campo obligatorio y máximo 50 caracteres
'contenido' => 'required | max:150',
'imagen' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:4096',
]);
$image_name = time().'.'.$request->imagen->getClientOriginalExtension();
$request->image->move(public_path('images'), $image_name);
$datos = array(
'titulo' => $request->titulo,
'contenido' => $request->contenido,
'imagen' => $image_name,
);
Noticia::whereId($id)->update($datos);
return redirect('/mostrar');
}
THis is Web.php file:
Route::get('/actualizar/{id}', 'crearNoticiaController#update')->name('actualizar');
Route::get('/editar/{id}', 'crearNoticiaController#edit')->name('editar');
this is form file:
<div class="subir-imagen">
<form method="get" action="{{ route('actualizar', $noticia->id) }}" enctype="multipart/form-data">
#csrf
<div class="crear-titulo">
<input class="titulo" type="text" name="titulo" placeholder="Escriba el titulo" value="{{$noticia->titulo}}">
</div>
<div class="crear-contenido">
<textarea class="ckeditor" name="contenido" placeholder="Escriba el contenido" >
{{$noticia->contenido}}
</textarea>
</div>
<table border="2">
<tr>
<td><img src="{{URL::to('/')}}/images/{{$noticia->imagen}}" alt="imagen" width="250" align="left"/></td>
</tr>
</table>
<div class="form-group">
<div class="col-md-6">
<input type="file" class="form-control" name="imagen" />
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<input type="submit" class="btn btn-primary" value="Enviar" id="btn-enviar" />
</div>
</div>
</form>
</div>
Thnaks for help
I faced same issue, but luckily i solved this problem.I added my solution below, i think this will help you to solve this problem
public function updatePost(Request $request, $id)
{
$validatedData = $request->validate([
'title' => 'required|unique:posts|max:25|min:4',
'image' => 'mimes:jpeg,jpg,png,JPEG,JPG,PNG | max:100000',
]);
$data = array();
$data['category_id'] = $request->category_id;
$data['title'] = $request->title;
$data['details'] = $request->details;
$image = $request->file('image');
if($image)
{
$image_name = hexdec(uniqid());
$ext = strtolower($image->getClientOriginalExtension());
$image_full_name = $image_name.'.'.$ext;
$upload_path = 'public/assets/img/';
$image_url = $upload_path.$image_full_name;
$success = $image->move($upload_path,$image_full_name);
$data['image'] = $image_url;
unlink($request->old_photo);
$posts = DB::table('posts')->where('posts.id', $id)->update($data);
if($posts)
{
return Redirect()->route('all.posts')->with('success','Posts are inserted successfully');
}
else
{
return back()->with('error', 'Posts are not inserted successfully');
}
}
else
{
$data['image'] = $request->old_photo;
$posts = DB::table('posts')->where('posts.id', $id)->update($data);
if($posts)
{
return Redirect()->route('all.posts')->with('success','Posts are inserted successfully');
}
else
{
return back()->with('error', 'Posts are not inserted successfully');
}
}
}
edit_post.blade.php
#extends('welcome')
#section('content')
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-10 mx-auto">
<p>
List Posts
</p>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ url('posts.update_posts/'.$posts->id) }}" method="post" enctype="multipart/form-data">
#csrf
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<div>Category Name</div>
<label>Category ID</label>
<select class="form-control" name="category_id">
#foreach($category as $categories)
<option value="{{ $categories->id }}" <?php if ($categories->id == $posts->category_id)
echo "selected"; ?> > {{ $categories->name }} </option>
#endforeach
</select>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label>Product Title</label>
<input type="text" name="title" class="form-control" value="{{ $posts->title }}" id="title" required data-validation-required-message="Please product name.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label>Details</label>
<textarea name="details" rows="5" class="form-control" value="{{ $posts->details }}" id="details"></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="control-group">
<div class="form-group floating-label-form-group controls">
<label>Product Image</label>
<input type="file" name="image" class="form-control" id="image"><br/>
Old Image : <img src="{{ URL::to($posts->image) }}" style="hight: 40px; width: 100px">
<input type="hidden" name="old_photo" value="{{ $posts->image }}">
</div>
</div>
<br>
<div id="success"></div>
<div class="form-group">
<button type="submit" class="btn btn-success" id="sendMessageButton">Update</button>
</div>
</form>
</div>
</div>
</div>
#endsection
First run on your project console command:
php artisan storage:link
Then try this code and if return any error message tell me khow:
$imagen = $request->file("imagen");
$extension = $imagen->extension();
$filename = time().".".$extension;
$request->file('imagen')->storeAs("public/images", $filename);
Finally check your public/images folder for image file exists.
Also you can read about storing uploaded files in laravel 6.x official documentation
I've solved with this way:
In web.php I put patch instead get
Route::patch('/actualizar/{id}', 'crearNoticiaController#update')->name('actualizar');
In the edit blade I put: #method('PATCH')
And this is the update in the controller:
public function update(Request $request, $id)
{
$noticia = Noticia::findOrFail($id);
$noticia->titulo = $request->get('titulo');
$noticia->contenido = $request->get('contenido');
$noticia->imagen = $request->file('imagen');
$validator = $request->validate([
'imagen' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:4096',
]);
$imageName = time().'.'.request()->imagen->getClientOriginalExtension();
request()->imagen->move(public_path('images'), $imageName);
$noticia->imagen = $imageName;
$noticia->update();
return redirect('/mostrar'); //Redirigimos a la la vista para mostrar las noticias
}
How to update multiple rows through post request in laravel when i submit the code it shows an error like Array to string conversion.
How can i solve this issue any body help thanks in advance?
post form here
<form class="form-horizontal" method="POST" action="{{ route('multiple') }}">
{{csrf_field()}}
#foreach($classes as $class)
<div class="">
<input type="hidden" class="form-control" name="id[]" value="{{$class->id}}">
<div class="col-md-12">
<input id="name" type="text" class="form-control" name="name[]" value="{{$class->name}}">
</div>
</div>
<div class="">
<div class="col-md-12">
<input id="class" type="text" class="form-control" name="class[]" value="{{$class->class}}">
</div>
</div>
#endforeach
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
submit
</button>
</div>
</div>
</form>
this is method
public function post_loop(Request $request)
{
foreach ($request as $data )
{
$values = array(
'name' => $request->name,
'class' => $request->class,
);
DB::table('multiple_data')->whereIn('id',$request->id)->update($values);
}
}
Your code is completely wrong. The request is a object and has many data about the request. But you are using it as an array to get your input.
Try this code.
$ids = $request->input('id');
$names = $request->input('name');
$classes = $request->input('class');
foreach($ids as $k => $id){
$values = array(
'name' => $names[$k],
'class' => $classes[$k],
);
DB::table('yourTable')->where('id','=',$id)->update($values);
}
I try to upload multiple files to an specific folder using laravel. I was able to do this with just one file. But after I tried to upload multiple files it didn't work. First of all I tried to make an foreach. But it didn't work I tried to find solutions on the internet but unfortunately I didn't find one. Can anybody help me?
public function store(Request $request) {
// dd(request()->all());
$this->validate(request(), [
'name' => 'required|unique:lessons|max:20',
'type' => 'required',
'description' => 'required',
'price' => 'required',
'main_picture' => 'required',
'pictures' => 'required'
]);
$input = $request->all();
$images= array();
if($files=$request->file('images')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('lesson images',$name);
$images[]=$name;
}
}
$lesson = Lesson::create( [
'name' =>$input['name'],
'type' => $input['type'],
'description' =>$input['description'],
'price' => $input['price'],
'main_picture' =>$input['main_picture'],
'pictures'=> implode("|",$images),
]);
$image_name = $request->file('main_picture')->getClientOriginalName();
$id = $lesson->id;
if($request->hasFile('main_picture')) {
$file = $request->file('main_picture');
$file->move('lesson images', $id . "-main-" . $image_name);
}
return redirect('/lessenoverzicht');
}
HTML:
<form method="post" enctype="multipart/form-data" action="/lessenoverzicht/toevoegen">
{{ csrf_field() }}
<div class="form-group">
<label for="name"><span class='required'>*</span>Naam les:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Voer naam les in..">
</div>
<div class="form-group">
<label for="exampleFormControlSelect1"><span class='required'>*</span>Type</label>
<select name='type' class="form-control" id="type">
<option value="" disabled selected>Select your option</option>
<option value='les'>Les</option>
<option value='workshop'>Workshop</option>
</select>
</div>
<div class="form-group">
<label for="description"><span class='required'>*</span>Omschrijving:</label>
<textarea class="form-control" id="description" name="description" rows="10" placeholder="Voer omschrijving in..."></textarea>
</div>
<label for="description"><span class='required'>*</span>Prijs:</label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">€</span>
</div>
<input type="number" value="0.00" min="0" step="1.00" class="form-control" id='price' name='price'>
</div>
<div class="form-group">
<label for="main_picture"><span class='required'>*</span>Main Image:</label>
<input type="file" class='form-control' id='main_picture' name="main_picture">
</div>
<div class="form-group">
<label for="picture">Afbeelding(en) omschrijving:</label>
<input type="file" class='form-control' id='picture' name="pictures" multiple>
</div>
<div class="form-group">
#include('layouts.errors')
</div>
<button type="submit" class="btn btn-primary">Toevoegen</button>
</form>
Can you try the below change in your code? I have only added Array type name property name="pictures[]"
<input type="file" class='form-control' id='picture' name="pictures[]" multiple>
Sample Code:
<form class="form-horizontal" enctype="multipart/form-data" method="post" action="/details">
and this for multiple selection:
<input required type="file" class="form-control" name="images[]" placeholder="address" multiple>
PHP logic
public function store(request $request) {
$input=$request->all();
$images=array();
if($files=$request->file('images')){
foreach($files as $file){
$name=$file->getClientOriginalName();
$file->move('image',$name);
$images[]=$name;
}
}
/*Insert your data*/
Detail::insert( [
'images'=> implode("|",$images),
'description' =>$input['description'],
//you can put other insertion here
]);
return redirect('redirecting page');
}
I am trying to upload image to the folder on the server and store another data in the databse from the same form simultaneously. But it is showing error You did not select a file to upload.
Below is my view and controller for the same:
View:
<form role="form" method="post" action="job_insert" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label>Job Name*</label>
<input type="text" class="form-control" name="jname" placeholder="Enter Job Name" required>
</div>
<div class="form-group">
<label>Description</label>
<textarea rows="4" cols="10" class="form-control" name="description" required></textarea>
</div>
<div class="form-group">
<label>File input</label>
<input type="file" name="userimage">
</div>
<div class="form-group">
<label>Paper Size*</label>
<div class="form-group">
<label>Paper Cutting Size</label>
<div>
<input id="cutting_size" name="cutting_size" type="text" placeholder="Cutting Size" class="form-control input-md" required>
</div>
</div>
<div class="form-group">
<label>Sheet</label>
<div>
<input id="sheet" name="sheet" type="text" placeholder="No. of Sheet" class="form-control input-md" required>
</div>
</div>
<div class="form-group">
<label>Tin No.</label>
<div>
<input id="sheet" name="tin" type="text" placeholder="Tin No." class="form-control input-md" required>
</div>
</div>
</div>
<!-- /.box-body -->
<div style="margin-left: 600px" class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
Controller:
public function job_insert()
{
$this->form_validation->set_rules('jname', 'job_name', 'trim');
if($this->form_validation->run()== FALSE)
{
$this->load->view('invalid_jobname');
}
else
{
$this->do_upload();
$data = $_SESSION['email'];
$id = $this->user_model->get_client_id($data);
$data = array('cid' => $id,
'job_name' => $this->input->post("jname"),
'job_id' => $this->input->post("job_id"),
'description' => $this->input->post("description"),
'img_name' => $img_name,
'adate' => Date('Y-m-d'),
'status' => 'NEW',
'paper_size' => $this->input->post("paper_size"),
'paper_type' => $this->input->post("paper_type"),
'cutting_size'=> $this->input->post("cutting_size"),
'sheet'=> $this->input->post("sheet"),
'tin' => $this->input->post("tin"),
'lamination' => $this->input->post("checkboxes"),
'print_type' => $this->input->post("checkboxes1"),
'binding' => $this->input->post("checkboxes2"),
);
$query = $this->user_model->job_insert($data);
if ($query == TRUE)
{
$this->load->view('job_submitted');
}
else
{
$this->load->view('error_page');
}
}
}
Here's my do_upload function:
public function do_upload()
{
$config = array(
'upload_path' => "./uploads/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => FALSE,
'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
$image = $this->input->post("image");
echo $image;
if(!$this->upload->do_upload('userimage'))
{
$error = $this->upload->display_errors();
echo $error;
}
}
Change this to
<input type="file" name="image">
to
<input type="file" name="userfile"/>
and in do_upload
if(!$this->upload->do_upload())
to
if ( ! $this->upload->do_upload('userfile'))
Provide enctype="multipart/form-data" in your form tag. Without it you cannot upload image.
update form tag with below code
<form role="form" method="post" action="job_insert" enctype="multipart/form-data">