Page 1 of 1

Ich habe Beziehungen in Modellen definiert, aber wie kann ich komplexe Abfragen auf eloquente Weise schreiben?

Posted: 23 Dec 2024, 14:04
by Anonymous
Ich habe eine Abfrage in einer vorhandenen Anwendung, jetzt muss ich die Abfrage auf eloquente Weise aktualisieren. Ich habe die folgende Struktur meiner Beziehungen. Wie kann ich die folgende Abfrage auf eloquente Weise schreiben?

Controller-Abfrage, die ich auf eloquente Weise ändern möchte

foreach ($data['Divisions'] as $sections) {

$SecQry1 = "SELECT sections.id, sections.division_id, sections.code, sections.name ,specifications.description
from specifications inner JOIN sections on specifications.section_id=sections.id where specifications.status='active'
AND specifications.manufacturer_id = $user->id AND sections.division_id=" . $sections->id . " group by sections.id";

$SectionsDivision1 = DB::select($SecQry1);

foreach ($SectionsDivision1 as $key => $selectionDiv) {
array_push($selectionDiv_array1, $selectionDiv);
}

}


class Specification extends Model {

protected $table = 'specifications';

public function section()
{
return $this->belongsTo('App\Section');
}
}


Klassenabschnitt erweitert Modell {

protected $table = 'sections';

public function specifications()
{
return $this->hasMany('App\Specification', 'section_id');
}
public function division()
{
return $this->belongsTo('App\Division');
}


}

class Division extends Model {

protected $table = 'divisions';
protected $fillable = array('name', 'description', 'code','status');

public function sections()
{
return $this->hasMany('App\Section', 'division_id');
}
}


0 => {#1063 ▼
+"id": 1
+"division_id": 1
+"code": "03 01 00"
+"name": "Maintenance of Concrete"
+"description": null
}
]