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

lilo's avatar
Level 2

Trying to get property 'url' of non-object

Hi,

I want to display image which is in my storage field. I want to retrieve this from the database but I get this error:

ErrorException Trying to get property 'url' of non-object

Here is my blade:

<div class="container clearfix">
            <div id="oc-clients" class="owl-carousel owl-carousel-full image-carousel carousel-widget" data-margin="30" data-loop="true" data-nav="false" data-autoplay="5000" data-pagi="false" data-items-xs="2" data-items-sm="3" data-items-md="4" data-items-lg="5" data-items-xl="6" style="padding: 20px 0;">

                @foreach($reference as $ref)
                    <div class="oc-item">
                        <a href="{{ $ref->link }}" target="_blank">
                            <img src="{{ $ref->logo->url }}">
                        </a>
                    </div>
                @endforeach

            </div>
</div>

Also here is my controller:

public function show($link)
{
    $reference =DB::table('references')->where('link',$link)->first();

    if($reference){
        abort(404);
    }
    
}

And my route:

Route::get('/', function () {
$response= Http::get('https://....../Services/publicapi/getcasecount');
$data = $response->json();
$featuredbox=App\FeaturedBox::all();
$slider=App\Slider::all();
$reference=App\Reference::all();
return view('index',['data'=>$data, 'featuredbox'=>$featuredbox, 'slider'=>$slider, 'reference'=>$reference]);});

How can I access the image truely? Thanks in advance

0 likes
13 replies
Sinnbeck's avatar

You cannot load relations when using DB. You need eloquent

    $reference = Reference::with('logo')->where('link',$link)->first();
Sinnbeck's avatar

And in the foreach here you should check if there is a logo

 @foreach($reference as $ref)
                    <div class="oc-item">
                         @if($ref->link)
                        <a href="{{ $ref->link }}" target="_blank">
                            <img src="{{ $ref->logo->url }}">
                        </a>
                        @endif
                    </div>
                @endforeach

Or only load references that have a logo

$reference=App\Reference::has('logo')->get();
lilo's avatar
Level 2

The error doesn't change unfortunately. Actually there is this crud named "Reference" and I have two columns in it which are logo and the link. I want to display them in index page as carousel items. When the user clicking on the logo, it must directed to the link of this logo. I made the carouel item and the links but can't retrieve the logos dynamically. I made the changes as you say in my controller and blade but nothing changed.

Sinnbeck's avatar

So the link isnt a relationship? There are just different columns?

 @foreach($reference as $ref)
                    <div class="oc-item">
                         @if($ref->link)
                        <a href="{{ $ref->link }}" target="_blank">
                            <img src="{{ $ref->logo }}">
                        </a>
                        @endif
                    </div>
@endforeach
lilo's avatar
Level 2

Yes they are just different columns in the same table

But with this code above, only the name of the image is displayed not the image itself.

Sinnbeck's avatar

So something like this (edit the path to the correct one if it isnt 'logos/')

@foreach($reference as $ref)
                    <div class="oc-item">
                         @if($ref->link)
                        <a href="{{ $ref->link }}" target="_blank">
                            <img src="{{ asset('logos/' . $ref->logo) }}">
                        </a>
                        @endif
                    </div>
@endforeach
lilo's avatar
Level 2

Still not working... I'm retrieving the data from db but also I upload the images into db through a panel. The last status of the code is given below:

My blade:

@foreach($reference as $ref)
                    <div class="oc-item">
                        @if($ref->link)
                            <a href="{{ $ref->link }}" target="_blank">
                                <img src="{{ env('APP_URL'). $ref->logo }}">
                            </a>
                        @endif
                    </div>
                @endforeach

My controller:

public function show($link)
{
    $reference =DB::table('references')->where('link',$link)->first();

    if($reference){
        abort(404);
    }
}

public function update(Request $request)
{
    $path = $request->file('logo');
    return $path;
}
Sinnbeck's avatar

Dont use env outside of config files!

You can use the url helper if you what instead

@foreach($reference as $ref)
                    <div class="oc-item">
                        @if($ref->link)
                            <a href="{{ $ref->link }}" target="_blank">
                                <img src="{{ url($ref->logo) }}">
                            </a>
                        @endif
                    </div>
                @endforeach
lilo's avatar
Level 2

When I use like given below, it start to give the same error which is "Trying to get property 'url' of non-object"

@foreach($reference as $ref)
                    <div class="oc-item">
                        @if($ref->link)
                            <a href="{{ $ref->link }}" target="_blank">
                                <img src="{{ $ref->logo->url }}">
                            </a>
                        @endif
                    </div>
                @endforeach
Sinnbeck's avatar

Yes.. Look at my code.. i use url() not ->url

lilo's avatar
Level 2

Ah yes.. So sorry but the problem continues. It gives " htmlspecialchars() expects parameter 1 to be string, object given "

Sinnbeck's avatar

Ok back to the start. You said that logo was a column, but now it's a relation?

If so, I have you the answer using @if several answers back

Or do you have an accessor?

lilo's avatar
Level 2

I made the panel using quick admin panel. In my Reference table there is two columns which are logo and link. The field type of the logo column is photo. And when I look at the db after doing migrations, there is a media table in which all the photo type images is stored. In previous projects, when I want to access the logo, I was able to access it using {{ $somevar->photo->url }} but when I do it in this projecti there is error.

Also, could this error have something to do with the logo's path? Because when I look at the panel and click on the photo that I upload I get 404 Error.

I hope I could explain and thanks for your help

Please or to participate in this conversation.