Skip to main content
  1. About
  2. For Teams
Asked
Viewed 67 times
Part of PHP Collective
1

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)
);
7
  • 1
    Have you checked the documentation for Relationships? You've got this line ->whereColumn('id', 'graphicCardId'), which suggests you'd be able to define public function graphicCards(): HasMany { return $this->hasMany(GraphicCard::class); } in your Machine (should be singular, not plural) Model. You'd then simply do Machine::with('graphicCards')->get();(or similar). Consider editing your question and including your Models; we can probably help further with that information provided.
    Tim Lewis
    –  Tim Lewis
    2024-04-09 12:33:08 +00:00
    Commented Apr 9, 2024 at 12:33
  • 1
    I will edit my question and provide my models and my database
    laravel_
    –  laravel_
    2024-04-09 12:39:59 +00:00
    Commented Apr 9, 2024 at 12:39
  • 1
    Yup; that's how relationships work. If you do $machine = Machine::with('graphicsCards')->first();, then $machine->graphicsCards would be a Collection of multiple related GraphicCard instances, which are PHP representations of the rows in you Database Table. You can define and include multiple relationships, like Machine::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.
    Tim Lewis
    –  Tim Lewis
    2024-04-09 12:55:23 +00:00
    Commented Apr 9, 2024 at 12:55
  • 1
    Also, for your return response()->json(), you're close. If you define your relationships, then do return response()->json(['machines' => Machine::with(...)->get()], 200), then you'll return a JSON response with each Machine as an Object, and each of those objects will have X nested arrays of Objects, 1 for each relationship you include in your with() statement.
    Tim Lewis
    –  Tim Lewis
    2024-04-09 13:02:38 +00:00
    Commented Apr 9, 2024 at 13:02
  • 1
    Thanks for your help! I've been reading the documentation for about 40 minutes and everything seems to flow better. I'm managing to return a nested object instead of just getting the name property. But I face a problem, unfortunately I have to do the opposite. To return the graphic card from a machine ->get(); When I try to follow the correct path, I run into the error that the table I want to relate to does not have the key directly connecting the machine. Like, the machine table contains a foreign key column, but the Graphic Card table does not. Likewise, THANK YOU VERY MUCH
    laravel_
    –  laravel_
    2024-04-09 14:20:13 +00:00
    Commented Apr 9, 2024 at 14:20

0

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.