php - Laravel Eloquent with() 方法适用于 'where' 但不适用于 'Model::find()'

标签 php laravel orm eloquent

我想在 3 个表之间建立关系,使用 Laravel Eloquent with() method . 这是我的代码(关系在模型中设置):

    $request_form = RequestForm::find(7)->with(['RequestFormsCheck' => function ($q) {
        $q->orderBy("created_at", "DESC")->with('RequestFormsCheckOptions');
    }])->get();

    dd($request_form);

但此代码返回所有请求表单,除了仅返回id = 7 这是输出:

Collection {#895 ▼
  #items: array:4 [▼
    0 => RequestForm {#796 ▶}
    1 => RequestForm {#797 ▶}
    2 => RequestForm {#798 ▶}
    3 => RequestForm {#799 ▶}
  ]
}

当我用 ->first() 替换 ->get() 时,它只返回 request form,但它的 id 是 1 :(

但是这段代码效果很好:

        $request_form = RequestForm::where('id', 7)->with(['RequestFormsCheck' => function ($q) {
            $q->orderBy("created_at", "DESC")->with('RequestFormsCheckOptions');
        }])->first();

        dd($request_form->toArray());

这是它的输出(这是我期望得到的):

array:12 [▼
  "id" => 7
  "name" => "Jack"
  "email" => "jack@gmail.com"
  "telephone" => null
  "description" => "..."
  "site" => "http://branch.local/"
  "item_id" => 10
  "lang" => "en"
  "read" => 1
  "created_at" => "2018-05-15 11:09:47"
  "updated_at" => "2018-05-20 05:24:41"
  "request_forms_check" => array:1 [▼
    0 => array:8 [▼
      "id" => 1
      "request_form_id" => 7
      "type" => 2
      "request_forms_check_options_id" => null
      "description" => "custom note"
      "created_at" => "2018-05-15 11:48:36"
      "updated_at" => "2018-05-15 11:48:36"
      "request_forms_check_options" => null
    ]
  ]
]

这些是我的迁移:

    Schema::create('request_forms', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 100)->nullable();
        $table->string('email', 100)->nullable();
        $table->string('telephone', 20)->nullable();
        $table->text('description')->nullable();
        $table->string('site', 100);
        $table->integer('item_id');
        $table->string('lang');
        $table->timestamps();
    });

    Schema::create('request_forms_check_options', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title', 500);
        $table->timestamps();
    });

还有这个

    Schema::create('request_forms_checks', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('request_form_id')->unsigned();
        $table->foreign('request_form_id')->references('id')->on('request_forms')->onDelete('cascade');
        $table->tinyInteger('type')->comment("1: option, 2:description");
        $table->integer('request_forms_check_options_id')->unsigned()->nullable();
        $table->foreign('request_forms_check_options_id')->references('id')->on('request_forms_check_options')->onDelete('cascade');
        $table->text('description')->nullable();
        $table->timestamps();
    });

最佳答案

首先我会推荐给你this answer但为了记录起见,我将根据我的理解再次解释:

当您使用 find() 方法时,模型的实例已经返回(这不是查询生成器),这就是为什么在结果上使用 with() find 将对数据库执行新查询,然后调用 get() 返回整个记录,而调用 first() 将返回第一条记录(按 id)。

我建议了解决此问题的方法,以防您仍想坚持使用 find()

  • find() 在查询之后出现(预先加载)。示例:

    $request_form = RequestForm::with(['RequestFormsCheck' => function ($q) {
        $q->orderBy("created_at", "DESC")->with('RequestFormsCheckOptions');
    }])->find(7);
    
  • 在查找后使用 load()(延迟加载)- 您有结果,然后加载相关模型。示例:

    $request_form = RequestForm::find(7)->load(['RequestFormsCheck' => function ($q) {
        $q->orderBy("created_at", "DESC")->with('RequestFormsCheckOptions');
    }]);
    
  • 在调用最终结果之前坚持对整个查询调用 with()(有效的那个)。我认为这是最好的方法,因为您只需要一个 RequestForm,然后是许多关系

My general understanding where with() works is on query builder, therefore you must still be getting together the query before with() will be relevant (this thought may need to be updated).

您可能需要阅读有关 Eager Loading - in Laravel docs 的更多信息

关于php - Laravel Eloquent with() 方法适用于 'where' 但不适用于 'Model::find()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50432669/

相关文章:

php - Mysql查询语句索引调优

php - 尝试通过 http api 将数据存储在外部 ipfs 节点上,ipfs.infura.io 拒绝我的连接

php - 拉维尔 5 : Alternative for Eloquent's 'orWhere' method for querying collections

c# - 在 VS2012 中使用 EF 4.4 而不是 5.0

来自mysql结果的php多维数组

php - 如何将 file_get_contents 与相对路径一起使用

javascript - 如何根据输入字段中的给定数据计算年龄?

php - 如何将 Doctrine ORM 默认跟踪策略更改为 Deferred Explicit

php - 依赖 ORM 实体上的服务自动注册错误

php - CodeIgniter - 无法访问助手中的 session