0
votes

I'm trying to implement uploading images to the CRUD controller. Everything works separately for me. But as soon as I implement the same code into the controller I still get a message that the file must be an image and have a valid extension

  • The image must be an image. The image must be a file of type: jpeg, png, jpg, gif, svg.

The code is as follows

$product->update($request->all());


    $request->validate([

        'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',

    ]);

    $imageName = time().'.'.$request->image->extension();
    $request->image->move(public_path('images'), $imageName);

    return redirect()->route('product.index');
    return back()
        ->with('success','You have successfully upload image..');

blade file

@extends('base')

@section('title', 'Vložení produktu') @section('description', 'Editor pro 
vytvoření nového produktu.')

@section('content')
   <div class="form-group">
    <label for="title">Název</label>
    <input type="text" name="title" id="title" class="form-control" value="{{ old('title') }}" required minlength="5" maxlength="80" />
</div>

<div class="form-group">
    <label for="url">URL</label>
    <input type="text" name="url" id="url" class="form-control" value="{{ old('url') }}" required minlength="5" maxlength="80" />
</div>

<div class="form-group">
    <label for="url">URL prodejce</label>
    <input type="text" name="seller_url" id="seller_url" class="form-control" value="{{ old('seller_url') }}" required minlength="5" maxlength="80" />
</div>

<div class="row">
    <div class="form-group col-md-6">
        <label for="url">Cena</label>
        <input type="text" name="price" id="price" class="form-control" value="{{ old('price') }}" />
    </div>
    <div class="form-group col-md-6">
        <label for="url">Stará cena</label>
        <input type="text" name="old_price" id="old_price" class="form-control" value="{{ old('old_price') }}" />
    </div>
</div>



<div class="form-group">
    <label for="url">Obrázky</label>
    <input type="file" name="image" id="image" class="form-control-file"/>
</div>

<button type="submit" class="btn btn-primary">Vytvořit produkt</button>
</form>
@endsection
2
How are you uploading the image? Are you using a form? An AJAX request? Is your input/data field named image? Please include all relevant information, including the code performing the upload.Tim Lewis
For recording I use the form see. another commentmartin suchodol

2 Answers

0
votes

Try this one, maybe helpful for you

$dirpath = public_path('images');
$name = $request['image']->getClientOriginalExtension();
$filename = time().'_'.$name;
$file=$request['image']->move($dirpath, $filename);
0
votes
First make sure to run this command php artisan storage:link
and make sure you are using the right code in your controller in store
Jst an example:


public function store(CreatePostsRequest $request)
    {
        //dd($request->all());
        //Upload the image
 $image =$request->image->store('posts');
        //Create the post
 $post = Post::create([
    'image' => $image
]);