morphTo
Laravel은 다수의 다형성 관계를 지원하여 모델이 다수의 여러 모델에 속할 수있게합니다. 이것은 복잡한 응용 분야에서 특히 유용합니다. morphMany
morphOne
MorphMap
이를 설명하기 위해 주제와 포스트 모델이있는 가상의 장면을 만들어 봅시다. 사용자는 주제와 게시물에 의견을 남길 수 있습니다. 다형성 관계를 사용하여 두 경우 모두 단일 주석 테이블을 사용할 수 있습니다. 놀랍게도 맞습니까? 주석을 구별하기 위해 Post_comments 테이블과 Topic_comments 테이블을 만들어야하기 때문에 이것은 약간 비현실적으로 보입니다. 다형성 관계를 사용하면 두 개의 테이블이 필요하지 않습니다. 실용적인 예를 통해 다형성 관계를 이해합시다.
.
또는
메소드를 사용하십시오. 메소드를 사용하여 모델을 연결하고 모델을 저장할 수 있습니다. 예는 다음과 같습니다.
방법을 통해 다수의 다형성 관계를 지원합니다. 이를 통해 모델이 다수의 여러 모델에 속할 수 있습니다. 열을 추가합니다.
<code>albums
id - integer
name - string
songs
id - integer
title - string
album_id - integer
upvotes
id - integer
upvoteable_id - integer
upvoteable_type - string
</code>
-m
up
{some_timestamp}_create_albums_table.php
<code>php artisan make:model Album -m
php artisan make:model Song -m
php artisan make:model Upvote -m
</code>
{some_timestamp}_create_songs_table.php
<code class="language-php">public function up()
{
Schema::create('albums', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}</code>
{some_timestamp}_create_upvotes_table.php
<code class="language-php">public function up()
{
Schema::create('songs', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('album_id')->unsigned()->index();
$table->timestamps();
$table->foreign('album_id')->references('id')->on('albums')->onDelete('cascade');
});
}</code>
<code class="language-php">public function up()
{
Schema::create('upvotes', function (Blueprint $table) {
$table->increments('id');
$table->morphs('upvoteable'); // 添加无符号整数 upvoteable_id 和字符串 upvoteable_type
$table->timestamps();
});
}</code>
메소드는 이러한 모델과 Upvote 모델 사이의 다형성 일대일 관계를 정의하고 특정 모델 인스턴스의 모든 것을 얻는 데 도움이됩니다. app/Upvote.php
<code>php artisan migrate</code>
app/Album.php
동적 속성을 사용할 수 있습니다.
<code class="language-php">[...]
class Upvote extends Model
{
/**
* 获取所有拥有模型。
*/
public function upvoteable()
{
return $this->morphTo();
}
}</code>
메소드입니다. 그러므로 우리는이 방법에 동적 속성으로 액세스 할 것입니다 : app/Song.php
Upvote 모델에 대한 관계는 앨범 인스턴스의 인스턴스에 따라 앨범 인스턴스를 반환합니다. <code>albums
id - integer
name - string
songs
id - integer
title - string
album_id - integer
upvotes
id - integer
upvoteable_id - integer
upvoteable_type - string
</code>
)? 이것은 우리가 열에서 매우 긴 최대 길이를 설정해야한다는 것을 의미합니다. 여기서 메소드가 우리를 도울 수있는 곳입니다.
upvoteable_type
App\Album
App\Song
App\Models\Data\Topics\Something\SomethingElse
Eloquent의 다형성 관계 MorphMap에 대한 FAQ
또는 morphTo
morphMany
물론 morphOne
<code>albums
id - integer
name - string
songs
id - integer
title - string
album_id - integer
upvotes
id - integer
upvoteable_id - integer
upvoteable_type - string
</code>
다형성 관계는 많은 유연성을 제공하지만 표준 웅변 관계보다 더 복잡하고 설정하고 관리하기가 더 어려울 수 있습니다. 또한 제약 조건을로드하려는 욕구와 같은 표준 관계의 모든 특징을 지원합니다. <code>php artisan make:model Album -m
php artisan make:model Song -m
php artisan make:model Upvote -m
</code>
메소드를 사용하여 다형성 관계에서 관련 레코드를 삭제할 수 있습니다. 예를 들어, 게시물에서 모든 주석을 삭제하려면 다음을 수행 할 수 있습니다.
associate
<code class="language-php">public function up()
{
Schema::create('albums', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}</code>
메소드를 제공합니다.
이것은 테이블에
및
위 내용은 웅변적인 소개의 다형성 관계의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!