函数无法加载有很多关系数据 laravel 模型 Eloquent
With function not working to load has many relation data laravel Model Eloquent
我有这两个型号。
这个 phone 模型是一个通用模型,可用于保存和获取用户、客户、员工等的 phone 值。所以meta_value用于保存相关模型的id,meta_key用于确定模型名称关系。
/* 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');
}
}
我无法获取带有客户数据的 phones 的数据。它总是 returns
[relations:protected] => Array
(
[phones] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
)
在我的控制器中,我执行以下代码。
/*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;
只有迭代有效,但我认为这不是一个好的解决方案。即使我尝试加载功能,但没有工作。
虽然我怀疑选择 phone_id
作为 id
可能会影响检查关系的查询,但快速调试将是从相关函数中删除它:
->select('id as phone_id','contact_number as phone','contact_number','country_code','type','dial_code')
->where('meta_key','customer');
让函数 phones
变得简单,
public function phones(){
return $this->hasMany('App\Phone','meta_value', 'id');
}
~已更新~
然后,您需要改用load
。
如果以后需要进一步检查关系,则通过回调对其进行过滤:
$customer = Customer::find('448')->load(['phones' => function($query) {
$query->where('meta_key','customer');
}]);
或:
$customer = Customer::with(['phones' => function($query) {
$query->where('meta_key','customer');
}])->find('448');
最初的问题是:
在 find
的结果上使用 with
和 get
会为新查询创建模型的另一个实例,因此您只会获得客户的所有记录。您可以使用 load
方法,或者如果您将使用 with
则必须在查询函数的末尾设置过滤器。
为了更好地理解,请查看 Laravel documentation on Eager Loading
您必须 select 所需的外键列 meta_value
:
->select('id as phone_id','contact_number as phone','contact_number','country_code',
'type','dial_code','meta_value')
对于单个客户,您不需要预先加载:
$customer = Customer::find(448);
$phones = $customer->phones;
看起来你也应该使用 polymorphic relationships。
我有这两个型号。 这个 phone 模型是一个通用模型,可用于保存和获取用户、客户、员工等的 phone 值。所以meta_value用于保存相关模型的id,meta_key用于确定模型名称关系。
/* 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');
}
}
我无法获取带有客户数据的 phones 的数据。它总是 returns
[relations:protected] => Array
(
[phones] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
)
在我的控制器中,我执行以下代码。
/*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;
只有迭代有效,但我认为这不是一个好的解决方案。即使我尝试加载功能,但没有工作。
虽然我怀疑选择 phone_id
作为 id
可能会影响检查关系的查询,但快速调试将是从相关函数中删除它:
->select('id as phone_id','contact_number as phone','contact_number','country_code','type','dial_code')
->where('meta_key','customer');
让函数 phones
变得简单,
public function phones(){
return $this->hasMany('App\Phone','meta_value', 'id');
}
~已更新~
然后,您需要改用load
。
如果以后需要进一步检查关系,则通过回调对其进行过滤:
$customer = Customer::find('448')->load(['phones' => function($query) {
$query->where('meta_key','customer');
}]);
或:
$customer = Customer::with(['phones' => function($query) {
$query->where('meta_key','customer');
}])->find('448');
最初的问题是:
在 find
的结果上使用 with
和 get
会为新查询创建模型的另一个实例,因此您只会获得客户的所有记录。您可以使用 load
方法,或者如果您将使用 with
则必须在查询函数的末尾设置过滤器。
为了更好地理解,请查看 Laravel documentation on Eager Loading
您必须 select 所需的外键列 meta_value
:
->select('id as phone_id','contact_number as phone','contact_number','country_code',
'type','dial_code','meta_value')
对于单个客户,您不需要预先加载:
$customer = Customer::find(448);
$phones = $customer->phones;
看起来你也应该使用 polymorphic relationships。