php - Laravel 5.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 );
    }


}

我正在像他一样做这些步骤,但我有一个问题,除了 created_atupdated_at 之外的所有字段都将像这样为空:

enter image description here

这是我的评论模型:

<?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/

相关文章:

php - 如何使用在线 cPanel 数据库在 php 中连接动态数据库

asp.net - 如果我在 ASP.NET 网页中保持数据库连接打开会发生什么

Laravel通过hasMany获取belongsToMany

php - 当发送错误的访问 token 时,Passport 返回 500 错误(没有响应)

php - Laravel .env 文件中环境特定的 SSL 配置

php - if语句不回避insert查询

mysql - 你怎么知道什么时候需要单独的表?

php - 在 php 变量中添加样式

php - 在 Woocommerce 电子邮件页脚模板中获取客户 user_id

php - Docker LAMP堆栈-保存PHP项目的位置在哪里?