skip to Main Content

hello i want to upload image via laravel. but i got this Error message:

Call to a member function getClientOriginalExtension() on string

i put my code below and will appreciate you for solve this problem.

i searched the problem and most of people said i should add enctype="multipart/form-data" but also i do have this attribute in my form.

here is my code

my Product.blade.php:

<form action="{{url('/addProduct')}}" mathod="POST" class="forms-sample" enctype="multipart/form-data">
                    @csrf  
                    <div class="form-group">
                        <label for="exampleInputUsername1">title</label>
                        <input type="text" class="form-control" name="title" id="exampleInputUsername1" placeholder="name of product">
                      </div>
                      
                      <div class="form-group">
                        <label for="exampleInputUsername1">description</label>
                        <textarea type="text" class="form-control" name="description" id="exampleInputUsername1" placeholder="description of product"></textarea>
                      </div>

                      <div class="form-group">
                        <label for="exampleInputUsername1">price</label>
                        <input type="text" class="form-control" name="price" id="exampleInputUsername1" placeholder="price of product">
                      </div>

                      <div class="form-group">
                        <label for="exampleInputUsername1">discount price</label>
                        <input type="text" class="form-control" name="discountprice" id="exampleInputUsername1" placeholder="discount price of product">
                      </div>

                      <div class="form-group">
                        <label for="exampleInputUsername1">quantity</label>
                        <input type="text" class="form-control" name="quantity" id="exampleInputUsername1" placeholder="quantity of product">
                      </div>

                      <div class="form-group">
                      <label for="exampleFormControlSelect1">category</label>
                      <select class="form-control form-control-lg" name="category" id="exampleFormControlSelect1">
                            @foreach($category as $cat)
                            <option value="{{$cat->category_name}}">{{$cat->category_name}}</option>
                            @endforeach
                      </select>
                    </div>

                        <input type="file" name="image" class="file-upload-default">

                      <input type="submit" class="btn btn-primary mr-2">Submit</input>
                      <button class="btn btn-dark">Cancel</button>
                    </form> 

my Controller:

use AppModelsCategory;
use AppModelsProduct;
.
.
.
public function addProduct(Request $request)
    {
        $product=new product;

        $product->title=$request->title;
        $product->description=$request->description;
        $product->category=$request->category;
        $product->quantity=$request->quantity;
        $product->price=$request->price;
        $product->discount_price=$request->discountprice;

        $image= $request->image;
        $imagename=time().'.'.$image->getClientOriginalExtension();
        $request->image->move('product', $imagename);

        // $product->image=$imagename;

        $product-> save();

        return redirect()->back()->with('message', 'محصول با موفقیت افزوده شد');
    }

my web.php file:

route::get('/addProduct', [AdminController::class, 'addProduct']); 

i can’t findout what is wrong with my code because when i check with other tutorials there is no difference.

2

Answers


  1. you need to use file() method

        $image= $request->file('image');
        $imagename=time().'.'.$image->getClientOriginalExtension();
        $request->image->move('product', $imagename);
    
    Login or Signup to reply.
  2. Maybe the reason is this line

    <form action="{{url('/addProduct')}}" mathod="POST" class="forms-sample" enctype="multipart/form-data">
    
    

    change mathod to method.

    And change get to post in your route as well

    Route::post('/addProduct', [AdminController::class, 'addProduct']); 
    

    And then you can re-test ! I hope it works .

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search