skip to Main Content
@extends('master') @section("content") <h1>Upload</h1> <form action="upload" method="POST" enctype="multipart/form-data">
    @csrf
        <div class="form-group">
        <label>Name</label>
        <input type="name" name="name" class="form-control" placeholder="Enter Name">
        </div>
        <div class="form-group">
        <label>Price</label>
        <input type="price" name="price" class="form-control" placeholder="Enter price">
        </div>
        <label>Category</label>
        <div class="dropdown">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Choose
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#">Item</a>
              <br><a class="dropdown-item" href="#">Diamond</a>
              <br><a class="dropdown-item" href="#">Akun</a>
            </div>
          </div>
        </div>
        <div>
        <input type="file" name="file"><br><br></div>
        <div>
        <button type="submit"> Sell Item</button>
        </div> </form> @endsection ```

function upload(Request $req)
{
    
    $products = new Product;
    $products->name=$req->name;
    $products->price=$req->price;
    $products = $req->file('file')->store('Products');
    $req->product()->upload([
        'file'=> $products
    ]);
    return redirect ('/');

} ```

i cannot upload it to database. its says

BadMethodCallException
Method IlluminateHttpRequest::product does not exist.

i want upload from form to database. before im using product database with seeder

3

Answers


  1. Your view

    <form method="POST" action="/upload" enctype="multipart/form-data">
      @csrf
      <div class="form-group">
        <label>Name</label>
        <input type="name" name="name" class="form-control" placeholder="Enter Name">
        </div>
        <div class="form-group">
        <label>Price</label>
        <input type="price" name="price" class="form-control" placeholder="Enter price">
        </div>
        <label>Category</label>
        <div class="dropdown">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            Choose
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
              <a class="dropdown-item" href="#">Item</a>
              <br><a class="dropdown-item" href="#">Diamond</a>
              <br><a class="dropdown-item" href="#">Akun</a>
            </div>
          </div>
        </div>
        <div>
        <input type="file" name="file"><br><br></div>
        <div>
        <button type="submit"> Sell Item</button>
        </div>
       </form>
    

    Your Controller

    public function upload(Request $request)
    {
    $products = new Product;
    $products->name = $request->name;
    $products->price = $request->price;
    if ($request->hasFile('file')) {
            $file = $request->file('file');
            $extension = $file->getClientOriginalExtension();
            $filename = time() . '.' . $extension;
            $file->move('uploads/', $filename);
            $products->file= $filename;
        }
    $products->save();
    return redirect('/');
    

    }

    That should work man

    Login or Signup to reply.
  2. Update your code in controller

    use IlluminateHttpRequest;

    public function upload(Request $request){
        $products = new Product;
        $products->name = $request->name;
        $products->price = $request->price;
    
        if ($request->file('file')) {
            $file = $request->file('file');
            $path = public_path() . '/uploads';
            $filename = time() . '_' . $file->getClientOriginalName();
            if ($file->move($path, $filename)) {
                $products->file= $filename;
            } 
        } 
    
        $products->save(); 
        return redirect('/');
    }
    

    In the path section you can define your file path, Where you want to upload your file

    Login or Signup to reply.
  3. Update your controller upload() function:

    use IlluminateHttpRequest;
    public function upload(Request $request){
        $request->validate([
             'file' => 'required|mimes:csv,txt,,xlx,xls,pdf|max:2048'
        ]);
        $products = new Product;
        $products->name = $request->name;
        $products->price = $request->price;
    
        if ($request->file('file')) {
            $fileName = time().'.'.$request->file->extension();
            $request->file->move(public_path('/upload'), $fileName);
            $products->file= $filename;
        } 
    
        $products->save(); 
        return redirect('/');
    }
    

    In this public_path(‘/upload’) path you can use your own path where you want to upload your file.

    You must have to write this line after your insert query $products->save(); this line otherwise it could not be store on database. And also you write your file upload path $file->move('uploads/', $filename); where your file will be uploaded, you could’t define. Your file upload path will be like this file->move(public_path('/your file path'), $fileName);

    Now, That should work properly.

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