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

D33ts's avatar
Level 1

Trying to access array offset on value of type int

Hi am new to laravel and currently following a project based course to understand about e-commerce and am facing this issue **** Trying to access array offset on value of type int **** i get this error message when i click to display subsubcategory_view page and its shows there is an error in this line <td>{{ $item['subcategory_id']['subcategory_name_en'] }}</td>

*sub_subcategory_view.blade.php

@foreach($subsubcategory as $item)
	 <tr>
		<td> {{ $item['category']['category_name_en'] }}  </td>
		<td>{{ $item['subcategory_id']['subcategory_name_en'] }}</td>
		 <td>{{ $item->subsubcategory_name_en }}</td>
		<td width="30%">
<a href="{{ route('subsubcategory.edit',$item->id) }}" class="btn btn-info" title="Edit Data"><i class="fa fa-pencil"></i> </a>

<a href="{{ route('subsubcategory.delete',$item->id) }}" class="btn btn-danger" title="Delete Data" id="delete">
	<i class="fa fa-trash"></i></a>
		</td>

	 </tr>
	  @endforeach

This is the model

class SubSubCategory extends Model
{
    use HasFactory;

    protected $fillable = [
        'category_id',
        'subcategory_id',
        'subsubcategory_name_en',
        'subsubcategory_name_fr',
        'subsubcategory_slug_en',
        'subsubcategory_slug_fr',
    ];

    public function category(){
        return $this->belongsTo(Category::class,'category_id','id');
    }

    public function subcategory(){
        return $this->belongsTo(SubCategory::class,'subcategory_id','id');
    }


}

i created a relationship between category and subcategory and subsubcategory so in sub_subcategory_view its shows an multi dimensional array the first is for the method name in model and the second one is for field name table

Routes

//Admin Category routes
Route::prefix('category')->group(function (){
    Route::controller(CategoryController::class)->group(function (){
        Route::get('/view','categoryView')->name('all.category');
        Route::post('/store','categoryStore')->name('category.store');
        Route::get('/edit/{id}','categoryEdit')->name('category.edit');
        Route::post('/update','categoryUpdate')->name('category.update');
        Route::get('/delete/{id}','categoryDelete')->name('category.delete');


    });

    Route::controller(SubCategoryController::class)->group(function (){
        Route::get('/sub/view','subCategoryView')->name('all.subcategory');
        Route::post('/sub/store','subCategoryStore')->name('subcategory.store');
        Route::get('/sub/edit/{id}','subCategoryEdit')->name('subcategory.edit');
        Route::post('/sub/update','subCategoryUpdate')->name('subcategory.update');
        Route::get('/sub/delete/{id}','subCategoryDelete')->name('subcategory.delete');


        //Admin subsubcategory
        Route::get('/sub/sub/view','subSubCategoryView')->name('all.subsubcategory');
        Route::get('/subcategory/ajax/{category_id}','getSubCategory');
        Route::post('/sub/sub/store','subSubCategoryStore')->name('subsubcategory.store');
        Route::get('/sub/sub/edit/{id}','subSubCategoryEdit')->name('subsubcategory.edit');
        Route::post('/sub/sub/update','subSubCategoryUpdate')->name('subsubcategory.update');
        Route::get('/sub/sub/delete/{id}','subSubCategoryDelete')->name('subsubcategory.delete');


    });



});

sub_categories table

        Schema::create('sub_sub_categories', function (Blueprint $table) {
            $table->id();
            $table->integer('category_id');
            $table->integer('subcategory_id');
            $table->string('subsubcategory_name_en');
            $table->string('subsubcategory_name_fr');
            $table->string('subsubcategory_slug_en');
            $table->string('subsubcategory_slug_fr');
            $table->timestamps();
        });
    }

thank you !!

0 likes
20 replies
frankielee's avatar

shouldn't it be

//model->relationship->property
$item->subcategory->subcategory_name_en
frankielee's avatar

@D33ts

This line. I have updated my code at previous comment

		<td>{{ $item['subcategory_id']['subcategory_name_en'] }}</td>
D33ts's avatar
Level 1

@frankielee i got this error : Attempt to read property "subcategory_id" on null

D33ts's avatar
Level 1

@Sinnbeck

<div class="box-body">
					<div class="table-responsive">
					  <table id="example1" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>Category </th>
								<th>SubCategory Name</th>
								<th>Sub-Subcategory English </th>
								<th>Action</th>

							</tr>
						</thead>
						<tbody>
	 @foreach($subsubcategory as $item)
	 <tr>
		<td> {{ $item['category']['category_name_en'] }}  </td>
		<td>{{ $item->subcategory->subcategory_id }}</td>
		 <td>{{ $item->subsubcategory_name_en }}</td>
		<td width="30%">
 <a href="{{ route('subsubcategory.edit',$item->id) }}" class="btn btn-info" title="Edit Data"><i class="fa fa-pencil"></i> </a>

 <a href="{{ route('subsubcategory.delete',$item->id) }}" class="btn btn-danger" title="Delete Data" id="delete">
 	<i class="fa fa-trash"></i></a>
		</td>

	 </tr>
	  @endforeach
						</tbody>

					  </table>
					</div>
				</div>

D33ts's avatar
Level 1

@Sinnbeck oh sorry , that subcategory_id is mistake it should be

<td>{{ $item['subcategory']['subcategory_name_en'] }}</td>

but still the same error

frankielee's avatar

@D33ts

So you are not calling the subcategory_id at all? how come the error remain the same?

