Level 88
@kanchan186 You use $filename before you actually created the variable $filename! You can only use $filename after the if statement.
public function store(Request $request)
{
$product=new product();
product::create([
'p_id' => request('p_id'),
'p_name' => request('p_name'),
'image' =>$filename
]);
if($request->hasfile('image'))
{
$file=$request->file('image');
$extension=$file->getClientOriginalExtension();
$filename=time().'.'.$extension;
$file->move('uploads/product/',$filename);
$product->image=$filename;
You have two issues
1st, you get a new product and put it in $product
2nd, but then you create a second product with the product::create
You save the name in the create function, but you save the filename with the earlier $product.
Suggest also that your models are capitalised, eg Product
public function store(Request $request)
{
$product=new product();
$product->p_id = request('p_id');
$product->p_name = request('p_name');
if($request->hasfile('image'))
{
$file=$request->file('image');
$extension=$file->getClientOriginalExtension();
$filename=time().'.'.$extension;
$file->move('uploads/product/',$filename);
$product->image=$filename;
}
$product->save();
Please or to participate in this conversation.