fro*_*man 1 php laravel eloquent laravel-5 laravel-5.5
我正在尝试使用belongsToMany关系,我从来没有遇到任何问题,但由于某种原因它返回空.
我在Store模型中建立了我的关系,如下所示:
public function images()
{
return $this->belongsToMany('App\Images', 'images_stores', 'image_id', 'store_id');
}
Run Code Online (Sandbox Code Playgroud)
当我测试关系时,它只返回一个空集合没有错误,尽管它不是.这是我访问数据的方式:
public function edit($id)
{
$store = Store::find($id);
dd($store->images);
return view('admin.pages.store.edit', compact('store'));
}
Run Code Online (Sandbox Code Playgroud)
但它只会返回一个空集合,我希望有人可以帮我解决这个问题.这些是我的迁移文件,但我认为问题不在迁移文件中.
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('path');
$table->timestamps();
});
// pivot table
Schema::create('images_stores', function (Blueprint $table) {
$table->increments('id');
$table->integer('image_id')->unsigned()->nullable();
$table->foreign('image_id')->references('id')->on('images')->onDelete('cascade');
$table->integer('store_id')->unsigned()->nullable();
$table->foreign('store_id')->references('id')->on('stores')->onDelete('cascade');
$table->timestamps();
});
// store table
Schema::create('stores', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('location');
$table->string('email');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
多谢你们.
我认为ids是倒置的.试试这个:
public function images()
{
return $this->belongsToMany('App\Images', 'images_stores', 'store_id', 'image_id');
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
477 次 |
| 最近记录: |