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

shahr's avatar
Level 10

Trying to access array offset on value of type null

table

public function up()
{
    Schema::create('skills', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('skill_name');
        $table->smallInteger('level');
        $table->timestamps();
    });

    Schema::create('skill_user', function (Blueprint $table) {
        $table->bigInteger('skill_id')->unsigned()->nullable();
        $table->foreign('skill_id')->references('id')->on('skills')->onDelete('cascade');
        $table->bigInteger('user_id')->unsigned()->nullable();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->unique(['skill_id' , 'user_id']);
    });
}

blade

    showDynamicExperimental();
function showDynamicExperimental()
{
    let html = '' +
        '@foreach(auth()->user()->skills as $skill)\n'+
        '<div class="col-md-6 position-relative">\n' +
        '<i class="fas fa-times text-danger position-absolute"></i>' +
        '<div class="row">\n' +
        '<div class="col-md-8">\n' +
        '<div class="form-group">\n' +
        '<label for="skill_name">skill_name</label>\n' +
        '<input type="text" id="skill_name" name="skill_name[]" class="form-control" value="{{ $skill->skill_name }}">\n' +
        '</div>\n' +
        '</div>\n' +
        '<div class="col-md-4">\n' +
        '<div class="form-group">\n' +
        '<label for="level">level</label>\n' +
        '<select class="form-control" id="level" name="level[]">\n' +
        '<option value="1" {{ $skill->level == 1 ? 'selected' : '' }}>★☆☆☆☆</option>\n'+
        '<option value="2" {{ $skill->level == 2 ? 'selected' : '' }}>★★☆☆☆</option>\n'+
        '<option value="3" {{ $skill->level == 3 ? 'selected' : '' }}>★★★☆☆</option>\n'+
        '<option value="4" {{ $skill->level == 4 ? 'selected' : '' }}>★★★★☆</option>\n'+
        '<option value="5" {{ $skill->level == 5 ? 'selected' : '' }}>★★★★★</option>\n'+
        '</select>\n'+
        '</div>\n' +
        '</div>\n' +
        '</div>\n' +
        '</div>\n ' +
        '@endforeach';
    $('#showExperimental').append(html);
}

Controller

    $user->skills()->detach();
    if ($request->skill_name) {
        foreach($request->skill_name as $key => $social){
            $user->socials()->attach($social, ['skill_name' => $request->link[$key]]);
        }
    }

User.php

public function skills()
{
    return $this->belongsToMany(Skill::class);
}

I get this error

Trying to access array offset on value of type null

0 likes
33 replies
Snapey's avatar

you are trying to access skill_id but I don't see that in any of your posted code

shahr's avatar
Level 10

My form is like this.

skills

What do you suggest ??

I changed this code

Controller

$user->skills()->detach();
if ($request->skill_id) {
    foreach($request->skill_id as $key => $social){
        $user->socials()->attach($social, ['skill_id' => $request->link[$key]]);
    }
}

blade

showDynamicExperimental();
function showDynamicExperimental()
{
	let html = '' +
		'@foreach(auth()->user()->skills as $skill)\n'+
		'<div class="col-md-6 position-relative">\n' +
		'<i class="fas fa-times text-danger position-absolute"></i>' +
		'<div class="row">\n' +
		'<div class="col-md-8">\n' +
		'<div class="form-group">\n' +
		'<label for="skill_id">skill_name</label>\n' +
		'<input type="text" id="skill_id" name="skill_id[]" class="form-control" value="{{ $skill->skill_name }}">\n' +
		'</div>\n' +
		'</div>\n' +
		'<div class="col-md-4">\n' +
		'<div class="form-group">\n' +
		'<label for="level">level</label>\n' +
		'<select class="form-control" id="level" name="level[]">\n' +
		'<option value="1" {{ $skill->level == 1 ? 'selected' : '' }}>★☆☆☆☆</option>\n'+
		'<option value="2" {{ $skill->level == 2 ? 'selected' : '' }}>★★☆☆☆</option>\n'+
		'<option value="3" {{ $skill->level == 3 ? 'selected' : '' }}>★★★☆☆</option>\n'+
		'<option value="4" {{ $skill->level == 4 ? 'selected' : '' }}>★★★★☆</option>\n'+
		'<option value="5" {{ $skill->level == 5 ? 'selected' : '' }}>★★★★★</option>\n'+
		'</select>\n'+
		'</div>\n' +
		'</div>\n' +
		'</div>\n' +
		'</div>\n ' +
		'@endforeach';
	$('#showExperimental').append(html);
}
automica's avatar

@oxbir

considering your relationship is:

public function skills()
{
    return $this->belongsToMany(Skill::class);
}

