簡單地說,這段代碼就是我的獲取數據解決方案,這個模型是關係,我的結果是真實的,但是我無法從關係中獲取數據並顯示在視圖上,Laravel從關係中獲取數據並在視圖中顯示
$data = UserAccountNumber::with(['currency_type'])->orderBy('id', 'DESC')->paginate(50);
以下結果這個代碼回報:
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'模型? – codedge
@codedge是先生。發佈更新。 –
我仍然無法看到任何'類UserAccountNumber'模型類.. – codedge