Kanchan186's avatar

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>
0 likes
15 replies
tykus's avatar

How is $product initialised; is it a Collection of models?

Kanchan186's avatar

yes sir, actually sir, i am very new to laravel. please help me to find out error

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\product;


class ProductController extends Controller
{
   public function addpd()
                {
                $product=product::get();
                      return view('addprod',compact('product'));
                  }


   public function store(Request $request)
      {
         //dd($request->all());

         //$product=new product();
        // $product->save();

         $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();
    return view('prodview')->with('product',$product);
    

    }



 public function show()
                 {
                    $product=product::get();
                    return view('prodview',compact('product'));

                    //return redirect('addstate');
                  }                 



    }

munazzil's avatar

You have to use your model product as Product P should be capital as like below,

 use App\Product;   


   public function addpd()
            {
            $product=Product::get();
            return view('addprod',compact('product'));
            }
Kanchan186's avatar

after made changes

ErrorException (E_ERROR)
Undefined variable: Product (View: C:\xampp\htdocs\product\resources\views\prodview.blade.php)
munazzil's avatar

Can you show your Product model?

with that can you dd($product); after the controller function and display the result over here.

Kanchan186's avatar
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class product extends Model
{
    protected  $table = 'products';
   protected $fillable = ['p_name', 'image'];
    protected  $primaryKey = 'p_id';

}

Kanchan186's avatar

when i add dd($request->all()); to productController.php

output:


array:4 [▼
  "_token" => "cNjqi1BY7C2OMyAkCvrStcxKAcwuLnwoFGZUNyZv"
  "p_name" => "xcvcfv"
  "image" => "86282.jpg"
  "submit" => null
]

problem is that image not stored in database

munazzil's avatar

Use as like below in controller,

    use App\product;   


   public function addpd()
        {
        $product=product::get();
        return view('addprod',compact('product'));
        }

and in the blade use as like below and check because you have two or more data only can be use @foreach otherwise remove it and check,

                     <tr>
                     <td>{{$product ->p_name}}</td>
                     <td>{{url('/')}}/$product ->image}}</td>
                        
                    </tr>
Snapey's avatar

If you want to provide your own product id - p_id as the primary key, and not use an auto-incrementing id, then you MUST add to your model

public $incrementing = false;
Kanchan186's avatar

again error

SQLSTATE[HY000]: General error: 1364 Field 'p_name' doesn't have a default value (SQL: insert into `products` (`updated_at`, `created_at`) values (2019-08-22 07:28:23, 2019-08-22 07:28:23))
Snapey's avatar

Actually, you should STOP and watch some of the free videos on this site, and the many on-line resources. Do some training first. Experiment in small steps. Don't just launch yourself into building something after watching 3 videos.

oh, and don't take advice from @mu

Snapey's avatar

Why are you posting an error about writing to the database, when your question is about displaying a table of products?

Kanchan186's avatar

no sir, i am using auto increment id and rename it as p_id

Snapey's avatar

So why are you saving p_id in your store method?

1 like

Please or to participate in this conversation.