Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSON中的Laravel返回模型关系_Php_Json_Laravel_Laravel 5 - Fatal编程技术网

Php JSON中的Laravel返回模型关系

Php JSON中的Laravel返回模型关系,php,json,laravel,laravel-5,Php,Json,Laravel,Laravel 5,当我尝试用JSON返回模型关系时,我看不到关系字段。这就是我的疑问: $customer_subscriptions = CustomerSubscription::has("customer") ->has("subscription") ->has("federationDiscipline") ->where("customer_id", "=", $customer_id)

当我尝试用JSON返回模型关系时,我看不到关系字段。这就是我的疑问:

$customer_subscriptions = CustomerSubscription::has("customer")
                ->has("subscription")
                ->has("federationDiscipline")
                ->where("customer_id", "=", $customer_id)
                ->whereHas("subscription", function($query) use($company_id) {
                    $query->where("company_id", "=", $company_id);
                })
                ->orderBy("start_date", "asc");

        return $customer_subscriptions;
这就是我的结果:

[0]=>
  array(14) {
    ["id"]=>
    int(2)
    ["customer_id"]=>
    int(1)
    ["subscription_id"]=>
    int(1)
    ["federation_discipline_id"]=>
    int(1)
    ["start_date"]=>
    string(10) "2017-04-01"
    ["end_date"]=>
    string(10) "2017-05-31"
    ["external_id"]=>
    NULL
    ["notes"]=>
    NULL
    ["created_user_id"]=>
    int(1)
    ["updated_user_id"]=>
    NULL
    ["deleted_user_id"]=>
    NULL
    ["created_at"]=>
    string(19) "2017-06-05 07:28:00"
    ["updated_at"]=>
    string(19) "2017-06-05 07:28:00"
    ["deleted_at"]=>
    NULL
  }

我看不到订阅和客户关系字段。查询结果应使用
->has将JSON返回到AJAX

,has
仅作为where条件,它不会将该关系加载到结果集中

您希望将
->与
一起使用

在您的案例中,使用('subscription','FederationDisciply')->

使用
with()
方法在结果中包含关系。例如:

$customer_subscriptions = CustomerSubscription::with("customer")->...
$customer_subscriptions = CustomerSubscription::has("customer")
    ->has("subscription")
    ->has("federationDiscipline")
    ->where("customer_id", "=", $customer_id)
    ->whereHas("subscription", function($query) use($company_id) {
        $query->where("company_id", "=", $company_id);
    })
    ->orderBy("start_date", "asc")
    ->with('customer');  // <--- Eager loading the customer

return $customer_subscriptions;

    return $customer_subscriptions;
或者,在模型上使用
protected$appends=[…]
属性强制为每个查询加载关系。但是,请记住,这将影响使用模型的任何地方的查询,因为它每次都会强制数据库查询这些关系。

必须将这些关系包含在json输出中。当前查询仅在存在关系时才查看,不会加载它们

例如:

$customer_subscriptions = CustomerSubscription::with("customer")->...
$customer_subscriptions = CustomerSubscription::has("customer")
    ->has("subscription")
    ->has("federationDiscipline")
    ->where("customer_id", "=", $customer_id)
    ->whereHas("subscription", function($query) use($company_id) {
        $query->where("company_id", "=", $company_id);
    })
    ->orderBy("start_date", "asc")
    ->with('customer');  // <--- Eager loading the customer

return $customer_subscriptions;

    return $customer_subscriptions;
$customer\u subscriptions=CustomerSubscription::has(“客户”)
->有(“认购”)
->has(“联邦纪律”)
->其中(“客户id”,“=”,$customer\u id)
->whereHas(“订阅”),函数($query)使用($company\u id){
$query->where(“公司id”,“=”,$company\U id);
})
->订购人(“开始日期”、“asc”)

->与('customer');//好的,它起作用了。但是如果我使用with,该模型也可以获得CustomerSubscription,而不需要customer/subscription/FederationCrimination。我可以和两者一起使用吗?是的,你可以两者都使用。好的。但是如果我使用with,该模型也可以获得CustomerSubscription,而不需要customer/subscription/FederationCrimination。我可以和两者一起使用吗?您当然可以同时使用两者,但是,我会将查询更改为
Customer::with(“subscription”)->……
,因为您正在寻找特定的客户。不需要直接查询透视表。