1
votes

I have model Article, Filegroup, File.

When save new article with image, it displays this error:

{"exception":"Illuminate\Database\QueryException","message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'image' in 'field list' (SQL: insert into articles (title, text, like_count, view_count, comment_count, image, web_image, updated_at, created_at) values (dsvdscdscdscsdcdsc, sacsaxasxsaxsaxsaxsaxsax, 0, 0, 0, 11, 13989, 2017-06-27 15:40:49, 2017-06-27 15:40:49))","trace":[{"file":"/var/www/laravel/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php","line":726,"function":"runQueryCallback","class":"Illuminate\Database\Connection","type":"->","args":["insert into articles (title, text, like_count, view_count, comment_count, image, web_image, updated_at, created_at) values (?, ?, ?, ?, ?, ?, ?, ?, ?)",..................

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');
    }
}

Tables articles created with migration:

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
Please verify if column image exists in table articlesImAtWar
image and web_image does not exists in your articles migrationNerea
image column does not exist in your articles table migrationVishal Varshney

1 Answers

0
votes

When you're using $guarded attributes on a model rather than $fillable it will try and insert every value in the attributes array of your model. More on that here: https://laravel.com/docs/5.4/eloquent#mass-assignment

You can either make sure that the array of attributes that you're assigning to your article model only contains attributes that have corresponding database columns or you can switch to $fillable like so:

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