与功能无法正常工作有很多关系数据laravel Model Eloquent [英] With function not working to load has many relation data laravel Model Eloquent
问题描述
我有这两种模式. 此电话模型是一种通用模型,可用于保存和获取用户,客户,员工等的电话价值.因此,meta_value用于保存相关模型的ID,meta_key用于确定模型名称的关系.
I have these two model. This phone model is a common model which can be used to save and fetch the phone value for a user, customer, Employee and so on. So the meta_value is used to save the id of related model and meta_key is used to determine the model name relation.
/* Customer Model*/
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Customer extends Model {
/**
* Get the Phone List.
*/
public function phones(){
return $this->hasMany('App\Phone','meta_value', 'id')
->select('id as phone_id','contact_number as phone','contact_number','country_code','type','dial_code')
->where('meta_key','customer');
}
}
/* Phone Model*/
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Phone extends Model {
/**
* Get the customer that owns the phone.
*/
public function customer(){
return $this->belongsTo('App\Customer', 'meta_value');
}
}
我无法获得带有客户数据的电话数据.它总是返回
I can't get the data of phones with customer data. It always returns
[relations:protected] => Array
(
[phones] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
)
在我的控制器中,执行以下代码.
In My controller I do the following code.
/*This is the case when I want to get the data for a single customer.*/
$customer = Customer::find('448')->with('phones')->get();
print_r($customer); // It will return all customers with no phones value.
$customer = Customer::find('448');
print_r($customer);die; // It will return a single customer whose id is 448 with no phones value.
print_r($customer->phones);die; // I can get the values of phones by this
$customer = Customer::find('448')->with('phones');
print_r($customer);die; // It will crash my postman when i hit this.
/*This is the case when I want to get the data for multiple customers.*/
$customers = Customer::where('id', '>', '447')->with('phones')->get();
print_r($customers);die; // It will return multiple customer with empty phones value.
// However when I iterate through the loop and get phones then I can get the phone value.
$customers = Customer::where('id', '>', '447')->get();
foreach ($customers as $customer) {
$customer->phones = $customer->phones;
}
print_r($customers);die;
仅迭代有效,但我认为这不是一个好的解决方案.甚至我尝试加载功能,但无法正常工作.
Only the iteration is working but I think that is not a good solution. Even I try load function but not working.
推荐答案
您必须选择所需的外键列meta_value
:
You have to select the required foreign key column meta_value
:
->select('id as phone_id','contact_number as phone','contact_number','country_code',
'type','dial_code','meta_value')
对于单个客户,您无需急于加载:
For a single customer you don't need eager loading:
$customer = Customer::find(448);
$phones = $customer->phones;
看来您应该使用多态关系.
这篇关于与功能无法正常工作有很多关系数据laravel Model Eloquent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!