1

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

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I tried the first method and it's working. I will try the second method, too :) The site needs 8 more minutes before I can accept your answer
@NeetinSolanki I will accept it but when I click on the checkmark it says, that "You can accept the answer in 3 minutes".
0

try

$product = DB::select('select * FROM inventory WHERE barcode = ?', [$id])->first();

dd($product);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.