我的数据库充满了数据并显示了所有数据的工作原理。但是如果我只尝试显示一个特定的线程,我得到一个空的结果,我无法找出原因。
web.php
<?php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::get('/threads', 'ThreadsController@index');
Route::get('/threads/{threads}', 'ThreadsController@show');
ThreadsController @ index有效,但ThreadsController @ show不起作用。
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
答案 0 :(得分:1)
将您的节目路线重命名为以下内容:
Route::get('/threads/{thread}', 'ThreadsController@show');
您正在加载Thread
对象,而不是Threads
对象。