@extends('master') @section("content") <h1>Upload</h1> <form action="upload" method="POST" enctype="multipart/form-data">
@csrf
<div >
<label>Name</label>
<input type="name" name="name" placeholder="Enter Name">
</div>
<div >
<label>Price</label>
<input type="price" name="price" placeholder="Enter price">
</div>
<label>Category</label>
<div >
<button type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Choose
</button>
<div aria-labelledby="dropdownMenuButton">
<a href="#">Item</a>
<br><a href="#">Diamond</a>
<br><a 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 Illuminate\Http\Request::product does not exist.
i want upload from form to database. before im using product database with seeder
CodePudding user response:
if you use $req->product()
in your form, don´t have value for your product. You need set value for your instance product and use $product->save();
but you can´t use $req->product();
CodePudding user response:
Your view
<form method="POST" action="/upload" enctype="multipart/form-data">
@csrf
<div >
<label>Name</label>
<input type="name" name="name" placeholder="Enter Name">
</div>
<div >
<label>Price</label>
<input type="price" name="price" placeholder="Enter price">
</div>
<label>Category</label>
<div >
<button type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Choose
</button>
<div aria-labelledby="dropdownMenuButton">
<a href="#">Item</a>
<br><a href="#">Diamond</a>
<br><a 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