duanli0453 2015-08-10 05:12
浏览 35
已采纳

BelongsToMany有很多Laravel SQLSTATE [42S22]

im trying to make warehouse(very simple of course). I have 3 tables as it is:

Stores table

class CreateStoresTable extends Migration
{

public function up()
{
    Schema::create('stores', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->integer('user_id');
        $table->timestamps();
    });
}      

Groups table:

 public function up()
{
    Schema::create('groups', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('visible');
        $table->timestamps();
    });
}

And a pivot table group_store:

public function up()
{
    Schema::create('group_store', function(Blueprint $table) {
        $table->integer('group_id')->unsigned()->index();
        $table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
        $table->integer('store_id')->unsigned()->index();
        $table->foreign('store_id')->references('id')->on('stores')->onDelete('cascade');
    });
}

Store Model -:

class Store extends Model
{
protected $table='stores';
protected $fillable = array('name','user_id');

public function group(){
    return $this->hasMany('App\Group');
}
}

Group Model

class Group extends Model
{
protected $table='groups';
protected $fillable = array('name','visible');

public function store(){
    return $this->belongsToMany('App\Store');
}
}

when i use this in tinker

$group=App\Group::find(4)
$group->store()->get() // and is working as it should

but when i try the reverse

$store=App\Store::first()
$store->group()->get() //i get this error message

Illuminate\Database\QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'groups.store_id' in 'where clause' (SQL: select * from groups where groups.store_id = 3 and groups.store_id is not null)'

Iam trying to understand why eloquent is searching for store_id in groups table...

  • 写回答

1条回答 默认 最新

  • douzhixun8393 2015-08-10 05:20
    关注

    Using a pivot table implies your relationship is many to many. As per the Laravel documentation, your relationships in your models should both be defined as belongsToMany.

    class Store extends Model
    {
        protected $table='stores';
        protected $fillable = array('name','user_id');
    
        public function group(){
            return $this->belongsToMany('App\Group');
        }
    }
    

    If you do not intend a many to many relationship, then your groups should only have one store, and your stores can have many groups. This would then mean that you just need a store_id column on your groups table. That is what Eloquent is looking for right now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了