为什么我的查询不在控制器中执行?

Ged*_*nas 0 laravel

我正试图items从表中获取CategoriesController.php,但我在Laravel(5.3)Debugbar中看到,我的查询未执行.为什么?这是我的代码:

# Http/Controllers/CategoriesController.php

use App\Category;
use App\Item;
use App\Http\Requests;
use App\Http\Controllers\Controller;

namespace App\Http\Controllers;

use Request;

class CategoriesController extends Controller {

  public function show($id) {

    $items = \App\Item::where('id', $id); # <- This is not executed!

    $category = \App\Category::find($id);

    return view('categories.show', compact('category', 'items'));

  }

}
Run Code Online (Sandbox Code Playgroud)

Ohg*_*why 5

::where() 正在链接查询构建器,但是您永远不会执行请求.

::where('id', $id)->first(); //if you only want the first result
//shorthand would be ::find($id);
Run Code Online (Sandbox Code Playgroud)

或者,如果你想要每场比赛:

::where('id', $id)->get(); 
Run Code Online (Sandbox Code Playgroud)