Laravel 5 passing database query from controller to view
10,780
controller
public function show(Project $project)
{
$technologies = DB::table('technologies')
->select('*')
->where('id', $project->technology_id)
->get(); // you were missing the get method
return view('projects.show', compact('project', 'technologies'));
}
View
This will not work in your view: @if ( !technologies() )
@section('content')
<h2>{{ $project->name }}</h2>
@if($technologies)
<ul>
@foreach($technologies as $technology)
<li><a href="#">{{ $technology->slug }}</a></li>
@endforeach
</ul>
@else
<p>no technologies.</p>
@endif
@stop
Also, if you're interested in a cleaner way to loop through your data or show a 'no data' warning using blade check out this article which uses blade's @each https://laravel-news.com/2014/09/laravel-blade/
![Scudder Stevens](https://i.stack.imgur.com/0Nicq.jpg?s=256&g=1)
Author by
Scudder Stevens
Updated on June 04, 2022Comments
-
Scudder Stevens about 2 years
I'm new to Laravel 5 and MVC frameworks in general. I having trouble getting results from a database query and passing it to my blade view. Here is what I have for a controller function ...
public function show(Project $project) { $technologies = DB::select('select * from technologies where id = ?', [$project->technology_id]); return view('projects.show', compact('project','technologies')); }
and my view is ...
@section('content') <h2>{{ $project->name }}</h2> @if ( !technologies() ) no technologies. @else <ul> @foreach( $technologies as $technology ) <li><a href="#">{{ $technology->slug }}</a></li> @endforeach </ul> @endif @endsection
Thanks for your help
-
Scudder Stevens about 9 yearsThanks for the helpful suggestions but I'm still getting a blank page in response. The correct url just nothing, even when I look at the source code. I'll double check that my DB seeds are working ... ? Maybe the view is the problem?
-
Scudder Stevens about 9 yearsThanks for the suggestion but I still get a totally blank page in response even when looking at the source.
-
haakym about 9 years@Scudder Stevens - How is your route for the url set up in routes.php? Also, have you checked your error log after visiting the URL? You could also just remove everything from the view and try outputting
<p>hello</p>
just to check it's returning the view perhaps? -
Scudder Stevens about 9 yearsroutes.php = 'Route::model('projects', 'Project'); Route::model('technologies', 'Technology'); Route::bind('projects', function($value, $route) { return App\Project::whereSlug($value)->first(); }); Route::resource('projects', 'ProjectsController');'
-
Scudder Stevens about 9 yearswhen I remove the $technologies = DB call from the controller and the for each loop from the view, leaving 'return view('projects.show', compact('project', 'technologies')); and <h2>{{ $project->name }}</h2>' I do get the project name. When I add the DB call back in to the controller it returns a blank page again.
-
haakym about 9 yearsYour file is saved as
.blade.php
right? Other things I can think of ... Can you try this after your query and before thereturn view(...
dd($technologies) and see if it spits anything out. Or try adding a backslash before DB as so:\DB::table('technologies')
or at the top of your file putuse DB
. Hope that gets you somewhere -
Scudder Stevens about 9 yearsAnd the Londoner has the Win! Thanks for your help and persistence haakym, when I added "use DB;" to the Controller everything snapped into place. Cheers!
-
haakym about 9 yearsLol! That comment put a smile on my face haha, was well worth it for that! Glad you got it working buddy. Happy coding.
-
haakym about 9 yearsJust a side point - it's very odd you weren't getting any errors such as no such class as
App\Controller\DB
just something to keep in mind if the blank pages come again otherwise you're going to have a very hard time debugging. -
Scudder Stevens about 9 yearsI'll check into it. I get errors sometimes and when I do they are usually long ... : ) thanks again