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

shahr's avatar
Level 10

Attempt to read property "id" on bool

I want to show the price on the edit page but I see this error:

Attempt to read property "id" on bool

Controller

public function edit(Product $product, Warranty  $warranty, Type $type)
{
    return view('Admin.types.edit', compact('product', 'warranty', 'type'));
}

Type.php

public function color()
{
    return $this->belongsTo(Color::class);
}

edit.blade.php

@foreach($type->color as $color)
    <div class="mb-3 row">
        <label for="color{{ $color->id }}" class="col-sm-2 col-form-label">{{ $color->color_name }}</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" id="color{{ $color->id }}" name="color[{{ $color->id }}]" value="{{ $type->price }}">
        </div>
    </div>
@endforeach
0 likes
3 replies
LaryAI's avatar
Level 58

The error "Attempt to read property 'id' on bool" means that you are trying to access the 'id' property on a boolean value (true/false). In this case, it is likely that the $type->color relationship is returning a boolean value instead of a collection of colors.

To fix this, you should check if the $type->color relationship is returning a collection before looping through it in the blade template. You can do this using the 'instanceof' operator to check if the value is an instance of the Illuminate\Database\Eloquent\Collection class.

Here's an updated version of the edit.blade.php file that includes this check:

@if ($type->color instanceof Illuminate\Database\Eloquent\Collection)
    @foreach($type->color as $color)
        <div class="mb-3 row">
            <label for="color{{ $color->id }}" class="col-sm-2 col-form-label">{{ $color->color_name }}</label>
            <div class="col-sm-6">
                <input type="text" class="form-control" id="color{{ $color->id }}" name="color[{{ $color->id }}]" value="{{ $type->price }}">
            </div>
        </div>
    @endforeach
@endif

This will only loop through the $type->color relationship if it is an instance of the Collection class, which should prevent the "Attempt to read property 'id' on bool" error from occurring.

MohamedTammam's avatar

It's one to many, you don't need foreach for that

    <div class="mb-3 row">
        <label for="color{{ $type->color->id }}" class="col-sm-2 col-form-label">{{ $type->color->color_name }}</label>
        <div class="col-sm-6">
            <input type="text" class="form-control" id="color{{ $type->color->id }}" name="color[{{ $type->color->id }}]" value="{{ $type->price }}">
        </div>
    </div>

Please or to participate in this conversation.