Reputation: 1920
First Laravel project. I have a DBController what selects a row from the database after the url (for example the URL is /product/123 then it search for the product which barcode is 123). And I want to make a table about the productdetails on the next page.
Controller:
class ProductDetailsController extends Controller
{
public function product($id){
$product = DB::select('select * FROM inventory WHERE barcode = ?', [$id]);;
return view('productdetails',['product'=>$product]);
}
}
print_r($product) output:
Array ( [0] => stdClass Object ( [ID] => 1 [barcode] => 01233 [brand] => Ismeretlen [supplier] => 1 [type] => Építésianyag [name] => Homok [img] => homok.jpg [wholeprice] => 3000 [price] => 5500 [VAT] => 3 [whereis] => U [whereis2] => 1 [count] => 8 [dimension] => m3 [threshold] => 1 [lastsell] => [lastbuy] => [lastchange] => 2017-02-23 20:56:46 [register] => ) )
If I try {{ $product->brand }}
I got:
ErrorException in 20d205ed160f36c734b8252e44ac81bfa0977988.php line 6: Trying to get property of non-object (View: /var/www/html/project/laravel/leltar/resources/views/productdetails.blade.php)
And if I try {{ $product['brand'] }}
I got:
ErrorException in 20d205ed160f36c734b8252e44ac81bfa0977988.php line 6: Undefined index: brand (View: /var/www/html/project/laravel/leltar/resources/views/productdetails.blade.php)
What's going wrong?
I learn Laravel from this PDF
Upvotes: 1
Views: 1988
Reputation: 65
you should do it with eloquent model,Create a modle of inventory like that $products=Inventory::where('id',$id)->first();
and you can get brands $products->brand.
Upvotes: 0
Reputation: 756
try
$product = DB::select('select * FROM inventory WHERE barcode = ?', [$id])->first();
dd($product);
Upvotes: 0
Reputation: 18557
Once check the structure of array it is having 0th index first,
You should get it like,
{{$product[0]->brand}}
OR
$product = DB::table("inventory")->where("barcode", $id)->first();
// and get data as
echo $product->brand;
Give it a try, it should work.
Upvotes: 3