Php 无法加载雄辩的模型';s关系
我正在尝试为使用Php 无法加载雄辩的模型';s关系,php,laravel,eloquent,Php,Laravel,Eloquent,我正在尝试为使用load()方法检索的模型加载关系,但它似乎不起作用。你知道我做错了什么吗 public function update(UpdateOrganisationFormRequest $request, Organisation $organisation) { $organisation->load('contacts'); dd( $organisation->contacts, // returns empty collectio
load()
方法检索的模型加载关系,但它似乎不起作用。你知道我做错了什么吗
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
);
}
这些答案表明,这是正确的方法:
以下是我的关系定义:
class Organisation extends Model
{
public function contacts()
{
return $this->hasMany(OrganisationContact::class);
}
}
class OrganisationContact extends Model
{
public function organisation()
{
return $this->belongsTo(Organisation::class);
}
}
我已经考虑过定制解析逻辑,但似乎有点过头了,因为我只需要为这个更新过程加载一些关系
任何帮助都将不胜感激!多谢各位
更新
由于这些评论,我意识到路由模型绑定没有返回任何内容。为了进一步了解情况。。。我正在运行一个测试,生成一个组织
模型,然后点击此资源的更新路径,代码如下所示:
测试类:
/** @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-模式
_创建_organizations_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');
}
}
未通过路由模型绑定检索组织
模型,因为测试中正在使用无中间件
特性
从Laravel 5.3开始,路由模型绑定通过中间件完成:
绑定替换中间件
路由模型绑定现在使用中间件完成。所有应用程序都应在app/Http/Kernel.php文件中将illumb\Routing\Middleware\SubstituteBindings
添加到web中间件组中
通过使用WithoutMiddleware
特性,您将停止触发路由模型绑定
在没有中间件的情况下删除,
,使其按预期运行
请参阅此处的进一步讨论:是否确定参数中的$organization
和organization::first()
相同?请显示您的模式。您可能还想检查该特定组织是否有联系人。请尝试此organization::find($organization->id)->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');
}
}