当我转到“/todos 和/todos/id ”时,我在 laravel 中的路线遇到问题,一切正常,但是当我尝试使用“/todos/create”时,我得到模型的“无查询结果”[待办事项列表] 我是新手,请帮助我...我真的不想放弃,因为我真的很喜欢这个 mvc
这是我的路线
Route::get('/', 'TodoListController@index');
Route::get('todos', 'TodoListController@index');
Route::get('/todos/{id}', 'TodoListController@show');
Route::get('db', function() {
$result = DB::table('todo_lists')->where('name', 'Your List')->first();
return $result->name;
});
Route::resource('todos', 'TodoListController');
型号
<?php
class TodoList extends Eloquent {}
Controller
public function index()
{
$todo_lists = TodoList::all();
return View::make('todos.index')->with('todo_lists', $todo_lists);
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$list = new TodoList();
$list->name = "another list";
$list->save();
return "I am here by accident";
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$list = TodoList::findOrFail($id);
return View::make('todos.show')->withList($list);
}
我的看法
@extends('layouts.main') @section('content')
<h1>All todo list</h1>
<ul>
@foreach ($todo_lists as $list)
<li>{{{ $list->name }}}</li>
@endforeach
</ul>
@stop
最佳答案
问题在于您为 /todos/{id}
定义的显式路由。由于此路由是在 resource
路由之前定义的,因此它捕获 todos/create
的路由,并将文本 create
视为
参数。 show
方法的 >{id}
删除 todos
和 /todos/create
的显式 get
路由,您的问题将得到解决。这两个路由均由resource
路由处理。
关于php - 在 Laravel 中,为什么我得到模型 [TodoList] 的“无查询结果”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30407865/