surely you want to do:

$user->skills()->detach();
if ($request->skill_id) {
    foreach($request->skill_id as $key => $social){
        $user->skills()->attach($social, ['skill_id' => $request->link[$key]]);
    }
}

i'm not sure about this working:

$user->skills()->attach($social, ['skill_id' => $request->link[$key]]);

working though. what are you referencing $request->link? surely it should be $request->level?

is this just some copy + paste issue? and you want to do:

$user->skills()->detach();
if ($request->skill_id) {
    foreach($request->skill_id as $key => $skill){
        $user->skills()->attach($skill, ['skill_level' => $request->level[$key]]);
    }
}

and your relationship should add the pivot column

public function skills()
{
    return $this->belongsToMany(Skill::class)->withPivot('skill_level');
}
shahr's avatar
Level 10

@automica

I see this error.

error

showDynamicExperimental();
function showDynamicExperimental()
{
    let html = '' +
        '@foreach(auth()->user()->skills as $skill)\n'+
automica's avatar

@oxbir ok. so you just want to do a normal join.

change to:

$user->skills()->detach();
if ($request->skill_id) {
    foreach($request->skill_id as $key => $skill){
        $user->skills()->attach($skill, ['skill_id' => $request->level[$key]]);
    }
}

and change your relationship back to:

public function skills()
{
    return $this->belongsToMany(Skill::class);
}
automica's avatar

@oxbir you don't see the error again. its a different error to last time.

what are you trying to save on the skill_user relationship?

I would suspect you want

user_id| skill_id | level ?

so you can have a user with multiple skills and each skill has 1 level?

shahr's avatar
Level 10

What is the solution?

And

What am I doing?

automica's avatar

@oxbir

in that case, you need to add a column called 'level' on your pivot table.

and we're back to doing this:

$user->skills()->detach();
if ($request->skill_id) {
    foreach($request->skill_id as $key => $skill){
        $user->skills()->attach($skill, ['level' => $request->level[$key]]);
    }
}

and this:

public function skills()
{
    return $this->belongsToMany(Skill::class)->withPivot('level');
}
automica's avatar

@oxbir we're only talking about one pivot table aren't we? have a guess?

automica's avatar

@oxbir its a different error to last time.

can you copy the actually error? The bit in the SQLSTATE box?

when you have done that, can you place a

dd($request->skill_id, $request->level);

in your controller above

$user->skills()->detach();

so I can see what you are trying to save?

shahr's avatar
Level 10

I get this message

array:2 [▼
  0 => "laravel"
  1 => "php"
]

array:2 [▼
  0 => "3"
  1 => "5"
 ]
automica's avatar

@oxbir you have this:

<input type="text" id="skill_id" name="skill_id[]" class="form-control" value="{{ $skill->skill_name }}">

which is wrong.

if you need want to store the skill_id, the value needs to be

<input type="text" id="skill_id" name="skill_id[]" class="form-control" value="{{ $skill->skill_id }}">
			

actually, you are setting the skill in an editable field. but this looks like you are listing a series of predefined skills and the you want the user to set a level for them.

in that case, I would guess you only want to display the list of skills as uneditable, so should set the skill_id as hidden field

	'<label for="skill_id">skill_name</label>\n' +
	'<input type="readonly"  class="form-control" value="{{ $skill->skill_name }}">\n' +
	'<input type="hidden" id="skill_id" name="skill_id[]" value="{{ $skill->skill_id }}">\n' +
shahr's avatar
Level 10

@automica

Nowhere my blade, there is no skill_name. only there is skill_id

automica's avatar

@oxbir I copied that from JavaScript block that you posted further up the thread....

check your

function showDynamicExperimental()

method.

automica's avatar

according to your form you are allow users to create new skills and then assign a level to them.

that looks like the fields in your skills table and you have a user_id in there.

in that case, what is your skill_user table for? is that a mistake?

automica's avatar

@oxbir my next question is why are you creating a JavaScript function that outputs a html string into your blade?

this:

showDynamicExperimental();
function showDynamicExperimental()
{
	let html = '' +
		'@foreach(auth()->user()->skills as $skill)\n'+
		'<div class="col-md-6 position-relative">\n' +
		'<i class="fas fa-times text-danger position-absolute"></i>' +
		'<div class="row">\n' +
		'<div class="col-md-8">\n' +
		'<div class="form-group">\n' +
		'<label for="skill_id">skill_name</label>\n' +
		'<input type="text" id="skill_id" name="skill_id[]" class="form-control" value="{{ $skill->skill_name }}">\n' +
		'</div>\n' +
		'</div>\n' +
		'<div class="col-md-4">\n' +
		'<div class="form-group">\n' +
		'<label for="level">level</label>\n' +
		'<select class="form-control" id="level" name="level[]">\n' +
		'<option value="1" {{ $skill->level == 1 ? 'selected' : '' }}>★☆☆☆☆</option>\n'+
		'<option value="2" {{ $skill->level == 2 ? 'selected' : '' }}>★★☆☆☆</option>\n'+
		'<option value="3" {{ $skill->level == 3 ? 'selected' : '' }}>★★★☆☆</option>\n'+
		'<option value="4" {{ $skill->level == 4 ? 'selected' : '' }}>★★★★☆</option>\n'+
		'<option value="5" {{ $skill->level == 5 ? 'selected' : '' }}>★★★★★</option>\n'+
		'</select>\n'+
		'</div>\n' +
		'</div>\n' +
		'</div>\n' +
		'</div>\n ' +
		'@endforeach';
	$('#showExperimental').append(html);
}

would normally be done by just adding the following:

@foreach(auth()->user()->skills as $skill)
    <div class="col-md-6 position-relative">
        <i class="fas fa-times text-danger position-absolute"></i>
        <div class="row">
            <div class="col-md-8">
                <div class="form-group">
                    <label for="skill_id">skill_name</label>
	<label for="skill_id">skill_name</label>\n' +
	<input type="readonly"  class="form-control" value="{{ $skill->skill_name }}">\n' +
	'<input type="hidden" id="skill_id" name="skill_id[]" value="{{ $skill->skill_id }}">\n' +

                    <input type="text" id="skill_id" name="skill_id[]" class="form-control"
                           value="{{ $skill->skill_name }}">
                </div>
            </div>
            <div class="col-md-4">
                <div class="form-group">
                    <label for="level">level</label>
                    <select class="form-control" id="level" name="level[]">
                        <option value="1" {{ $skill->level == 1 ? 'selected' : '' }}>★☆☆☆☆</option>
                        <option value="2" {{ $skill->level == 2 ? 'selected' : '' }}>★★☆☆☆</option>
                        <option value="3" {{ $skill->level == 3 ? 'selected' : '' }}>★★★☆☆</option>
                        <option value="4" {{ $skill->level == 4 ? 'selected' : '' }}>★★★★☆</option>
                        <option value="5" {{ $skill->level == 5 ? 'selected' : '' }}>★★★★★</option>
                    </select>
                </div>
            </div>
        </div>
    </div>
@endforeach

inside your #showExperimental div.

automica's avatar

@oxbir please answer this question...

according to your form you are allow users to create new skills and then assign a level to them.

that looks like the fields in your skills table and you have a user_id in there.

in that case, what is your skill_user table for? is that a mistake?

if infact you want skills to be a list that many users can have, you cant allow a user to manually type a skill in to an input field.

They have to select the skill from a dropdown (which will get you its skill_id) and then you can select a level too.

shahr's avatar
Level 10

@automica

When after save , I see in table of skill_user table,

[![table][1]][1]
shahr's avatar
Level 10

@automica

After login a user, The user enters this skills form, selects the skills.

skills

A user can enter multiple skills, and save in database.

The field of name

  • skill_name
  • level

And in table

  • skill_name
  • level
  • user_id

I don't know if the skills and skill_user table should be, or not, I don't know.

What would you do if you were me?

Can you show me the cleanest coding?

shahr's avatar
Level 10

Can you show me the cleanest coding?

MichalOravec's avatar

What the hell?!

Answer my question.

I hope no one will...

2 likes
shahr's avatar
Level 10

You go get lost. I wasn't with you.

automica's avatar

Ha.

I am a sucker for this...

So @oxbir explain to me what you are using the skills data for?

is this list of skills and levels just for a user to make a cv?

or are you planning on using this data in the back end report so you can find a list of users that have a particular skill (eg laravel) and level eg basic)?

You seem to be unclear about what the relationship is for the user and skills

You have skills table which has skill_name, level and user_id

Relationship

  • User hasMany Skill
  • Skill beingsTo User

And a skill_user table which suggests you are trying to have

  • User belongsToMany SkIll

Your UI suggests you want to just store data in your skills table but you are writing code to store on the belongsToMany relationship.

So tell me which.

If you don’t know, then hire someone to help you to do this.

Starting to write code without proper planning is a waste of everyone’s time.

You’ve currently only watched 3 since you set up this user account which means you’ve not watched the eloquent lessons and that’s why you are having some much trouble and ask so many questions.

Given laracasts is half price at the moment why not invest some of your time and money and watch some lessons?

1 like

Please or to participate in this conversation.