I have a database where my tables relate to each other. I would like to perform several subqueries, but I have only been able to return 1 subquery, is there any function that can help me? I tried with DB::raw() and was unsuccessful
In the machines table, for example, I have the processor ids, and I can return the processors with this query.
return Machines::addSelect(['graphicCardId' => GraphicCard::select('name') ->whereColumn('id', 'graphicCardId') ->limit(6) ])->get();
How can I insert more subqueries and have them return a json object? I tried to perform a graphic_Card subquery like above, but with another table, but I don't have its return, since I already gave a 'return'. I also tried:
return response()->json([Machines::all()], 200)
But I got no results. In this format, I can only perform a subquery. -> I'am using blade
Thank you very much in advance! I'm a beginner developer passionate about laravel. Hugs from Brazil
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Machines extends Model
{
use HasFactory;
protected $table = 'machine';
}
Other Model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class GraphicCard extends Model
{
use HasFactory;
protected $table = 'graphiccard';
}
Here is database:
drop database if exists AlatechMachines;
create database AlatechMachines;
use AlatechMachines;
create table user (
id int auto_increment not null,
username varchar(64) not null,
password varchar(512) not null,
accessToken varchar(512) not null,
constraint primary key(id)
);
create table brand (
id int auto_increment not null,
name varchar(96) not null,
constraint primary key(id)
);
create table socketType (
id int auto_increment not null,
name varchar(96) not null,
constraint primary key(id)
);
create table ramMemoryType (
id int auto_increment not null,
name varchar(96) not null,
constraint primary key(id)
);
create table motherboard (
id int auto_increment not null,
name varchar(96) not null,
imageUrl varchar(512) not null,
brandId int not null,
socketTypeId int not null,
ramMemoryTypeId int not null,
ramMemorySlots int not null,
maxTdp int not null,
sataSlots int not null,
m2Slots int not null,
pciSlots int not null,
constraint foreign key(ramMemoryTypeId) references ramMemoryType(id),
constraint foreign key(socketTypeId) references socketType(id),
constraint foreign key(brandId) references brand(id),
constraint primary key(id)
);
create table processor (
id int auto_increment not null,
name varchar(96) not null,
imageUrl varchar(512) not null,
brandId int not null,
socketTypeId int not null,
cores int not null,
baseFrequency float not null,
maxFrequency float not null,
cacheMemory float not null,
tdp int not null,
constraint foreign key(socketTypeId) references socketType(id),
constraint foreign key(brandId) references brand(id),
constraint primary key(id)
);
create table ramMemory (
id int auto_increment not null,
name varchar(96) not null,
imageUrl varchar(512) not null,
brandId int not null,
size int not null,
ramMemoryTypeId int not null,
frequency float not null,
constraint foreign key(ramMemoryTypeId) references ramMemoryType(id),
constraint foreign key(brandId) references brand(id),
constraint primary key(id)
);
create table storageDevice (
id int auto_increment not null,
name varchar(96) not null,
imageUrl varchar(512) not null,
brandId int not null,
storageDeviceType enum('hdd', 'ssd') not null,
size int not null,
storageDeviceInterface enum('sata', 'm2') not null,
constraint foreign key(brandId) references brand(id),
constraint primary key(id)
);
create table graphicCard (
id int auto_increment not null,
name varchar(96) not null,
imageUrl varchar(512) not null,
brandId int not null,
memorySize int not null,
memoryType enum('gddr5', 'gddr6') not null,
minimumPowerSupply int not null,
supportMultiGpu bool not null,
constraint foreign key(brandId) references brand(id),
constraint primary key(id)
);
create table powerSupply (
id int auto_increment not null,
name varchar(96) not null,
imageUrl varchar(512) not null,
brandId int not null,
potency int not null,
badge80Plus enum('none', 'white', 'bronze', 'silver', 'gold', 'platinum', 'titanium') not null,
constraint foreign key(brandId) references brand(id),
constraint primary key(id)
);
create table machine (
id int auto_increment not null,
name varchar(96) not null,
description varchar(512) not null,
imageUrl varchar(512) not null,
motherboardId int not null,
processorId int not null,
ramMemoryId int not null,
ramMemoryAmount int not null,
graphicCardId int not null,
graphicCardAmount int not null,
powerSupplyId int not null,
constraint foreign key(motherboardId) references motherboard(id),
constraint foreign key(processorId) references processor(id),
constraint foreign key(ramMemoryId) references ramMemory(id),
constraint foreign key(graphicCardId) references graphicCard(id),
constraint foreign key(powerSupplyId) references powerSupply(id),
constraint primary key(id)
);
create table machineHasStorageDevice (
machineId int not null,
storageDeviceId int not null,
amount int not null,
constraint foreign key(machineId) references machine(id) on delete no action,
constraint foreign key(storageDeviceId) references storageDevice(id) on delete no action,
constraint primary key(machineId, storageDeviceId)
);
->whereColumn('id', 'graphicCardId')
, which suggests you'd be able to definepublic function graphicCards(): HasMany { return $this->hasMany(GraphicCard::class); }
in yourMachine
(should be singular, not plural) Model. You'd then simply doMachine::with('graphicCards')->get();
(or similar). Consider editing your question and including your Models; we can probably help further with that information provided.$machine = Machine::with('graphicsCards')->first();
, then$machine->graphicsCards
would be a Collection of multiple relatedGraphicCard
instances, which are PHP representations of the rows in you Database Table. You can define and include multiple relationships, likeMachine::with(['processors', 'graphicCards'])
. Read the documentation and try some things, and if you have a follow-up, you can edit your question again with more information.return response()->json()
, you're close. If you define your relationships, then doreturn response()->json(['machines' => Machine::with(...)->get()], 200)
, then you'll return a JSON response with eachMachine
as an Object, and each of those objects will have X nested arrays of Objects, 1 for each relationship you include in yourwith()
statement.