Laravel 5.1 create not working

I am new to Laravel 5.1. I'm watching a tutorial video and in video teacher is using this code to insert data in database :

<?php

namespace App\Http\Controllers;

use App\comments;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CommentController  extends Controller
{
    public function getCommentNew()
    {
        $data = array(

            'commenter' => 'soheil' ,
            'comment  ' => 'Test content' ,
            'email'     => 'soheil@gmail.com' ,
            'post_id'   =>  1 ,
        ) ;
        comments::create( $data );
    }


}

I am doing the steps like him but I have a problem , all fields ecept created_at and updated_at will be empty like this :

this is my comments model :

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class comments extends Model
{
    protected $fillable = ['commenter,email,post_id,comment,approved'];
    public function post(){
        return $this->belongsTo('App\posts');
    }
}

and this is migration :

<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCommentsTable extends Migration
{
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedinteger('post_id');
            $table->string('commenter') ;
            $table->string('email') ;
            $table->text('comment') ;
            $table->boolean('approved');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('comments');
    }
}

Answers


You haven't properly set the $fillable attribute in your Model, try with :

// in your model
protected $fillable = [
    'commenter','email','post_id','comment','approved'
];

You have to define column names saperately on fillable array as shempignon described on above answer Ex: ['column1', 'column2'...] not in a single string. Each column name needs to be an array element


Try this and it'll be fine :) , you just forgot protected $table = 'comments';

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class comments extends Model
{
    protected $table = 'comments';
    protected $fillable = ['commenter','email','post_id','comment','approved'];
    public function post(){
        return $this->belongsTo('App\posts');
    }
}

Need Your Help