我试图在Laravel中学习一门课程,并且尝试建立一个简单的CRUD方法,但我被卡在Create上。我在Laravel中有一个错误,我不明白如何解决,我试图清除路由缓存,但是不起作用。我看到了一些与此错误有关的答案,但我不确定它们是否匹配,因为我在Web文件中使用post方法。 Snippet
控制器
namespace App\Http\Controllers;
use App\Todo;
use Illuminate\Http\Request;
class TodosControler extends Controller
{
public function index()
{
return view('todos.index')->with('todos', Todo::all());
}
//za pojedine
public function show($todoId)
{
//dd($todoId); //diedump isto kao die u PHPu
// $todo = Todo::find($todoId); onda ovo kad slozimo samo stavimo u return
return view('todos.show')->with('todo', Todo::find($todoId));
}
public function create()
{
return view('todos.create');
}
public function store()
{
$data = request()->all();
$todo = new Todo();
$todo->name = $data['name'];
$todo->description = $data['description'];
$todo->completed = false;
$todo->save();
return redirect('/todos');
}
}
网络文件
use App\Http\Controllers\AboutController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('todos' , 'TodosControler@index');
Route::get('todos/{todo}' , 'TodosControler@show');
Route::get('new-todos' , 'TodosControler@create');
Route::post('store-todos' , 'TodosControler@store');
表格
<form action="/store-todos" method="POST">
@csrf
<div class="form-group">
<input type="text" class="form-control" name="name" placeholder="Name">
</div>
<div class="form-group">
<textarea name="description" cols="5" rows="5" class="form-control" placeholder="description"></textarea>
</div>
<div class="form-group text-center">
<button type="submit" class="btn btn-success">Create todo</button>
</div>
</form>
答案 0 :(得分:1)
我认为这是错误Route::get('new-todos' , 'TodosControler@create')
。
也许您需要Route::post('new-todos' , 'TodosControler@create')
希望有帮助。