2

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

4
  • Trouble ?? What Trouble ? Commented May 31, 2015 at 9:23
  • Have you tried return View::make('projects.show')->with(compact('project'))->with(compact('technologies')); ? Commented May 31, 2015 at 9:26
  • Don't you need to call ->get() on the query? Commented May 31, 2015 at 9:30
  • Thanks for the suggestion but I still get a totally blank page in response even when looking at the source. Commented Jun 3, 2015 at 12:42

2 Answers 2

3

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/

Sign up to request clarification or add additional context in comments.

9 Comments

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 - 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?
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');'
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.
I'll check into it. I get errors sometimes and when I do they are usually long ... : ) thanks again
|
0

rewrite your query as,

$technologies = DB::table('technologies')->where('id',$project->technology_id)->get();

1 Comment

Thanks for the suggestion but I still get a totally blank page in response even when looking at the source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.