2014-03-01 48 views
1

我刚开始使用Laravel,并试图从数据库中检索值并试图通过路由器在视图上输出它们时卡住了。这里是我的代码在路由器:使用刀模板鉴于Laravel:无法将数组从数据库从路由器传递到视图

Route::get('/', function(){ 
    $posts = DB::select("select title from posts"); 
    return View::make("hello", $posts); 
}); 

代码:

@extends('master') 

@section('title') 
    @foreach ($posts as $post) { 
     {{ $post->title }} 
    @endforeach 
@endsection 

我试图检查查询,它运行完美,但我得到的观看页面上的未定义的变量错误。

enter image description here

它是什么,我做错了,我该如何改正呢?请帮助..

+0

Give this Before。 $ posts = DB :: table('posts') - > select('title') - > get(); –

+0

错误已更改,但仍无法正常工作。 http://puu.sh/7eKGI.png – Gaurav

回答

1

试试这个:

$posts = DB::select(DB::raw('SELECT title FROM posts')); 

或:

$posts = DB::table('posts')->select('title')->get(); 

并返回查看:

return View::make('hello', ['posts' => $posts]); 

您需要发送给之前分配$posts给一个变量风景。在上面,变量是posts

+0

无论如何查询结果是一样的;但返回的观点正在解决所有问题。非常感谢帮助,现在完美! :d – Gaurav

相关问题