`morphTo` 方法的第一个参数的用例是什么?
What is the use case of the first argument of the `morphTo` method?
根据 this issue,当使用自定义方法名称在 Laravel 上定义多态关系时,morphTo
方法的名称参数没有按预期工作,让我们假设一个简单的多态 table 结构:
posts
id - integer
name - string
users
id - integer
name - string
images
id - integer
url - string
imageable_id - integer
imageable_type - string
和这个模型结构:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
// ...
// It doesn't work as expected
public function picturable1()
{
return $this->morphTo('imageable');
}
// It doesn't work as expected
public function picturable2()
{
return $this->morphTo('imageable', 'imageable_type', 'imageable_id');
}
// It works unexpectedly
public function picturable3()
{
return $this->morphTo(null, 'imageable_type', 'imageable_id');
}
}
加载这些关系时:
$image = \App\Image::with('picturable1')->find(1);
$image->picturable1; // exists and returns null but imageable instance was expected
$image->imageable; // returns imageable instance unexpectedly
$image = \App\Image::with('picturable2')->find(1);
$image->picturable2; // exists and returns null but imageable instance was expected
$image->imageable; // returns imageable instance unexpectedly
$image = \App\Image::with('picturable3')->find(1);
$image->picturable3; // returns imageable instance as expected
$image->imageable; // doesn't exists as expected
所以问题是,morphTo
方法的名称参数的用例是什么?
以及如上例那样自定义关系名称的正确方法是什么?
我想 name
参数允许您自定义存储相关模型的 属性 的名称。
所以在控制器中你应该指定你期望的名称作为关系:
public function picturable1()
{
return $this->morphTo('picturable1', 'imageable_type', 'imageable_id');
// or return $this->morphTo(null, 'imageable_type', 'imageable_id');
}
public function picturable2()
{
return $this->morphTo('picturable2', 'imageable_type', 'imageable_id');
// or return $this->morphTo(null, 'imageable_type', 'imageable_id');
}
添加说明 Laravel 7.x documentation:
If you need to specify custom type
and id
columns for the morphTo
relation, always ensure you pass the relationship name (which should exactly match the method name) as the first parameter:
/**
* Get the model that the image belongs to.
*/
public function picturable()
{
return $this->morphTo(__FUNCTION__, 'imageable_type', 'imageable_id');
}
根据 this issue,当使用自定义方法名称在 Laravel 上定义多态关系时,morphTo
方法的名称参数没有按预期工作,让我们假设一个简单的多态 table 结构:
posts
id - integer
name - string
users
id - integer
name - string
images
id - integer
url - string
imageable_id - integer
imageable_type - string
和这个模型结构:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
// ...
// It doesn't work as expected
public function picturable1()
{
return $this->morphTo('imageable');
}
// It doesn't work as expected
public function picturable2()
{
return $this->morphTo('imageable', 'imageable_type', 'imageable_id');
}
// It works unexpectedly
public function picturable3()
{
return $this->morphTo(null, 'imageable_type', 'imageable_id');
}
}
加载这些关系时:
$image = \App\Image::with('picturable1')->find(1);
$image->picturable1; // exists and returns null but imageable instance was expected
$image->imageable; // returns imageable instance unexpectedly
$image = \App\Image::with('picturable2')->find(1);
$image->picturable2; // exists and returns null but imageable instance was expected
$image->imageable; // returns imageable instance unexpectedly
$image = \App\Image::with('picturable3')->find(1);
$image->picturable3; // returns imageable instance as expected
$image->imageable; // doesn't exists as expected
所以问题是,morphTo
方法的名称参数的用例是什么?
以及如上例那样自定义关系名称的正确方法是什么?
我想 name
参数允许您自定义存储相关模型的 属性 的名称。
所以在控制器中你应该指定你期望的名称作为关系:
public function picturable1()
{
return $this->morphTo('picturable1', 'imageable_type', 'imageable_id');
// or return $this->morphTo(null, 'imageable_type', 'imageable_id');
}
public function picturable2()
{
return $this->morphTo('picturable2', 'imageable_type', 'imageable_id');
// or return $this->morphTo(null, 'imageable_type', 'imageable_id');
}
添加说明 Laravel 7.x documentation:
If you need to specify custom
type
andid
columns for themorphTo
relation, always ensure you pass the relationship name (which should exactly match the method name) as the first parameter:
/**
* Get the model that the image belongs to.
*/
public function picturable()
{
return $this->morphTo(__FUNCTION__, 'imageable_type', 'imageable_id');
}