由于您没有提供任何代码,所以我将从基本描述。
迁移
位置表
Schema::create('locations', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->timestamps();
});
笔记本电脑桌
Schema::create('laptops', function(Blueprint $table){
$table->increments('id');
$table->string('name');
$table->integer('location_id')->unsigned();
$table->integer('stock');
$table->timestamps();
$table->foreign('location_id')
->references('id')
->on('locations')
->onDelete('cascade')
->onUpdate('cascade');
});
模型
位置模型
class Location extends Model {
public function laptops(){
return $this->hasMany('\App\Laptop');
}
}
笔记本电脑型号
class Laptop extends Model {
public function location(){
return $this->belongsTo('\App\Location');
}
}
如果您已经创建了这样的应用,那么有多种方法可以实现目标。
这将查询属于某个位置且股票价值为1 的所有笔记本电脑。
1.
Laptop::where('location_id','some_location_id')->where('stock',1)->get();
2
Location::where('id','some_location_id')->with(['laptops' => function($query){
$query->where('stock',1);
}])->get();
我希望这能解决您的问题。随时发表评论并提出任何问题。