我是 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 );
}
}
我正在像他一样做这些步骤,但我有一个问题,除了 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');
}
}
这是迁移:
<?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');
}
}
最佳答案
您还没有在您的模型中正确设置 $fillable
属性,请尝试:
// in your model
protected $fillable = [
'commenter','email','post_id','comment','approved'
];
关于php - Laravel 5.1 创建不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35040787/