在laravel

时间:2017-07-21 13:15:09

标签: laravel laravel-5

我无法弄清楚我的创建视图有什么问题。我需要提交按钮将我重定向到我的节目视图,以便我可以看到所有创建的标签。 以下是创建标记,显示标记然后存储标记的路径:

Route::get('/tags/create', ['uses' => 'TagController@create', 'as' => 'tags.create']); //  Allows you to create a new tag

Route::post('/postTags', ['uses' => 'TagController@store', 'as' => 'postTags']); //    Place where all the tags are stored

Route::get('/tag/show', ['uses' => 'TagController@show', 'as' => 'tags.show']); //  Shows you all the existing tags

这是我的标签控制器:

public function create()
{
    $tag = new Tag();
    return view('tags.create', compact('tag'));
}

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
    ]);

    $tag = new Tag;
    $tag->name = $request['name'];
    $tag->save();

    return redirect()->route("tags.show");
}
public function show()
{
    $tags = Tag::all();
    return view('tags.show', compact('tags'));
}

我的创建视图:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>New Tag</h1>
<form method="POST" action="{{route("postTags")}}">

    {{csrf_field()}}

    <label for="name">Click here to edit the title of your post!</label>
    <input type="text" name="Name" id="name" required/>
    <input type="submit" value="Submit" onclick="window.location='{{ route("tags.show") }}'"/>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:5)

这是错误,你给了name="Name",第一个字母是大写,而且功能都很小

public function store(Request $request)
{
    $this->validate($request, [
        'name' => 'required',
    ]);

    $tag = new Tag;
    $tag->name = $request['name'];
    $tag->save();

    return redirect()->route("tags.show");
}

您也在上面的函数中创建对象而不调用构造函数$tag = new Tag;,将其创建为$tag = new Tag();