Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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 Laravel从关系中获取数据并显示在视图中_Php_Laravel_Properties_Relationship - Fatal编程技术网

Php Laravel从关系中获取数据并显示在视图中

Php Laravel从关系中获取数据并显示在视图中,php,laravel,properties,relationship,Php,Laravel,Properties,Relationship,简单地说,这段代码就是我的获取数据解决方案,这个模型是关系,我的结果是正确的,但我不能从关系中获取数据并在视图中显示出来,例如 $data=UserAccountNumber::with(['currency_type'])->orderBy('id','DESC')->paginate(50); 此代码返回以下结果: LengthAwarePaginator{#585▼ #总数:2 #最后一页:1 #项目:集合{601▼ #项目:阵列:2[▼ 0=>UserAccountNumber{#58

简单地说,这段代码就是我的获取数据解决方案,这个模型是关系,我的结果是正确的,但我不能从关系中获取数据并在视图中显示出来,例如

$data=UserAccountNumber::with(['currency_type'])->orderBy('id','DESC')->paginate(50);
此代码返回以下结果:

LengthAwarePaginator{#585▼
#总数:2
#最后一页:1
#项目:集合{601▼
#项目:阵列:2[▼
0=>UserAccountNumber{#588▶}
1=>UserAccountNumber{#589▼
#表:“用户账号”
#防护:阵列:1[▶]
#连接:空
#主密钥:“id”
#每页:15
+递增:真
+时间戳:真
#属性:数组:9[▼
“id”=>2
“用户id”=>17
“账号”=>“24234234”
“卡号”=>“2423423”
“sheba”=>“asdasdad”
“货币类型”=>5
“状态”=>0
“创建于”=>“2016-05-13 10:55:00”
“更新时间为”=>“2016-05-13 10:55:00”
]
#原件:阵列:9[▶]
#关系:数组:1[▼
“货币类型”=>货币类型{603▼
#表:“货币类型”
#可填充:数组:3[▶]
#日期:数组:1[▶]
#连接:空
#主密钥:“id”
#每页:15
+递增:真
+时间戳:真
#属性:数组:7[▼
“id”=>5
“用户id”=>1
“货币类型”=>“欧元”
“货币符号”=>“欧元”
“创建于”=>“2016-04-17 12:07:47”
“更新时间:2016-04-17 12:07:47”
“已删除”=>空
]
查看:

在视图中,我可以获取
UserAccountNumber
数据,但无法获取
currency\u type
并在视图中显示:


@foreach($key=>$contents形式的数据)
{{$contents->card_number}
{{$contents->card_number}
{{$contents->seba}
{{$contents->created_at}
{{$contents->currency\u type->currency\u type}
@endforeach
我在视图中看到
{{$contents->currency\u type->currency\u type}
的错误

错误:

尝试获取非对象的属性
更新帖子:

class UserAccountNumber扩展模型
{
受保护的$table='用户\帐户\号码';
受保护的$guarded=['id'];
公共函数用户()
{
返回$this->belongsTo('App\Http\Models\User');
}
公共功能货币类型()
{
返回$this->belongsTo('App\Http\Models\CurrencyType','currency\u type','id');
}
}

某些
UserAccountNumber
似乎没有附加
currency\u类型。请在代码中添加类似的内容:

@if (isset($contents->currency_type))
    {{ $contents->currency_type->currency_type }}
@endif
或:


首先要改进的是您的
UserAccountNumber
模型。您的
currency\u-type
关系应该写为:

公共函数currencyType()
{
返回$this->belongsTo(CurrencyType::class,'currency_type','id');
}
然后,您的呼叫将如下所示:

//使用上面的currencyType()方法
$data=UserAccountNumber::with('currencyType')
->orderBy('id','DESC')
->分页(50);
然后,您可以通过以下方式访问您的帐户中的货币类型:

@foreach($key=>$contents形式的数据)
$contents->currencyType->currency\u类型
@endforeach

currency\u type
是一种关系,但是如果有一种适当的关系,即一对多,一种货币类型对多个用户帐号,那么应该总是有一种货币类型,对吗@Alexey?请给我们看一下你的
UserAccountNumber
型号好吗?@codedge是的,先生。更新后。我仍然看不到任何
类用户帐号呃
model class..@codedge这就是我的主题。我认为我的关系不正确。请让我将代码粘贴到帖子中
{{ $contents->currency_type->currency_type or 'There is not type' }}