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>
How is $product initialised; is it a Collection of models?
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');
}
}
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'));
}
after made changes
ErrorException (E_ERROR)
Undefined variable: Product (View: C:\xampp\htdocs\product\resources\views\prodview.blade.php)
Can you show your Product model?
with that can you dd($product); after the controller function and display the result over here.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class product extends Model
{
protected $table = 'products';
protected $fillable = ['p_name', 'image'];
protected $primaryKey = 'p_id';
}
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
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>
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;
@munazzil he already does what you suggest in the show method
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))
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
Why are you posting an error about writing to the database, when your question is about displaying a table of products?
no sir, i am using auto increment id and rename it as p_id
So why are you saving p_id in your store method?
Please sign in or create an account to participate in this conversation.