简体   繁体   English

如何在Laravel中为表单标签绑定路由URL

[英]How to bind a route url for form tag in laravel

This is my form tag 这是我的表格标签

    <form method="POST" action="{{ url("/post/{$article->id}/comment") }}">> 

This is my route 这是我的路线

    Route::post('/post/{article}/comment', 'CommentController@store');

This is my commentcontroller method 这是我的commentcontroller方法

    public function store(Article $article)
        {
             $comment = $article->comment->create([
            'body' => request('body'),
            ]);
            return back();
        }

show.blade.php show.blade.php

`@extends('master')
@section('content')
<!-- Example row of columns -->
<div class="container">
    <h1> {{ $articles->title }} </h1>
    <p> {{ $articles->body }} </p>
    <hr></hr>
    <div class="comment">
        <ul class="list-group"> 
            @foreach($articles->comments as $comment)
            <li class="list-group-item">
                <strong>
                    {{ $comment->created_at->diffForHumans()}} : &nbsp;
                </strong>
                {{ $comment->body }}
            </li>
            @endforeach
        </ul>
    </div>
    <!-- Here is comment form -->
    <hr>
    <div class="card">
        <div class="card-block">
            <form method="POST" action="{{ url ("/post/{$article->id}/comment") }}">> 
                {{ csrf_field() }}
                <div class="form-group">
                    <textarea name="body" placeholder="your comment here." class="form-control">  </textarea>

                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">Add Comment</button>
                </div>
            </form>
        </div>
        @include('partials.errors')
    </div>
</div>
@endsection`

When I'm trying to add comment on article I'm getting an error like this: 当我尝试对文章添加评论时,出现如下错误:

> Undefined variable: article (View: C:\xampp7\htdocs\Lara\resources\views\article\show.blade.php)

Anything wrong in this? 这有什么问题吗? help me out. 帮帮我。 Thanks in advance 提前致谢

It seems you are not passing $article to view. 看来您没有通过$article进行查看。 You haven't included the code but you have somewhere something like this: 您还没有包含代码,但是您有类似以下内容的地方:

return view('article.show');

and instead you should have: 相反,您应该具有:

return view('article.show', compact('article'));

so finally your show method in controller should look something like this: 所以最后您在控制器中的show方法应该看起来像这样:

public function show(Article $article)
{
    return view('article.show', compact('article'));
}

Its because you are calling your article $articles (plural) when in fact it is a single article. 这是因为实际上您将文章称为$articles (复数),而实际上它只是一篇文章。

in your controller change $articles to $article and then change it in the view where you have articles, for instance $articles->body . 在控制器中,将$articles更改$articles $article ,然后在您拥有文章的视图中将其更改,例如$articles->body It will then be correct when you use it in the form action. 当您在form操作中使用它时,它将是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM