功能无法加载有许多关系数据层级模型Eloquent

时间:2018-05-15 08:24:04

标签: php laravel model eloquent eager-loading

我有这两个模型。 此手机型号是一种常用型号,可用于为用户,客户,员工等保存和获取手机价值。因此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');
    }
}

我无法获取包含客户数据的手机数据。它总是返回

[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;

只有迭代才有效,但我认为这不是一个好的解决方案。即使我尝试加载功能但不工作。

2 个答案:

答案 0 :(得分:2)

您必须选择所需的外键列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

答案 1 :(得分:0)

虽然我怀疑选择phone_idid可能会影响查询检查关系,但快速调试将从相关函数中删除它:

->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');

最初的问题是:

with的结果上使用getfind为新查询创建了另一个模型实例,因此您只能获得客户的所有记录。您可以使用load方法,或者如果您使用with,那么您必须在查询函数的末尾设置过滤器。

为了更好地理解,请查看Laravel documentation on Eager Loading