2017-08-09 50 views
0

我的數據庫中充滿了數據並顯示了所有數據。但是如果我試圖只顯示一個特定的線程,我會得到一個空的結果,我不知道爲什麼。Laravel - 試圖顯示特定線程的空結果

web.php

<?php 

Route::get('/', function() { 
    return view('welcome'); 
}); 


Auth::routes(); 

Route::get('/home', '[email protected]')->name('home'); 
Route::get('/threads', '[email protected]'); 
Route::get('/threads/{threads}', '[email protected]'); 

ThreadsController @指數的作品,但ThreadsController @秀不起作用。

ThreadsController.php

<?php 

namespace App\Http\Controllers; 

use App\Thread; 
use Illuminate\Http\Request; 

class ThreadsController extends Controller 
{ 
    /** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     $threads = Thread::latest()->get(); 
     return view('threads.index', compact('threads')); 
     //return $threads; 
    } 

    /** 
    * Show the form for creating a new resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function create() 
    { 
     // 
    } 

    /** 
    * Store a newly created resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function store(Request $request) 
    { 
     // 
    } 

    /** 
    * Display the specified resource. 
    * 
    * @param \App\Thread $thread 
    * @return \Illuminate\Http\Response 
    */ 
    public function show(Thread $thread) 
    { 
     //return view('threads.show', compact('thread')); 
     return $thread; 
    } 

    /** 
    * Show the form for editing the specified resource. 
    * 
    * @param \App\Thread $thread 
    * @return \Illuminate\Http\Response 
    */ 
    public function edit(Thread $thread) 
    { 
     // 
    } 

    /** 
    * Update the specified resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param \App\Thread $thread 
    * @return \Illuminate\Http\Response 
    */ 
    public function update(Request $request, Thread $thread) 
    { 
     // 
    } 

    /** 
    * Remove the specified resource from storage. 
    * 
    * @param \App\Thread $thread 
    * @return \Illuminate\Http\Response 
    */ 
    public function destroy(Thread $thread) 
    { 
     // 
    } 
} 

這是laracast教程的例子:link

爲什麼這個例子沒有工作?我在代碼中找不到任何拼寫錯誤。 我的版本:

php artisan --version 
Laravel Framework 5.4.32 

回答

1

重命名你的節目路線如下:

Route::get('/threads/{thread}', '[email protected]'); 

您正在載入Thread對象,而不是一個Threads對象。

+0

根據本教程'Route :: get('/ threads/{id}','ThreadsController @ show');'應該是相同的,但我通過{id}再次得到一個空結果。你能解釋一下爲什麼{id}不起作用嗎? –

+1

部件名稱應該與函數中的類型提示變量相同。 [如文檔中所述](https://laravel.com/docs/5.4/routing#implicit-binding)。 – Jerodev