Reputation: 27
I have an attribute house_id, I want to get the address from the house_id and display it in a view.
My view
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'house_id',
],
]) ?>
Thanks
Edit: Here is the solution Thanks to Raditz for the help. Model:
public static function getHouseAddressPayroll($house_id)
{
if ($house_id) { $house = House::findOne($house_id);
$address = $house->address_line_1.', '.$house->city.', '.$house->state.' '.$house->zipcode;
return $address;
} else {
$data = "N/A";
return $data;
}
}
View:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
[
'attribute' => 'first_house',
'value' => House::getHouseAddressPayroll($model->first_house)
],
[
'attribute' => 'second_house',
'value' => House::getHouseAddressPayroll($model->second_house)
],
[
'attribute' => 'third_house',
'value' => House::getHouseAddressPayroll($model->third_house)
],
[
'attribute' => 'fourth_house',
'value' => House::getHouseAddressPayroll($model->fourth_house)
],
],
]) ?>
Upvotes: 0
Views: 1493
Reputation: 177
Your question is very generic. It would be more helpful if you share more info. But I will take a guess at what you trying to achieve.
I assume the address is stored in another table called "house" with a model House. Assuming your current model is called Profile. In Profile model, you would need to have a 1-to-1 or 1-to-many relation function with the House model.
Assuming the relation is 1-to-1. Your relation function would looks something like below:
public function getHouse()
{
return $this->hasOne(House::className(), ['id' => 'house_id']);
}
Now that you have set the relation for both table, in your DetailView widget, you could do something like this:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'house.address'
]
]) ?>
address is the column name in your house table. If you have more than 1 columns that you want to use, you could use a function like below:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
[
'attribute' => 'house_id',
'value' => function ($model, $widget){
return $model->house->address1.'<br/>'.$model->house->address2;
}
],
]
]) ?>
Upvotes: 1