Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Michael Fayez's avatar

Attempt to read property "id" on null

what is the solution for this problem .. thanks in advance

0 likes
12 replies
Sinnbeck's avatar

Without code we can only guess. Maybe you have this, but no user is signed in

Auth::user()->id
tykus's avatar

@Michael Fayez the solution is to share the exception stacktrace and/or the relevant code

Michael Fayez's avatar

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Category;
use App\Models\SubCategory;
use App\Models\MultiImg;
use App\Models\Brand;
use App\Models\Product;
use App\Models\User;

class IndexController extends Controller
{

    public function Index(){
        $skip_category_0 = Category::skip(0)->first();
        $skip_product_0 = Product::where('status',1)->where('category_id',$skip_category_0->id)->orderBy('id','DESC')->limit(5)->get();

        $skip_category_2 = Category::skip(2)->first();
        $skip_product_2 = Product::where('status',1)->where('category_id',$skip_category_2->id)->orderBy('id','DESC')->limit(5)->get();

        $skip_category_7 = Category::skip(7)->first();
        $skip_product_7 = Product::where('status',1)->where('category_id',$skip_category_7->id)->orderBy('id','DESC')->limit(5)->get();

        $hot_deals = Product::where('hot_deals',1)->where('discount_price','!=',NULL)->orderBy('id','DESC')->limit(3)->get();

        $special_offer = Product::where('special_offer',1)->orderBy('id','DESC')->limit(3)->get();

        $new = Product::where('status',1)->orderBy('id','DESC')->limit(3)->get();

        $special_deals = Product::where('special_deals',1)->orderBy('id','DESC')->limit(3)->get();

        return view('frontend.index',compact('skip_category_0','skip_product_0','skip_category_2','skip_product_2','skip_category_7','skip_product_7','hot_deals','special_offer','new','special_deals'));

    }```
Michael Fayez's avatar

@tykus ```<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\Models\Category; use App\Models\SubCategory; use App\Models\MultiImg; use App\Models\Brand; use App\Models\Product; use App\Models\User;

class IndexController extends Controller {

public function Index(){
    $skip_category_0 = Category::skip(0)->first();
    $skip_product_0 = Product::where('status',1)->where('category_id',$skip_category_0->id)->orderBy('id','DESC')->limit(5)->get();

    $skip_category_2 = Category::skip(2)->first();
    $skip_product_2 = Product::where('status',1)->where('category_id',$skip_category_2->id)->orderBy('id','DESC')->limit(5)->get();

    $skip_category_7 = Category::skip(7)->first();
    $skip_product_7 = Product::where('status',1)->where('category_id',$skip_category_7->id)->orderBy('id','DESC')->limit(5)->get();

    $hot_deals = Product::where('hot_deals',1)->where('discount_price','!=',NULL)->orderBy('id','DESC')->limit(3)->get();

    $special_offer = Product::where('special_offer',1)->orderBy('id','DESC')->limit(3)->get();

    $new = Product::where('status',1)->orderBy('id','DESC')->limit(3)->get();

    $special_deals = Product::where('special_deals',1)->orderBy('id','DESC')->limit(3)->get();

    return view('frontend.index',compact('skip_category_0','skip_product_0','skip_category_2','skip_product_2','skip_category_7','skip_product_7','hot_deals','special_offer','new','special_deals'));

}```
asims's avatar

@michael fayez sounds like one of those $skip_category variables is null. You should be able to find the problem one from the stack trace.

asims's avatar

@michael fayez if you’re trying to leave code you’ll need to wrap it in ``` at the start and the end I think.

1 like
tykus's avatar

@michael fayez these will be null if the there are no matching records

$skip_category_0 = Category::skip(0)->first();
$skip_category_2 = Category::skip(2)->first();
$skip_category_7 = Category::skip(7)->first();

You should do something in mitigation...

Please or to participate in this conversation.