下面有三个关系表。
https://drive.google.com/file/d/1q1kdURIwFXxHb2MgdRyBkE1e3DMug7r-/view?usp=sharing
我还有三个单独的模型,它们在我的所有表之间定义了关系。我可以使用hasManyThrough()
关系从国家模型中读取城市模型的信息,但不能从城市模型中读取国家信息。我试图使用``hasManyThrough`''来检索City模型,但是没有得到结果(作为注释的国家方法附带)。请在这里阅读我的模型及其关系方法。
是否有人帮助我使用口才方法hasManyThrough / hasManyThrough
或使用hasManyThrough / hasManyThrough
的反函数来获取City模型的信息?
01。
<?php
namespace App\Hrm;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Country extends Model
{
//use SoftDeletes;
protected $fillable = ['name','description','status'];
public function districts(){
return $this->hasMany(District::class);
}
public function cities(){
return $this->hasManyThrough(City::class,District::class);
}
}
02。
<?php
namespace App\Hrm;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class District extends Model
{
//use SoftDeletes;
protected $fillable = ['country_id','name','description','status'];
public function country(){
return $this->belongsTo(Country::class);
}
public function cities(){
return $this->hasMany(City::class);
}
}
3。
namespace App\Hrm;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class City extends Model
{
//use SoftDeletes;
protected $fillable = ['district_id','name','description','status'];
public function district(){
return $this->belongsTo(District::class);
}
// public function country(){
// return $this->hasOneThrough(Country::class, District::class);
// }
答案 0 :(得分:0)
为什么不能使用父方法?
$city = City::find(1);
$country = $city->district->country();
答案 1 :(得分:0)
在Laravel中,似乎没有一种本地方法可以定义“ hasManyThrough”关系的逆函数。在github上已经打开了几个issues来请求它,但它们已关闭。
如果您不介意为此功能安装第三方软件包,则可以使用staudenmeir/belongs-to-through
软件包。然后,您应该能够像这样定义DatePicker
关系:
<TextBlock Text="{x:Static local:AppResources.HelloWorld}">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility" Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedDate, RelativeSource={RelativeSource AncestorType=DatePicker}}"
Value="{x:Null}">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>