mdkamrul
mdkamrul

Reputation: 274

Laravel "Trying to get property 'id' of non-object"

I wanted to return result of search at Laravel with resource controller here is my code

public function index(Request $request)
{
    try {

        // Verify the authentication role
        if (!$this->hasPermissionTo('read purchases')) {
            return $this->sendPermissionError();
        }
      // Init query
        $query = Purchase::query();

        // Default order by
        $query->orderBy('id', 'DESC');

        // Filter by keyword
        if ($request->has('keyword')) {
            $product_id = Product::where('product_name', $request->get('keyword'))->first()->id;
            if(!is_null($product_id)){
                $query->where('product_id', '=', "{$product_id}");
                $result = $query->paginate(10);
                return PurchaseResource::collection($result);
            }
            $category_id = Category::where('name', $request->get('keyword'))->first()->id;
            if(!is_null($category_id)){
                $query->orWhere('category_id', '=', "{$category_id}");
                $result = $query->paginate(10);
                return PurchaseResource::collection($result);
            }
        }
        $result = $query->paginate(10);
        // Retrieved execute
        //$purchases = Purchase::paginate();

        // Return objects
        return PurchaseResource::collection($result);

    } catch (\Exception $ex) {
        return $this->sendError($ex->getMessage());
    }
}

I am searching for both product and category and product but when I do for category this throughing me error "Trying to get property 'id' of non-object". for below lines

$product_id = Product::where('product_name', $request->get('keyword'))->first()->id;
$category_id = Category::where('name', $request->get('keyword'))->first()->id;

Can you please let me know why this?

Upvotes: 0

Views: 721

Answers (3)

Afandi Yusuf
Afandi Yusuf

Reputation: 389

You should check the value from first() is null or not by.

  $category = Category::where('name', $request->get('keyword'))->first();
  if($category){
   $category_id = $category->id
  }else{
   //do something if null
  }

Same with product_id

Hope it helps.

Upvotes: 1

Illya
Illya

Reputation: 1275

It seems you check null after ->first()->id

$category_id = Category::where('name', $request->get('keyword'))->first()->id;
    if(!is_null($category_id)){

You can use Null Coalescing Operator to insert value to $category if ->first()->id is undefined

$category_id = Category::where('name', $request->get('keyword'))->first()->id ?? null;
    if(!is_null($category_id)){

$category_id will be null instead of Throwing an Error

Upvotes: 0

Dilip Hirapara
Dilip Hirapara

Reputation: 15316

What if you don't get any results with relative keywords? the error you are getting when you search with keyword the product_name does not exist in DB. Hence, you are not getting any collection,

$product_id = $category_id = 0;
$product = Product::where('product_name', $request->get('keyword'))->first();
if(!empty($product)){
    $product_id = $product->id;
}
$category = Category::where('name', $request->get('keyword'))->first();
if(!empty($product)){
    $category_id = $category ->id;
}

You will get $product_id and $category_id 0 if not records match with the keyword.

Upvotes: 1

Related Questions