无法加载雄辩模型的关系

时间:2017-12-12 13:36:04

标签: php laravel eloquent

我正在尝试加载通过load()方法使用route model binding检索的模型的关系,但它似乎无法正常工作。关于我做错了什么想法?

public function update(UpdateOrganisationFormRequest $request, Organisation $organisation)
{
    $organisation->load('contacts');

    dd(
        $organisation->contacts, // returns empty collection
        Organisation::first()->contacts // returns values I generated via factory
    );
}

这些答案表明这是正确的方法:

Eager loading with route model binding

Laravel Route model binding with relaionship

https://laracasts.com/discuss/channels/laravel/laravel-model-binding-with-eager-loading

以下是我的关系定义:

class Organisation extends Model
{
    public function contacts()
    {
        return $this->hasMany(OrganisationContact::class);
    }
}

class OrganisationContact extends Model
{
    public function organisation()
    {
        return $this->belongsTo(Organisation::class);
    }
}

我已经考虑过自定义分辨率逻辑,但似乎有点矫枉过正,因为我只需要为此更新过程加载一些关系。

任何帮助将不胜感激!谢谢。

更新

感谢评论,我意识到路由模型绑定没有返回任何内容。对于一些进一步的上下文...我正在运行一个测试,我生成一个Organisation模型,然后点击该资源的更新路由,代码如下所示:

测试类:

/** @test */
public function existing_organisation_can_be_updated()
{
    // Arrange
    $organisation = factory(Organisation::class)->create();

    factory(OrganisationContact::class)->create([
        'organisation_id' => $organisation->id,
    ]);

    /*
        note:
        dd( route('admin.organisation.update', $organisation->id) )
        produces: "http://mydevurl.dev/admin/organisation/1"
    */

    // Act
    $response = $this->put(route('admin.organisation.update', $organisation->id), [
        'name' => 'New name',
    ]);

    // ... assertions and other stuff
}

资源控制器

public function update(UpdateOrganisationFormRequest $request, Organisation $organisation)
{
    // this return null ???
    dd(
        $organisation->id    
    );

    // update logic ...
}

更新2 - 架构

_create_organisations_table.php

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrganisationsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('organisations', function (Blueprint $table) {
            $table->increments('id');
            // some other fields
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('organisations');
    }
}

_create_organisation_contacts

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrganisationContacts extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('organisation_contacts', function (Blueprint $table) {
            $table->increments('id');
            // some other fields
            $table->timestamps();

            // the relationship
            $table->foreign('organisation_id')->references('id')->on('organisations')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('organisation_contacts');
    }
}

1 个答案:

答案 0 :(得分:2)

未通过路由模型绑定检索Organisation模型,因为测试中正在使用WithoutMiddleware特征!

从Laravel 5.3开始,路由模型绑定通过中间件完成:

  

绑定替换中间件

     

现在使用中间件完成路径模型绑定。所有应用程序都应将Illuminate\Routing\Middleware\SubstituteBindings添加到a​​pp / Http / Kernel.php文件中的Web中间件组

     

...

     

https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0

通过使用WithoutMiddleware特征,您将阻止路径模型绑定被触发。

删除WithoutMiddleware以使其按预期运行。

请参阅此处的进一步讨论:https://github.com/laravel/framework/issues/15163