急切加载访问枢轴table
eager loading access the pivot table
我有3个模型
1 本书:
class Book extends Model
{
protected $guarded=[];
public function users(){return $this->belongsToMany(User::class);}
public function bookUser(){return $this->hasMany(BookUser::class);}
}
2 位用户
class User extends Authenticatable
{
protected $guarded = [];
public function books(){return $this->belongsToMany(Book::class);}
3 本书用户
class BookUser extends Model
{
protected $guarded = [];
protected $table = 'book_user';
public function book(){return $this->belongsTo(Book::class);}
public function user(){return $this->belongsTo(User::class) ; }
}
bookuser 迁移:
Schema::create('book_user', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->foreignId('book_id')->constrained();
$table->foreignId('user_id')->constrained();
$table->boolean('like')->nullable()->default(0);
});
我正在尝试获取当前用户喜欢的所有书籍:
public function index()
{
id=Auth::user()->id;
$books=Book::with('users')->get();
return response()->json($books);
}
这是我得到的:
[
{
"id": 1,
"created_at": "2021-03-22T14:16:30.000000Z",
"updated_at": "2021-03-22T14:16:30.000000Z",
"name": "power",
"image": "978014444447899.jpg",
"users": [
{
"id": 1,
"name": "mark",
"type": "reader",
"image": null,
"created_at": "2021-03-22T13:59:26.000000Z",
"updated_at": "2021-03-22T13:59:26.000000Z",
"pivot": {
"book_id": 1,
"user_id": 1,
"created_at": "2021-03-22T14:20:26.000000Z",
"updated_at": "2021-03-22T14:39:56.000000Z",
"like": 1
}
}
]
}]
我如何访问枢轴 table...或者如何获得它??我正在尝试,但 id 不起作用
$id=Auth::user()->id;
$books=Book::with('users',function($query) {
return $query->where('user.id','=',$id);
})->get();
您只需将 $id
注入 use
的函数作用域
$id=Auth::user()->id;
$books=Book::with(['users' => function($query) use($id) {
$query->where('user.id','=',$id);
}])->get();
我有3个模型
1 本书:
class Book extends Model
{
protected $guarded=[];
public function users(){return $this->belongsToMany(User::class);}
public function bookUser(){return $this->hasMany(BookUser::class);}
}
2 位用户
class User extends Authenticatable
{
protected $guarded = [];
public function books(){return $this->belongsToMany(Book::class);}
3 本书用户
class BookUser extends Model
{
protected $guarded = [];
protected $table = 'book_user';
public function book(){return $this->belongsTo(Book::class);}
public function user(){return $this->belongsTo(User::class) ; }
}
bookuser 迁移:
Schema::create('book_user', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->foreignId('book_id')->constrained();
$table->foreignId('user_id')->constrained();
$table->boolean('like')->nullable()->default(0);
});
我正在尝试获取当前用户喜欢的所有书籍:
public function index()
{
id=Auth::user()->id;
$books=Book::with('users')->get();
return response()->json($books);
}
这是我得到的:
[
{
"id": 1,
"created_at": "2021-03-22T14:16:30.000000Z",
"updated_at": "2021-03-22T14:16:30.000000Z",
"name": "power",
"image": "978014444447899.jpg",
"users": [
{
"id": 1,
"name": "mark",
"type": "reader",
"image": null,
"created_at": "2021-03-22T13:59:26.000000Z",
"updated_at": "2021-03-22T13:59:26.000000Z",
"pivot": {
"book_id": 1,
"user_id": 1,
"created_at": "2021-03-22T14:20:26.000000Z",
"updated_at": "2021-03-22T14:39:56.000000Z",
"like": 1
}
}
]
}]
我如何访问枢轴 table...或者如何获得它??我正在尝试,但 id 不起作用
$id=Auth::user()->id;
$books=Book::with('users',function($query) {
return $query->where('user.id','=',$id);
})->get();
您只需将 $id
注入 use
$id=Auth::user()->id;
$books=Book::with(['users' => function($query) use($id) {
$query->where('user.id','=',$id);
}])->get();