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/

Share:
10,780
Scudder Stevens
Author by

Scudder Stevens

Updated on June 04, 2022

Comments

  • Scudder Stevens
    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
    Scudder Stevens about 9 years
    Thanks 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
    Scudder Stevens about 9 years
    Thanks for the suggestion but I still get a totally blank page in response even when looking at the source.
  • haakym
    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
    Scudder Stevens about 9 years
    routes.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
    Scudder Stevens about 9 years
    when 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
    haakym about 9 years
    Your file is saved as .blade.php right? Other things I can think of ... Can you try this after your query and before the return 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 put use DB. Hope that gets you somewhere
  • Scudder Stevens
    Scudder Stevens about 9 years
    And 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
    haakym about 9 years
    Lol! That comment put a smile on my face haha, was well worth it for that! Glad you got it working buddy. Happy coding.
  • haakym
    haakym about 9 years
    Just 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
    Scudder Stevens about 9 years
    I'll check into it. I get errors sometimes and when I do they are usually long ... : ) thanks again