首页 文章

未找到列:1054未知列'image' in 'field list'

提问于
浏览
1

我有模型文章,文件组,文件 .

使用图像保存新文章时,会显示以下错误:

{“exception”:“Illuminate \ Database \ QueryException”,“message”:“SQLSTATE [42S22]:找不到列:1054'字段列表'中的未知列'图像'(SQL:插入文章( Headers ,文本, like_count,view_count,comment_count,image,web_image,updated_at,created_at)values(dsvdscdscdscsdcdsc,sacsaxasxsaxaxaxsaxsaxsax,0,0,0,11,13989,2017-06-27 15:40:49,2017-06-27 15:40: 49)) “ ”跟踪“:〔{ ”文件“: ”/无功/网络/ laravel /网络/供应商/ laravel /框架/ SRC /照亮/数据库/ Connection.php“, ”行“:726,” 功能“:”runQueryCallback“,”class“:”Illuminate \ Database \ Connection“,”type“:” - >“,”args“:[”插入文章( Headers ,文本,like_count,view_count,comment_count,image,web_image ,updated_at,created_at)值(?,?,?,?,?,?,?,?,?)“,..................

models/Article.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    protected $guarded = ['id'];
    protected $with = ['image'];

    public function image()
    {
        return $this->belongsTo('App\Models\Filegroup', 'image_id');
    }
}

models/FileGroup.php

use Illuminate\Database\Eloquent\Model;

class Filegroup extends Model
{
    protected $table = 'file_groups';

    protected $with = ['files', 'name'];

    protected $guarded = ['id'];

    /**
     * One-to-Many relations with SiteString.
     *
     * @foreignModel SiteString
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function name()
    {
        return $this->belongsTo('App\Models\SiteString', 'name_id');
    }

    /**
     * Many-to-Many relations with File.
     *
     * @foreignModel File
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function files()
    {
        return $this->hasMany('App\Models\File', 'filegroup_id');
    }
}

models/file.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class File extends Model
{
    protected $table = 'files';

    protected $fillable = [
        'mimi_type',
        'path',
        'width',
        'height',
        'size'
    ];

    /**
     * One-to-Many inverse relations with Filegroup.
     *
     * @foreignModel Filegroup
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function group()
    {
        return $this->belongsTo('App\Models\Filegroup', 'filegroup_id');
    }
}

使用迁移创建的表文章:

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('image_id');
            $table->text('text');
            $table->string('title');
            $table->integer('like_count');
            $table->integer('view_count');
            $table->integer('comment_count');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}

1 回答

  • 0

    当您在模型而不是 $fillable 上使用 $guarded 属性时,它将尝试在模型的属性数组中插入每个值 . 更多相关信息:https://laravel.com/docs/5.4/eloquent#mass-assignment

    您可以确保分配给文章模型的属性数组仅包含具有相应数据库列的属性,或者可以切换到 $fillable ,如下所示:

    protected $fillable = [
        'image_id',
        'text',
        'title',
        'like_count',
        'view_count',
        'comment_count',
    ];
    

相关问题