Hello All, First time posting on here.
I am stuck at the moment.
What I have are 3 models: Locations, Items, Stock.
A relation is made between the Items and Stock then Stock has a relation to Locations.
What I am looking to do is return all Items (the model) that has Stock given the location.
I have all of the models set up and the FKs configured between the tables but I don't know how to form the query using Eloquent's interface, I know it's going to be a join of some kind but it is just not ringing a bell.
None the less here is the db migrations.
Locations:
Schema::create('locations', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('company_id')->unsigned();
$table->foreign('company_id')
->references('id')->on('companies')
->onDelete('cascade');
$table->boolean('enabled')->default(true);
$table->softDeletes();
$table->timestamps();
});
Items:
Schema::create('items', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('company_id')->unsigned();
$table->foreign('company_id')
->references('id')->on('companies')
->onDelete('cascade');
$table->string("name")->nullable();
$table->string("sku")->nullable()->unique();
$table->boolean('is_discontinued')->default(false);
$table->softDeletes();
$table->timestamps();
});
Stock:
Schema::create('item_stock', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('company_id')->unsigned();
$table->foreign('company_id')
->references('id')->on('companies')
->onDelete('cascade');
$table->bigInteger('item_id')->unsigned();
$table->foreign('item_id')
->references('id')->on('items')
->onDelete('cascade');
$table->bigInteger('location_id')->unsigned();
$table->foreign('location_id')
->references('id')->on('locations')
->onDelete('cascade');
$table->bigInteger('qty')->default(0);
$table->softDeletes();
$table->timestamps();
});