問題描述
我有三個表garages
、cars
、securities
.
證券是保護一輛汽車的安全,您可以擁有多個安全,但一個安全只能保護一輛車.車庫是汽車所在,證券也是.
我想要的是列出所有證券并知道他所在的車庫的名稱.問題是 securities
沒有包含車庫 id 的列,只有他保持安全的汽車的 id,但是 car 有車庫的 id.
Laravel Eloquent 有一個名為 hasManyThrough
的方法,但 securities
只有一個 garage
到 cars
.>
這是表格:
車庫表:
-----------------------------------|車庫編號|車庫名稱|車庫大小|---------------------|1|車庫 1|200|---------------------|2|車庫 2|400|---------------------
汽車表:
---------------------------|car_id|car_name|garage_id|---------------------------|1|車1|1|---------------------------|2|車2|1|---------------------------|3|車3|2|---------------------------
證券表:
-------------------|security_id|security_name|car_id|----------------------------------|1|安全 1|1|----------------------------------|2|安全2|1|----------------------------------|3|安全3|2|----------------------------------|4|安全 4|3|----------------------------------
輸出必須是:
安全 1 在車庫 1保安 2 在車庫 1保安 3 在車庫 1保安 4 在車庫 2
我有模型:
代碼是列出車庫,但我想做類似但列出證券(只是為了讓您了解結(jié)構(gòu)如何).
$garages = Garages::with(['cars', 'securities'])->get();$garages->transform(function($garages) {返回數(shù)組('車庫名稱' =>$garage->garage_name,'count_cars' =>$garage->cars->count(),'count_securities' =>$garage->securities->count(),);});車庫類擴展了 Eloquent{公共功能汽車(){返回 $this->hasMany('cars');}公共功能證券(){返回 $this->hasMany('證券');}}類 Cars 擴展了 Eloquent{}證券類擴展 Eloquent{}
再次強調(diào):我想知道與保安人員保持安全的汽車相關(guān)的車庫名稱.
只是為了讓它更容易理解,如果我這樣做:
$securities = Securities::with(['cars'])->get();證券類擴展 Eloquent{公共功能汽車(){返回 $this->hasOne('cars');}}
我將僅從 cars
表中獲取 garage_id
作為關(guān)系.我真正想要的是車庫的名字.
[relations:protected] =>大批([汽車] =>汽車對象(...[屬性:受保護] =>大批(...[汽車名稱] =>車1[車庫 ID] =>1...
您似乎沒有正確關(guān)聯(lián)對象.讓我們分解一下:
如果一個Garage
有很多Car
,那么一個Car
屬于Garage
,讓我們繼續(xù)這個想法心.
車庫
有很多汽車
Car
有很多Security
安全
屬于汽車
Garage
通過Car
有很多
Security
在 Eloquent 中,您可以直接將這些關(guān)系添加進來,鑒于您在上面發(fā)布的架構(gòu),它應(yīng)該可以工作.
class Garage 擴展了 Eloquent{公共功能汽車(){返回 $this->hasMany('cars');}公共功能證券(){返回 $this->hasManyThrough('Security', 'Car');}}類 Car 擴展了 Eloquent{公共功能證券(){返回 $this->hasMany('Security');}//在第二次編輯中添加公共功能車庫(){返回 $this->belongsTo('Garage');}}類 Security 擴展了 Eloquent{公共函數(shù) car(){返回 $this->belongsTo('Car');}}
應(yīng)該就是這樣了.
您可以從任何模型訪問所有這些關(guān)系,只要有一條路徑可以使用這些關(guān)系的組合從一個模型繪制到另一個模型.看看這個,例如:
$security = Security::with('car.garage')->first();
將檢索第一個 Security
記錄并在其上加載 Car
關(guān)系,然后再執(zhí)行一步并在每個 Garage
關(guān)系上加載所有 Garage
關(guān)系code>Car 對象加載在 Security
模型下.
您可以使用以下語法訪問它們:
$security->car->garage->id//其他例子$garage = Garage::with('cars.securities')->first();foreach($garage->cars as $car){foreach($cars-> 證券作為 $security){echo "汽車 {$car->id} 已分配給它 {$security->id}";}}
此外,請注意關(guān)系對象是 Collection
的實例,因此您擁有所有花哨的方法,例如 ->each()
、->;count()
, ->map()
可用.
I have three tables garages
, cars
, securities
.
The securities are the ones that is keeping one car safe, you can have more than one security, but a single security can keep only one car safe. The garage is where the car is and the securities are as well.
What I want is to list all the securities and know the name of the garage that he is. The problem is that securities
doesn't have a column containing the id of the garage, only the id of the car that he is keeping safe, but car has the id of the garage.
Laravel Eloquent has a method called hasManyThrough
, but securities
has one garage
through cars
only.
Here is the tables:
garages table:
-----------------------------------
|garage_id|garage_name|garage_size|
-----------------------------------
| 1| Garage 1| 200|
-----------------------------------
| 2| Garage 2| 400|
-----------------------------------
cars table:
---------------------------
|car_id|car_name|garage_id|
---------------------------
| 1| Car 1| 1|
---------------------------
| 2| Car 2| 1|
---------------------------
| 3| Car 3| 2|
---------------------------
securities table:
----------------------------------
|security_id|security_name|car_id|
----------------------------------
| 1| Security 1| 1|
----------------------------------
| 2| Security 2| 1|
----------------------------------
| 3| Security 3| 2|
----------------------------------
| 4| Security 4| 3|
----------------------------------
The output must to be:
Security 1 is on Garage 1
Security 2 is on Garage 1
Security 3 is on Garage 1
Security 4 is on Garage 2
And I have the models:
The code is to list the garages, but I want to make similar but to list the securities (just for you to have an idea of how the structure is).
$garages = Garages::with(['cars', 'securities'])->get();
$garages->transform(function($garages) {
return array(
'garage_name' => $garage->garage_name,
'count_cars' => $garage->cars->count(),
'count_securities' => $garage->securities->count(),
);
});
class Garages extends Eloquent
{
public function cars()
{
return $this->hasMany('cars');
}
public function securities()
{
return $this->hasMany('securities');
}
}
class Cars extends Eloquent
{
}
class Securities extends Eloquent
{
}
And just to make an emphasis again: I want to have the name of the garage related to the car that the security is keeping safe.
Just to make it even easier to understand, if I do this:
$securities = Securities::with(['cars'])->get();
class Securities extends Eloquent
{
public function cars()
{
return $this->hasOne('cars');
}
}
I will get only the garage_id
from cars
table as relations. What I really want is the name of the garage.
[relations:protected] => Array
(
[cars] => Cars Object
(
...
[attributes:protected] => Array
(
...
[car_name] => Car 1
[garage_id] => 1
...
It looks like you are not relating objects correctly. Let's break that down:
If a Garage
Has Many Car
then a Car
Belongs To Garage
, Lets proceed with this idea in mind.
Garage
Has ManyCar
Car
Has ManySecurity
Security
Belongs ToCar
Garage
Has ManySecurity
ThroughCar
In Eloquent you can just go ahead and clap those relations in, it should work given the schema you posted above.
class Garage extends Eloquent
{
public function cars()
{
return $this->hasMany('cars');
}
public function securities()
{
return $this->hasManyThrough('Security', 'Car');
}
}
class Car extends Eloquent
{
public function securities()
{
return $this->hasMany('Security');
}
// ADDED IN SECOND EDIT
public function garage()
{
return $this->belongsTo('Garage');
}
}
class Security extends Eloquent
{
public function car()
{
return $this->belongsTo('Car');
}
}
And that should be it.
EDIT: You can access all these relations from any model as long as there is a path you can draw from one model to another using a combination of these relations. Check this out for example:
$security = Security::with('car.garage')->first();
will retrieve first Security
record and load Car
relation on it, then it goes one more step and load all Garage
relations on every Car
object loaded under Security
model.
You can access them using this syntax:
$security->car->garage->id
// Other examples
$garage = Garage::with('cars.securities')->first();
foreach($garage->cars as $car)
{
foreach($cars->securities as $security)
{
echo "Car {$car->id} has {$security->id} assigned to it";
}
}
Furthermore, notice that the relationship objects are an instance of Collection
so you have all the fancy methods such as ->each()
, ->count()
, ->map()
available on them.
這篇關(guān)于有一個通過 Laravel Eloquent的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!