{{Form::open(['route'=>['comments.store',$post->id],'method'=>'POST'])}}
routes
i added comments section to my blogging website,i created a route with name 'comments.store' (which is showing when i do php artisan route:list.) now in my comment section when i give it a route of comments.store it shows 'route not found error'. "NotFoundHttpException in RouteCollection.php line 161:"
my route file is Route::post('comments/{$post_id}',['uses'=>'CommentsController@store','as'=>'comments.store']);
does this work?
{{Form::open(['route'=>[route('comments.store',$post->id)],'method'=>'POST'])}}
no @vapenation it still does not work. Route [http://127.0.0.1:8000/comments/4] not defined. (View: /opt/lampp/htdocs/laravel/blog/resources/views/blog/single.blade.php)
this is my route i am using->| | POST | comments/{$post_id} | comments.store | App\Http\Controllers\CommentsController@store
<?php $titleTag=htmlspecialchars($post->title);?>
@section('title',"|$titleTag")
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>{{$post->title}}</h1>
<p>{{$post->body}}</p>
<hr>
<p>Posted In:{{$post->category->name}}</p>
</div>
</div>
<div class="row">
<div id="comment-form" class="col-md-8 col-md-offset-2" style="margin-top:50px;">
{{Form::open(['route'=>['comments.store',$post->id],'method'=>'POST'])}}
<div class="row">
<div class="col-md-6">
{{Form::label('name','Name:')}}
{{Form::text('name',null,['class'=>'form-control'])}}
</div>
<div class="col-md-6">
{{Form::label('email','Email:')}}
{{Form::text('email',null,['class'=>'form-control'])}}
</div>
<div class="col-md-12">
{{Form::label('comment',"Comment:")}}
{{Form::textarea('comment',null,['class'=>'form-control','rows'=>'5'])}}
{{Form::submit('Add Comment',['class'=>'btn-block btn-success','style'=>'margin-top:5px'])}}
</div>
</div>
{{Form::close()}}
@endsection
@anmol I'm confused. Your talking about routes but your listing a Form::open which is part of laravelcollective/html package
Assuming that you pulled in this package and you are using blade the syntax would be
{!! Form::open(['route' => ['comments.store', $post->id]]) !!}
The laravel collective html assumes it is post when using Form::open
@codenex let me make it a little more clear.i made a route and it was successfully created as i can see iit from the terminal. now when i want to use it in my comments section and i use form helpers to call the route you can look into my code that i just commented here.it is showing not found error.so the error is not about route it has something to the way it is being called,i suppose.
when i click the submit button it shows this error.before that everything works fine.
@anmol You stated in your initial my route file is Route::post('comments/{$post_id}',['uses'=>'CommentsController@store','as'=>'comments.store']);
I'm just saying that's not your route file. That is in your blade file. Secondly you are either using a way old version of Laravel or using the Laravel Collective HTML package of which neither was mentioned.
If you are using the Laravel Collective package your syntax is incorrect which may be why it's not working. I posted you the correct syntax. Check that and see if it fixes your issue. If not let me know what it shows in your URL field after you click the submit button.
Open your network tools in your browser then press submit. This will tell us if it is the form or the routing.
Look at the first entry that appears after you press submit. It will tell you the URL and the method (get or post)
The docs say Form::open(['route' => ['route.name', $user->id]]) so your form looks ok, but I don't use it myself as I find it a waste of time.
@Snapey Oh yeah looks like I was looking at @vapenation response. I have a hard time putting together fragmented data, lol. In either case for the form portion you don't need the post method as it is assumed by default. But as is it is it should work.
{{ Form::open(['route' => ['comments.store', $post->id]]) }}
What I am noticing is that if we check a resource controller the post route does't actually take a parameter at the store method. So I guess the next question is how is the store method on your controller setup?
@snapey method=POST url=http://127.0.0.1:8000/comments/4 adress=127.0.0.1:8000 status:404 not found. file=4 cause=document.
@codenex i tried using the fixed version of the code but it gave a similar error.
@anmol reread my latest...
In the same browser tools look at the fields sent to the server. In the request you can see each posted field
If there is a field _method then this means Laravel is being told to put or patch instead of post and your route needs to reflect the same.
these are the request fields @Snapey y request url:http://127.0.0.1:8000/comments/4 request method:POST remote adress:127.0.0.1:800 status code:not found404.
@codenex no the new code does not work as well.
@Snapey when i was migrating my migration file,i had an error but since it got migrated in he database so i ignored it for an instance.can that have something to do with it???because it is using a foreign key constraint post_id.
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
n your SQL syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near ') on delete cascade' at line 1 (
SQL: alter table `comments` add constraint `comments_post_id_foreign` forei
gn key (`post_id`) references `posts` () on delete cascade)
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
n your SQL syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near ') on delete cascade' at line 1
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error i
n your SQL syntax; check the manual that corresponds to your MariaDB server
version for the right syntax to use near ') on delete cascade' at line 1
@anmol if you posted the fields, I cannot see them.
They are in the post request. In Chrome, click on the entry on the left hand panel, then on the right, select 'headers' then scroll to the bottom to see form data.
If you cannot do that post the source code from your browser (now your view file.
No, very unlikely to be the migration
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Josefin Slab">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<title>travelogue |second post</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link media="all" type="text/css" rel="stylesheet" href="http://127.0.0.1:8000/css/styles.css">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="https://www.facebook.com/Travelogue-1107871025939482/">Travelogue</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class=""><a href="/">Home <span class="sr-only">(current)</span></a></li>
<li class=""><a href="/blog">Blog <span class="sr-only">(current)</span></a></li>
<li class=""><a href="/about">About</a></li>
<li class=""><a href="/contact">Contact</a></li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<a href="http://127.0.0.1:8000/register" class="btn btn-primary" style="margin-top:6px">Register</a>
<a href="http://127.0.0.1:8000/login" class="btn btn-primary" style="margin-top:6px">Login</a>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>second post</h1>
<p>“Stuff your eyes with wonder, live as if you’d drop dead in ten seconds. See the world. It’s more fantastic than any dream made or paid for in factories.” – Ray Bradbury".</p>
<hr>
<p>Posted In:road trips</p>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
</div>
</div>
<div class="row">
<div id="comment-form" class="col-md-8 col-md-offset-2" style="margin-top:50px;">
<form method="POST" action="http://127.0.0.1:8000/comments/4" accept-charset="UTF-8"><input name="_token" type="hidden" value="pa6Tiyoglrg80bNYiCxJY5OKrhFDhviAvZt16ur4">
<div class="row">
<div class="col-md-6">
<label for="name">Name:</label>
<input class="form-control" name="name" type="text" id="name">
</div>
<div class="col-md-6">
<label for="email">Email:</label>
<input class="form-control" name="email" type="text" id="email">
</div>
<div class="col-md-12">
<label for="comment">Comment:</label>
<textarea class="form-control" rows="5" name="comment" cols="50" id="comment"></textarea>
<input class="btn-block btn-success" style="margin-top:5px" type="submit" value="Add Comment">
</div>
</div>
</form>
<hr>
<p class="text-center"><em>copyright anmol - all rights reserved</em></p>
</div>
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
</html>
@Snapey this is the source code.does it help???
Big apology here........
So your source code looks fine. I cannot see any hidden _method field, so the issue must be with the route.
I go back to page 1 and look at your original post
my route file is Route::post('comments/{$post_id}',
BAM!
You should not have the $ in the route
aargh!
Change it to
my route file is Route::post('comments/{post_id}',
Sometimes you just need to have a break and come back and look at something with fresh eyes.
FatalThrowableError in CommentsController.php line 44: Class 'App\Http\Controllers\Comment' not found
@Snapey i think that problem have solved been plz for once look at my controller file as well.i have made a mess out of everything.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
use Session;
class CommentsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* 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,$post_id)
{
$this->validate($request,array(
'name'=>'required|max:255',
'email'=>'required|email|max:255','comment'=>'required|min:5|max:2000'));
$post=Post::find($post_id);
$comment= new Comment();
$comment->name=$request->name;
$comment->email=$request->email;
$comment->comment=$request->comment;
$comment->approved=true;
$comment->post()->associate($post);
$comment->save();
Session::flash('success','Comment was added');
return redirect()->route('blog.single',[$post->slug]);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
CommentsController not Comment
Please or to participate in this conversation.