mysql - Eloquent ORM 错误 : Column not found: 1054

标签 mysql laravel laravel-4 eloquent

我对 hasOne 关系有疑问,但我不明白为什么。 一篇文章只是一个供应商。所以在Article模型中放了一个指向主键供应商的外键。

class Article extends Eloquent {

    public $timestamps = true;

    protected $softDelete = true;

    // Accesibles to edit
    //protected $fillable = array( 'code', 'name', 'size', 'price', 'redial', 'supplier_id', 'count', 'description', 'colour' );
    protected $fillable = array('*');

    // prevent edit
    protected $guarded = array( 'updated_at', 'created_at', 'id' );


    public function supplier() {
        return $this->hasOne('Supplier');
    }

}

class Supplier extends Eloquent {

    public $timestamps = true;

    protected $softDelete = true;

    protected $fillable = array('*');

    protected $guarded = array('id', 'created_at', 'updated_at');


    public function article() {
        return $this->belongsTo('Article');
    }

}

迁移:

Schema::create('articles', function($table)
        {
            // DB Engine
            $table->engine = 'InnoDB';

            // Columns
            $table->increments('id');
            $table->string('code')->unique();
            $table->string('name', 100);
            $table->text('description')->nullable();
            $table->string('colour')->nullable();
            $table->integer('count')->unsigned();
            $table->float('price')->unsigned();
            $table->float('redial')->default(100)->unsigned();

            $table->integer('supplier_id')->unsigned();
            $table->foreign('supplier_id')->references('id')->on('suppliers');

            // Additionals columns
            $table->softDeletes(); // Never removed, only sets a date of deletion in delete_at column
            $table->timestamps();    // Set created_at updated_at columns automatically

            // Index and others atributtes
            $table->index('code');
        });

Schema::create('suppliers', function($table)
        {
            // DB Engine
            $table->engine = 'InnoDB';

            // Columns
            $table->increments('id');
            $table->string('full_name', 50);
            $table->string('company', 50);

            // Additionals columns
            $table->softDeletes(); // Never removed, only sets a date of deletion in delete_at column
            $table->timestamps();    // Set created_at updated_at columns automatically

        });

文章 Controller :

public function create()
    {
        $article = new Article;
        $article->code = Input::get('code');
        $article->name = Input::get('name');
        $article->description = Input::get('description');
        $article->size = Input::get('size');
        $article->colour = Input::get('colour');
        $article->count = Input::get('count');
        $article->price = Input::get('price');
        $article->redial = Input::get('redial');

        $supplier = Supplier::find(Input::get('supplier_id'));

        $article->supplier->associate($supplier);

        $article->save();

        return View::make('articles.show')->with( array('article' => $article) );
    }

输出:

Illuminate \ Database \ QueryException
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'suppliers.supplier_id' in 'where clause' (SQL: select * from `suppliers` where `suppliers`.`deleted_at` is null and `suppliers`.`supplier_id` is null limit 1)

最佳答案

您缺少 foreign key在你的suppliers表,因此将其添加到 suppliers 的迁移中

$table->integer('article_id');

由于您使用了 hasOne('Supplier')在你的Article然后模型 Laravel将在suppliers中寻找相关型号使用 article_id 的表格这将与父表(文章)中的模型相关,使用 id .换句话说,记录在你的articles表将与 suppliers 建立关系使用这样的东西:

表格articles (家长):

id | ...
 1 | ...
 2 | ...

表格suplierss ( child ):

id | article_id | ... 
 1 |     1      | ... //<-- article_id in this table is id in articles table
 2 |     2      | ... //<-- article_id in this table is id in articles table

articles table primary Key/idforeign key/article_idsuppliers表。

换句话说,您有 suppliers 的引用在articles使用 article_id 的表格所以你应该在你的suppliers中使用表:

public function article() {
    return $this->hasMany('Article');
}

然后在相反的模型(文章)中使用反向关系(belongsTo)使用:

// In Article Model
public function supplier() {
    return $this->belongsTo('Supplier');
}

关于mysql - Eloquent ORM 错误 : Column not found: 1054,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22676642/

相关文章:

python - 在 pymysql 中使用正确的语法将 python 变量传递给查询

Laravel Query Builder 多次使用

laravel-4 - Artisan migrate:make --create 命令不会生成 Schema::create()

linux - Laravel 4.2 邮件突然停止在 Amazon ec2 linux 中工作

php - MYSQL 触发器错误 phpmyadmin

Mysql使用存储过程将vb6到数据库的2个值与2列进行比较

mysql - 获取 4 个不同列的唯一分组依据值

php - Laravel 数组而不是集合

Laravel Eloquent : how to get related records via whereHas() method?

php - Laravel 用户权限访问某些页面?