Laravel 从关系中获取数据并显示在视图中
Laravel get data from relationship and show in view
只是这段代码是我获取数据的解决方案,这个模型是关系,我的结果是正确的,但我无法从关系中获取数据并在视图中显示,例如;
$data = UserAccountNumber::with(['currency_type'])->orderBy('id', 'DESC')->paginate(50);
此代码 return 结果如下:
LengthAwarePaginator {#585 ▼
#total: 2
#lastPage: 1
#items: Collection {#601 ▼
#items: array:2 [▼
0 => UserAccountNumber {#588 ▶}
1 => UserAccountNumber {#589 ▼
#table: "user_account_numbers"
#guarded: array:1 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:9 [▼
"id" => 2
"user_id" => 17
"account_number" => "24234234"
"card_number" => "242423423"
"sheba" => "asdasdad"
"currency_type" => 5
"status" => 0
"created_at" => "2016-05-13 10:55:00"
"updated_at" => "2016-05-13 10:55:00"
]
#original: array:9 [▶]
#relations: array:1 [▼
"currency_type" => CurrencyType {#603 ▼
#table: "currency_type"
#fillable: array:3 [▶]
#dates: array:1 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:7 [▼
"id" => 5
"user_id" => 1
"currency_type" => "EURO"
"currency_symbol" => "EUR"
"created_at" => "2016-04-17 12:07:47"
"updated_at" => "2016-04-17 12:07:47"
"deleted_at" => null
]
查看:
在视图中我可以获取 UserAccountNumber
数据,但我无法获取 currency_type
并在视图中显示:
<table class='table-style' cellpadding='0' cellspacing='0'>
@foreach ($data as $key=>$contents)
<tr>
<td>{{$contents->card_number}}</td>
<td>{{$contents->card_number}}</td>
<td>{{$contents->seba}}</td>
<td>{{$contents->created_at}}</td>
<td>
{{$contents->currency_type->currency_type}}
</td>
</tr>
@endforeach
</table>
我在视图中收到 {{$contents->currency_type->currency_type}}
错误。
错误:
Trying to get property of non-object
POST 更新:
class UserAccountNumber extends Model
{
protected $table = 'user_account_numbers';
protected $guarded = ['id'];
public function user()
{
return $this->belongsTo('App\Http\Models\User');
}
public function currency_type()
{
return $this->belongsTo('App\Http\Models\CurrencyType', 'currency_type', 'id');
}
}
似乎有些 UserAccountNumber
没有附加 currency_type
。在您的代码中添加如下内容:
@if (isset($contents->currency_type))
{{ $contents->currency_type->currency_type }}
@endif
或者:
{{ $contents->currency_type->currency_type or 'There is not type' }}
首先要改进的是您的 UserAccountNumber
模型。你的 currency_type
关系应该这样写:
public function currencyType()
{
return $this->belongsTo(CurrencyType::class, 'currency_type', 'id');
}
您的电话将如下所示:
// using the currencyType() method from above
$data = UserAccountNumber::with('currencyType')
->orderBy('id', 'DESC')
->paginate(50);
然后您可以通过以下方式访问您的货币类型:
@foreach ($data as $key=>$contents)
$contents->currencyType->currency_type
@endforeach
只是这段代码是我获取数据的解决方案,这个模型是关系,我的结果是正确的,但我无法从关系中获取数据并在视图中显示,例如;
$data = UserAccountNumber::with(['currency_type'])->orderBy('id', 'DESC')->paginate(50);
此代码 return 结果如下:
LengthAwarePaginator {#585 ▼
#total: 2
#lastPage: 1
#items: Collection {#601 ▼
#items: array:2 [▼
0 => UserAccountNumber {#588 ▶}
1 => UserAccountNumber {#589 ▼
#table: "user_account_numbers"
#guarded: array:1 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:9 [▼
"id" => 2
"user_id" => 17
"account_number" => "24234234"
"card_number" => "242423423"
"sheba" => "asdasdad"
"currency_type" => 5
"status" => 0
"created_at" => "2016-05-13 10:55:00"
"updated_at" => "2016-05-13 10:55:00"
]
#original: array:9 [▶]
#relations: array:1 [▼
"currency_type" => CurrencyType {#603 ▼
#table: "currency_type"
#fillable: array:3 [▶]
#dates: array:1 [▶]
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:7 [▼
"id" => 5
"user_id" => 1
"currency_type" => "EURO"
"currency_symbol" => "EUR"
"created_at" => "2016-04-17 12:07:47"
"updated_at" => "2016-04-17 12:07:47"
"deleted_at" => null
]
查看:
在视图中我可以获取 UserAccountNumber
数据,但我无法获取 currency_type
并在视图中显示:
<table class='table-style' cellpadding='0' cellspacing='0'>
@foreach ($data as $key=>$contents)
<tr>
<td>{{$contents->card_number}}</td>
<td>{{$contents->card_number}}</td>
<td>{{$contents->seba}}</td>
<td>{{$contents->created_at}}</td>
<td>
{{$contents->currency_type->currency_type}}
</td>
</tr>
@endforeach
</table>
我在视图中收到 {{$contents->currency_type->currency_type}}
错误。
错误:
Trying to get property of non-object
POST 更新:
class UserAccountNumber extends Model
{
protected $table = 'user_account_numbers';
protected $guarded = ['id'];
public function user()
{
return $this->belongsTo('App\Http\Models\User');
}
public function currency_type()
{
return $this->belongsTo('App\Http\Models\CurrencyType', 'currency_type', 'id');
}
}
似乎有些 UserAccountNumber
没有附加 currency_type
。在您的代码中添加如下内容:
@if (isset($contents->currency_type))
{{ $contents->currency_type->currency_type }}
@endif
或者:
{{ $contents->currency_type->currency_type or 'There is not type' }}
首先要改进的是您的 UserAccountNumber
模型。你的 currency_type
关系应该这样写:
public function currencyType()
{
return $this->belongsTo(CurrencyType::class, 'currency_type', 'id');
}
您的电话将如下所示:
// using the currencyType() method from above
$data = UserAccountNumber::with('currencyType')
->orderBy('id', 'DESC')
->paginate(50);
然后您可以通过以下方式访问您的货币类型:
@foreach ($data as $key=>$contents)
$contents->currencyType->currency_type
@endforeach