Kanchan186's avatar

how to define variable filename ErrorException (E_NOTICE) Undefined variable: filename

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;
0 likes
4 replies
bobbybouwmann's avatar

@kanchan186 You use $filename before you actually created the variable $filename! You can only use $filename after the if statement.

Snapey's avatar
Snapey
Best Answer
Level 122

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();
Kanchan186's avatar

thanks sir, above error solved but getting new error

ErrorException (E_ERROR)
Trying to get property 'p_name' of non-object (View: C:\xampp\htdocs\product\resources\views\prodview.blade.php)



<body>
        <div class="flex-center position-ref full-height">
            

            <div class="content">
                
                    <a href="{{url('/')}}/store">Add Prod</a>
                    <table border="1" >
                        
                        <tr>
                            <th>Prod Name</th>
                            <th>Product image</th>
                        </tr>


                        @foreach($product as $prod)
                         <tr>
                         <td>{{$prod->p_name}}</td>
                         <td>{{url('/')}}/$prod->image}}</td>
                            
                        </tr>
                        @endforeach
                    </table>
                        
                </div>

                
            </div>
       
    </body>

Snapey's avatar

See your other post add public $incrementing=false; to your model if you want to use your own primary key

Please or to participate in this conversation.