soh*_* yo 1 php database laravel eloquent laravel-5.1
我是Laravel 5.1的新手.我正在观看教程视频,视频教师正在使用此代码在数据库中插入数据:
<?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 );
}
}
Run Code Online (Sandbox Code Playgroud)
我正在做像他这样的步骤,但我有一个问题,所有字段都是ecept,created_at并且updated_at将像这样空:
这是我的评论模型:
<?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');
}
}
Run Code Online (Sandbox Code Playgroud)
这是迁移:
<?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');
}
}
Run Code Online (Sandbox Code Playgroud)
您尚未$fillable在模型中正确设置属性,请尝试:
// in your model
protected $fillable = [
'commenter','email','post_id','comment','approved'
];
Run Code Online (Sandbox Code Playgroud)