Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Attempt to read property "color_name" on bool

I want to display the first color of the product.

Product.php

public function colors()
{
    return $this->belongsToMany(Color::class);
}

blade

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@if($product->colors->count() > 0)
    @foreach($product->colors->first() as $color)
        <div data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="{{ $color->color_name }}">
            <input type="radio" id="color-{{ $color->id }}" name="color" value="color-{{ $color->id }}">
            <label for="color-{{ $color->id }}">
                <span>
                    <img src="{{ asset('themes/images/check.svg') }}" alt="{{ $color->color_name }}">
                </span>
            </label>
        </div>
    @endforeach
@endif

>Solution :

$product->colors->first() is an instance of Color model. So $color in @foreach loop is a property of Color model. If you want to display first color, do the below:

@if($product->colors->count() > 0)
    @php $color = $product->colors->first(); @endphp
    <div data-bs-toggle="tooltip" data-bs-placement="bottom" data-bs-title="{{ $color->color_name }}">
        <input type="radio" id="color-{{ $color->id }}" name="color" value="color-{{ $color->id }}">
        <label for="color-{{ $color->id }}">
            <span>
                <img src="{{ asset('themes/images/check.svg') }}" alt="{{ $color->color_name }}">
            </span>
        </label>
    </div>
@endif
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading