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

Bosstone's avatar

Fetch Data in Blade from Eloquent?

Hi Folks,

I am new to laravel and I am stuck at this problem:

I have 3 tables:

User: id, name Projects: id, name, user_id Images: id, name, project_id

Using Eloquent I can fetch any projects connected to a user and the images connecting to the project:

public function ProjectShow () {

  $user = Auth::user();

  $projects = Project::with('images')
                ->whereHas('user', function ($query) use ($user) {
                    $query->where('id', '=', $user->id);
                })->get();

 return view('project-show', get_defined_vars())->with(['user' => $user]);

}

When I "return $projects;" then I will get this result:

{"id":73,"user_id":1,"name":"dfds","projektname":"1527685835","cat_id":1,"beschreibung":null,"youtube":null,"copyright":null,"testimonial":null,"check":0,"ort":null,"stat":0,"created_at":null,"updated_at":null,"deleted_at":null,"images":[{"id":4,"project_id":73,"filename":"15276858427.png","url":"/Users/sl/Developer/awa/public/images/1/1/15276858427.png","filesize":null,"created_at":"2018-05-30 13:10:42","updated_at":"2018-05-30 13:10:42","deleted_at":null},{"id":5,"project_id":73,"filename":"15276858428.png","url":"/Users/sl/Developer/awa/public/images/1/1/15276858428.png","filesize":null,"created_at":"2018-05-30 13:10:42","updated_at":"2018-05-30 13:10:42","deleted_at":null},{"id":6,"project_id":73,"filename":"152768584224Alle.png","url":"/Users/sl/Developer/awa/public/images/1/1/152768584224Alle.png","filesize":null,"created_at":"2018-05-30 13:10:42","updated_at":"2018-05-30 13:10:42","deleted_at":null},

Where id:73 is the ID from projects and id:4 is the connected Images ID

When I remove the "return $projects;" then I am not able to fetch the $projects Variable in Blade:

The Blade:

@extends('layouts.app')

@section('content')

@foreach ($projects as $project)

<p>This is Project Name:  {{ $project->Name }}</p>
<p>This is Project FotoURL:  {{ $project->url }}</p>
<p>--------</p>

@endforeach

@endsection

Error: No Error, but the variables are empty...

Thank you for any feedback!

Kind Regards,

Stefan

0 likes
4 replies
tykus's avatar

I would be explicit about the variables you are passing to the view:

return view('project-show', compact('projects', 'user'));

Also, are you looking only for the authenticated user's projects? You can scope the query using the authenticated user as follows:

$projects = auth()->user()->projects()->with('images')->get();

This will depend on having a hasMany relation on the User model:

public function projects()
{
    return $this->hasMany(Project::class);
}
2 likes
himanshu-dhiman's avatar

Looping statement would be :

@foreach($projects->images as image)

<p>This is Project Name:  {{ $projects->name }}</p>
<p>This is Project FotoURL:  {{ $image->url }}</p>
<p>--------</p>

@endforeach 

Try if this works.

1 like
tykus's avatar
tykus
Best Answer
Level 104

If you wanted to display only the first image as the project image, you could do the following:

@foreach($projects as $project)
    <p>This is Project Name:  {{ $project->name }}</p>
    <p>This is Project FotoURL:  {{ optional($project->images->first())->url }}</p>
    <p>--------</p>
@endforeach 

Otherwise, you would need two loops (nested), first for the projects, and secondly for the project images:

@foreach($projects as $project)
    <p>This is Project Name:  {{ $project->name }}</p>

    @foreach($project->images as $image)
        <p>This is Project FotoURL:  {{ $image->url }}</p>
    @endforeach
@endforeach     
1 like
Bosstone's avatar

Dear Tykus & himanshu-dhiman

thank you very much for your feedback. I am very impressed how fast you were and what I am missing...

Kind Regards,

Stefan

Please or to participate in this conversation.