D33ts's avatar
Level 1

@frankielee i mean this error Trying to access array offset on value of type null

frankielee's avatar

@D33ts

So make sure every item has the subcategory?

Btw, do you notice that you are not sharing the controller?

D33ts's avatar
Level 1

@frankielee when i commented this line

<td>{{ $item['subcategory']['subcategory_name_en'] }}</td>

i have no errors but i have an empty column in fact subcategory is the name of a method that exist in model and subcategory_name_en is the name of the field in the table

D33ts's avatar
Level 1

Controller :

public function subSubCategoryView(){
        $categories = Category::orderBy('category_name_en','ASC')->get();
        $subsubcategory =  SubSubCategory::latest()->get();

      return view('backend.category.sub_subcategory_view',compact('subsubcategory','categories'));
    }

    public function getSubCategory($category_id){
        $subcat = SubCategory::where('category_id',$category_id)->orderBy('subcategory_name_en','ASC')->get();
     	return json_encode($subcat);
    }



    public function subSubCategoryStore(Request $request){
        $request->validate([
    		'category_id' => 'required',
    		'subcategory_id' => 'required',
    		'subsubcategory_name_en' => 'required',
    		'subsubcategory_name_fr' => 'required',
    	],[
    		'category_id.required' => 'Please select Any option',
    		'subsubcategory_name_en.required' => 'Input SubSubCategory English Name',
    	]);



	   SubSubCategory::insert([
		'category_id' => $request->category_id,
		'subcategory_id' => $request->subcategory_id,
		'subsubcategory_name_en' => $request->subsubcategory_name_en,
		'subsubcategory_name_fr' => $request->subsubcategory_name_fr,
		'subsubcategory_slug_en' => strtolower(str_replace(' ', '-',$request->subsubcategory_name_en)),
		'subsubcategory_slug_fr' => strtolower(str_replace(' ', '-',$request->subsubcategory_name_fr)),


    	]);

	    $notification = array(
			'message' => 'Sub-SubCategory Inserted Successfully',
			'alert-type' => 'success'
		);

		return redirect()->back()->with($notification);
}


    public function subSubCategoryEdit($id){
        $categories = Category::orderBy('category_name_en','ASC')->get();
    	$subcategories = SubCategory::orderBy('subcategory_name_en','ASC')->get();
    	$subsubcategories = SubSubCategory::findOrFail($id);
    	return view('backend.category.sub_subcategory_edit',compact('categories','subcategories','subsubcategories'));
    }

    public function subSubCategoryUpdate(Request $request){
        $subsubcat_id = $request->id;

    	SubSubCategory::findOrFail($subsubcat_id)->update([
		'category_id' => $request->category_id,
		'subcategory_id' => $request->subcategory_id,
		'subsubcategory_name_en' => $request->subsubcategory_name_en,
		'subsubcategory_name_fr' => $request->subsubcategory_name_fr,
		'subsubcategory_slug_en' => strtolower(str_replace(' ', '-',$request->subsubcategory_name_en)),
		'subsubcategory_slug_fr' => str_replace(' ', '-',$request->subsubcategory_name_fr),


    	]);

	    $notification = array(
			'message' => 'Sub-SubCategory Update Successfully',
			'alert-type' => 'info'
		);

		return redirect()->route('all.subsubcategory')->with($notification);
    }

    public function subSubCategoryDelete($id){

    	SubSubCategory::findOrFail($id)->delete();
    	 $notification = array(
			'message' => 'Sub-SubCategory Deleted Successfully',
			'alert-type' => 'info'
		);

		return redirect()->back()->with($notification);

    }

}
frankielee's avatar

@D33ts

Not sure which controller method is being called.

I believe this docs will solve your case.

1 like
D33ts's avatar
Level 1

@frankielee i think its the view not sure, because once i click to display the sub_subcategories as i have data in database ,i got that error

D33ts's avatar
Level 1

how can i share an image in this post ? i wanna clarify something pls however the error is gone but it shows 'the value is null' on some part of the the table and others not instead of subcategory name

D33ts's avatar
Level 1

update : now i ctrl+z to the old the stat of code

<table id="example1" class="table table-bordered table-striped">
						<thead>
							<tr>
								<th>Category </th>
								<th>SubCategory Name</th>
								<th>Sub-Subcategory English </th>
								<th>Action</th>

							</tr>
						</thead>
						<tbody>
	 @foreach($subsubcategory as $item)
	 <tr>
		<td> {{ $item['category']['category_name_en'] }}  </td>
		{{-- <td>{{ $item?->subcategory?->subcategory_name_en  ?? " " }}</td> --}}
        <td>{{ $item['subcategory']['subcategory_name_en'] }}</td>
		 <td>{{ $item->subsubcategory_name_en }}</td>
		<td width="30%">
 <a href="{{ route('subsubcategory.edit',$item->id) }}" class="btn btn-info" title="Edit Data"><i class="fa fa-pencil"></i> </a>

 <a href="{{ route('subsubcategory.delete',$item->id) }}" class="btn btn-danger" title="Delete Data" id="delete">
 	<i class="fa fa-trash"></i></a>
		</td>

	 </tr>
	  @endforeach

for some reason no more errors but , i can't add a sub_subcategory , but i can modify or delete

D33ts's avatar
Level 1

now SOMEHOW everything is fine concerning this , as my 1st post on laracasts this the weirdest issue i've ever faced i wanna thank @frankielee and @sinnbeck for helping me thank you !

Please or to participate in this conversation.