Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉雷维尔雄辩的一对多关系_Php_Database_Laravel_Orm_Eloquent - Fatal编程技术网

Php 拉雷维尔雄辩的一对多关系

Php 拉雷维尔雄辩的一对多关系,php,database,laravel,orm,eloquent,Php,Database,Laravel,Orm,Eloquent,我不知道如何使用elount定义一对多关系 这里有桌子 ----------- Country ----------- id | title | code | currency_code ----------- Currency Code -------- id | name | symbol | code 一个国家可能有多种货币 因此,我将我的模型定义如下 class Country extends Model { public function currencies()

我不知道如何使用
elount
定义一对多关系

这里有桌子

----------- Country -----------
id | title | code | currency_code

----------- Currency Code --------
id | name | symbol | code 
一个国家可能有多种货币

因此,我将我的模型定义如下

class Country extends Model
{
    public function currencies()
    {
        $this->hasMany('App\Models\Currency', 'code', 'currency_code');
    }

}
货币模型也很简单

class Currency extends Model
{


}
我选择这样的国家

   return Country::all('country_id AS value', 'title_ru AS title','currency_code')
   ->sortBy('value');
但当我尝试访问货币时,它返回null

如果我的定义有什么问题,我将不胜感激

谢谢你应该试试这个:

国家/地区模式

class Country extends Model
{
    public function currencies()
    {
        $this->belongsTo('App\Models\Currency', 'currency_code');
    }

}
class Currency extends Model
{
    public function country()
    {
        $this->belongsTo('App\Models\Country');
    }

}
货币模型

class Country extends Model
{
    public function currencies()
    {
        $this->belongsTo('App\Models\Currency', 'currency_code');
    }

}
class Currency extends Model
{
    public function country()
    {
        $this->belongsTo('App\Models\Country');
    }

}

您可以按照以下代码设置模型:

国家/地区型号:

class Country extends Model
{
    protected $table = 'country';
    protected $primaryKey = 'id';

    public function currency(){
        return $this->hasMany(App\Models\Currency', 'code', 'currency_code');
    }

}
class Currency extends Model
{
    protected $table = 'currency';
    protected $primaryKey = 'id';

    public function country(){
        return $this->belongTo('App\Models\Country', 'code');
    }

}
货币模型:

class Country extends Model
{
    protected $table = 'country';
    protected $primaryKey = 'id';

    public function currency(){
        return $this->hasMany(App\Models\Currency', 'code', 'currency_code');
    }

}
class Currency extends Model
{
    protected $table = 'currency';
    protected $primaryKey = 'id';

    public function country(){
        return $this->belongTo('App\Models\Country', 'code');
    }

}
然后,如果您想获得特定的国家/地区数据(使用货币),您可以使用:

$data = County::where('id','=',$id)->with('currency')->first();
或者如果你想得到你能使用的一切:

$data = County::with('currency')->get();

这些文件提供了很好的例子,请阅读@Lukeramasden我已经阅读了好几次文档,但都不起作用。您可能需要在查询中加载货币。检查文档并查看哪些应用程序如何/在哪里访问
货币
?感谢您的回答,但我收到以下错误
select*from
cdr\u currencies`where
cdr\u currencies
id
为空`@bxfvgekd:请告诉我全部错误,我将尝试解决此问题