@ersinkandemir Ok I found out why it wasn't working. I'm working with a slug.. silly me.
But I'm not getting a BadMethodCallException in /home/vagrant/Code/Laravel/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php line 2024:
Call to undefined method Illuminate\Database\Query\Builder::make()
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use App\Post;
use App\Comment;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class CommentsController extends Controller
{
/**
* Adds a comment to a specific post
*/
public function store(Request $request, $slug){
$post = Post::whereSlug($slug)->first();
$commentData = array_merge($request->all(), ['user_id' => $request->user()->id]);
$comment = \App\Comment::make($commentData);
$post->comments()->save($comment);
return redirect()->back();
}
}
This is the line:
$comment = App\Comment::make($commentData);
routes.php
# Comments
$router->post('posts/{slug}', ['as' => 'comment.store', 'uses' => 'CommentsController@store']);
show.blade.php
@extends('masterpage.app')
@section('content')
<h1>{{ $post->title }}</h1>
<h4>{!! nl2br($post->content) !!}</h4>
@if($post->youtube != '')
<iframe width="420" height="315" src="http://www.youtube.com/embed/{{$post->youtube}}" frameborder="0" allowfullscreen></iframe>
@endif
<div class="panel panel-default">
<div class="panel-body">
{!! Form::open(['route' => ['comment.store', $post->slug]]) !!}
<div class="form-group">
{!! Form::Textarea('comment', 'Comment here...', ['class' => 'form-control comments']) !!}
</div>
<div class="form-group">
{!! Form::submit('Leave a comment', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
</div>
@stop
The view-source
<form method="POST" action="http://app.dev:8000/posts/my-first-post-odtyvJzm92" accept-charset="UTF-8"><input name="_token" type="hidden" value="IINrYcikpdk3qe8STE6DqEUVjtSwWxduG5u5NGZJ">
<div class="form-group">
<textarea class="form-control comments" name="comment" cols="50" rows="10">Comment here...</textarea>
</div>
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Leave a comment">
</div>
</form>