<i id='RdVTp'><tr id='RdVTp'><dt id='RdVTp'><q id='RdVTp'><span id='RdVTp'><b id='RdVTp'><form id='RdVTp'><ins id='RdVTp'></ins><ul id='RdVTp'></ul><sub id='RdVTp'></sub></form><legend id='RdVTp'></legend><bdo id='RdVTp'><pre id='RdVTp'><center id='RdVTp'></center></pre></bdo></b><th id='RdVTp'></th></span></q></dt></tr></i><div id='RdVTp'><tfoot id='RdVTp'></tfoot><dl id='RdVTp'><fieldset id='RdVTp'></fieldset></dl></div>

    • <bdo id='RdVTp'></bdo><ul id='RdVTp'></ul>
    <legend id='RdVTp'><style id='RdVTp'><dir id='RdVTp'><q id='RdVTp'></q></dir></style></legend>

    <small id='RdVTp'></small><noframes id='RdVTp'>

    <tfoot id='RdVTp'></tfoot>
    1. Laravel Eloquent LEFT JOIN WHERE NULL

      时间:2023-09-24
      <tfoot id='h367l'></tfoot>
        <tbody id='h367l'></tbody>
        <bdo id='h367l'></bdo><ul id='h367l'></ul>
        <i id='h367l'><tr id='h367l'><dt id='h367l'><q id='h367l'><span id='h367l'><b id='h367l'><form id='h367l'><ins id='h367l'></ins><ul id='h367l'></ul><sub id='h367l'></sub></form><legend id='h367l'></legend><bdo id='h367l'><pre id='h367l'><center id='h367l'></center></pre></bdo></b><th id='h367l'></th></span></q></dt></tr></i><div id='h367l'><tfoot id='h367l'></tfoot><dl id='h367l'><fieldset id='h367l'></fieldset></dl></div>

      • <small id='h367l'></small><noframes id='h367l'>

        • <legend id='h367l'><style id='h367l'><dir id='h367l'><q id='h367l'></q></dir></style></legend>

              1. 本文介绍了Laravel Eloquent LEFT JOIN WHERE NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试使用 Eloquent 在数据库种子期间执行以下查询:

                I'm trying to use Eloquent to perform the following query during a database seed:

                SELECT
                    *
                FROM
                    customers
                LEFT JOIN
                    orders
                    ON customers.id = orders.customer_id
                WHERE
                    orders.customer_id IS NULL
                

                这是我在 Eloquent 中的实现:

                And here is my implementation in Eloquent:

                $c = Customer::leftJoin('orders', function($join) {
                      $join->on('customers.id', '=', 'orders.customer_id');
                    })
                    ->whereNull('orders.customer_id')
                    ->first();
                

                第一个查询总是返回完整的结果,而 Eloquent 的等效项总是返回空元素,除了 customersemailphone 字段> 桌子.我无法解释这一点,因为 CustomersOrders 模型都是工匠生成的骨架.

                Whereas the first query always returns full results, the Eloquent equivalent always returns empty elements for everything but the email and phone fields of the customers table. I'm at a loss to explain this since the Customers and Orders models are both artisan generated skeletons.

                例如:

                class Customer extends Eloquent {
                
                    // Add your validation rules here
                    public static $rules = [
                        // 'title' => 'required'
                    ];
                
                    // Don't forget to fill this array
                    protected $fillable = [];
                
                }
                

                这是当我 dd() 对种子(最初由 Faker 生成)第一个 Eloquent 查询时输出的数组:

                Here is the array that is output when I dd() the first Eloquent query on a seed (generated originally by Faker):

                protected $original =>
                  array(25) {
                    'id' =>
                    NULL
                    'first_name' =>
                    NULL
                    'last_name' =>
                    NULL
                    'email' =>
                    string(24) "luther.braun@example.org"
                    'phone' =>
                    string(17) "642.150.9176x5684"
                    'address1' =>
                    NULL
                    'address2' =>
                    NULL
                    'city' =>
                    NULL
                    'state' =>
                    NULL
                    'county' =>
                    NULL
                    'district' =>
                    NULL
                    'postal_code' =>
                    NULL
                    'country' =>
                    NULL
                    'notes' =>
                    NULL
                    'created_at' =>
                    NULL
                    'updated_at' =>
                    NULL
                    'customer_id' =>
                    NULL
                    'total' =>
                    NULL
                }
                

                推荐答案

                这可以通过指定特定表中所需的特定列名来解决,如下所示:

                This can be resolved by specifying the specific column names desired from the specific table like so:

                $c = Customer::leftJoin('orders', function($join) {
                      $join->on('customers.id', '=', 'orders.customer_id');
                    })
                    ->whereNull('orders.customer_id')
                    ->first([
                        'customers.id',
                        'customers.first_name',
                        'customers.last_name',
                        'customers.email',
                        'customers.phone',
                        'customers.address1',
                        'customers.address2',
                        'customers.city',
                        'customers.state',
                        'customers.county',
                        'customers.district',
                        'customers.postal_code',
                        'customers.country'
                    ]);
                

                这篇关于Laravel Eloquent LEFT JOIN WHERE NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel:如何从数据库中获取最后 N 个条目 下一篇:Eloquent 集合:each 与 foreach

                相关文章

                最新文章

                  <bdo id='eqhJn'></bdo><ul id='eqhJn'></ul>
              2. <legend id='eqhJn'><style id='eqhJn'><dir id='eqhJn'><q id='eqhJn'></q></dir></style></legend>
                <i id='eqhJn'><tr id='eqhJn'><dt id='eqhJn'><q id='eqhJn'><span id='eqhJn'><b id='eqhJn'><form id='eqhJn'><ins id='eqhJn'></ins><ul id='eqhJn'></ul><sub id='eqhJn'></sub></form><legend id='eqhJn'></legend><bdo id='eqhJn'><pre id='eqhJn'><center id='eqhJn'></center></pre></bdo></b><th id='eqhJn'></th></span></q></dt></tr></i><div id='eqhJn'><tfoot id='eqhJn'></tfoot><dl id='eqhJn'><fieldset id='eqhJn'></fieldset></dl></div>
              3. <tfoot id='eqhJn'></tfoot>

                1. <small id='eqhJn'></small><noframes id='eqhJn'>