我想使用area_id获取区域,城镇,区域和国家/地区的名称,该区域迁移到属于特定用户的用户表。
我有5个表,其中包括
包含ID,名称和说明的国家/地区表。
具有id,name,description和country_id
的Region表包含id,name,description和region_id的Town表。
包含id,name,description和town_id的Area表。
我有一个用户表,其中area_id的值迁移到用户表,并具有属性id,name,surname,email,phone,area_id。
如果使用Laravel在数据库的用户表中存在area_id,我希望知道如何获取特定用户的区域,城镇,地区和国家/地区的名称。
在我的用户控制器中,我做了类似这样的事情
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function accueil(Session $session)
{
$recentJobbers =User::with('Area')
->limit(5)
->whereType('jobber')
->orderBy('updated_at', 'desc')
->get();
return view('home', compact('recentJobbers','areas', 'categories'));
}
在我的部分刀片视图中我做了这个
<div class="col-lg-12">
<div class="heading">
<h2>Jobbeurs en vedette</h2>
<span>Principaux jobbers ayant déjà été solicités pour leur travail.</span>
</div><!-- Heading -->
<div class="job-listings-sec">
@foreach($recentJobbers as $recentJobber)
<div class="job-listing">
<div class="job-title-sec">
<div class="c-logo"> <img src="{{url('images/avatars/'. $recentJobber->photo) }}" alt="Image" /> </div>
<h3><a href="{{ route('profileDetails', ['id'=>$recentJobber->id]) }}" title="">{{ $recentJobber->name }} {{ $recentJobber->surname }} <span style="float:right; padding-left: 10px;">(a ete contacte 25 fois)</span></a></h3>
<span>{{ $recentJobber->title }}</span>
</div>
<span class="job-lctn"><i class="la la-map-marker"></i>{{ $recentJobber->area->name }}, {{ $recentJobber->town->name }}, {{ $recentJobber->region->name }}</span>
<span class="fav-job"><i class="la la-heart-o"></i></span>
<a href="{{ route('profileDetails', ['id'=>$recentJobber->id]) }}" class="job-is ft">Voir Profil</a>
</div><!-- Job -->
@endforeach
</div>
</div>
在我的用户模型中,我做了类似的事情
public function category()
{
return $this->belongsTo('App\Category');
}
public function service()
{
return $this->belongsTo('App\Service');
}
public function region()
{
return $this->belongsTo('App\Region');
}
public function town()
{
return $this->belongsTo('App\Town');
}
public function area()
{
return $this->belongsTo('App\Area');
}
但它只返回区域名称。不返回城镇,地区或国家名称。
我想知道实现这一目标的最简单方法是什么。欢迎任何帮助
答案 0 :(得分:0)
您可以做的是在每个模型上建立关系 - 而不是User
上的所有模型
class Region extends Model
{
protected $fillable = ['country_id', 'name', 'description'];
public function country(): BelongsTo
{
return $this->belongsTo(Country::class);
}
}
class Town extends Model
{
protected $fillable = ['region_id', 'name', 'description'];
public function region(): BelongsTo
{
return $this->belongsTo(Region::class);
}
}
class Area extends Model
{
protected $fillable = ['town_id', 'name', 'description'];
public function town(): BelongsTo
{
return $this->belongsTo(Town::class);
}
}
class User extends Authenticatable
{
// ...
public function area(): BelongsTo
{
return $this->belongsTo(Area::class);
}
}
然后,要获得所有这些关系,您可以像这样使用急切加载:
$recentJobbers = User::with('area.town.region.country')
->limit(5)
->whereType('jobber')
->orderBy('updated_at', 'desc')
->get();
现在您可以通过以下方式访问每条记录:
$recentJobber->area->town->region->country->name;
$recentJobber->area->town->region->name;
$recentJobber->area->town->name;
$recentJobber->area->name;
确保使用如上所示的预先加载 - 否则在每次迭代中获取这些关系将非常昂贵。以下是有关该主题的一些其他信息:https://laravel.com/docs/5.6/eloquent-relationships#eager-loading
为了让它看起来更漂亮,您可以为User
模型添加访问者
class User extends Authenticatable
{
// ...
public function area(): BelongsTo
{
return $this->belongsTo(Area::class);
}
public function getTownAttribute(): Town
{
return $this->area->town;
}
public function getRegionAttribute(): Region
{
return $this->area->town->region;
}
public function getCountryAttribute(): Country
{
return $this->area->town->region->country;
}
}
然后像在您的视图示例中那样调用它
$recentJobber->country->name;
$recentJobber->region->name;
$recentJobber->town->name;
$recentJobber->area->name;
这是支持上述
的测试 /**
* @test
*/
public function pulls_area_town_region_and_country()
{
$country = Country::create([
'name' => 'United Kingdom',
'description' => 'United Kingdom'
]);
$region = Region::create([
'country_id' => $country->id,
'name' => 'Somerset',
'description' => 'Somerset'
]);
$town = Town::create([
'region_id' => $region->id,
'name' => 'Bristol',
'description' => 'Bristol'
]);
$area = Area::create([
'town_id' => $town->id,
'name' => 'South',
'description' => 'South'
]);
factory(User::class)->create([
'area_id' => $area->id
]);
$user = User::with('area.town.region.country')->first();
$this->assertEquals('United Kingdom', $user->area->town->region->country->name);
$this->assertEquals('Somerset', $user->area->town->region->name);
$this->assertEquals('Bristol', $user->area->town->name);
$this->assertEquals('South', $user->area->name);
$this->assertEquals('United Kingdom', $user->country->name);
$this->assertEquals('Somerset', $user->region->name);
$this->assertEquals('Bristol', $user->town->name);
$this->assertEquals('South', $user->area->name);
}