ララベルは、多くのスルー関係が例と説明した


雄弁な関係は、Laravelフレームワークの最も有用で強力な機能の一つです.ララベルが一番好きな理由の一つです.主にそれを取得したり、非常に簡単で効率的な方法でデータを挿入するのに役立ちます.
我々が知っているように、ラーラベルの関係のいくつかのタイプがあります.我々(開発者)は、最も最初の4つを使用します.これらは、1対1、1対多、1対多(逆)である.
私は多くの人が好きです.正直に言って、私は最もこれらの2つの関係を楽しみます.今日は簡単に例を挙げて説明します.この記事を終えてから、あなたはとてもはっきりしていますように.
それで、我々の主なセクションへジャンプしましょう.
私たちがレストランのアイテム/メニューを作成しているシナリオを持ちましょう、そして、アイテムはタイプに属します、そして、タイプはカテゴリーに属します.
簡単な言葉のカテゴリーでは、多くのタイプと型が多くの項目があります.今、私たちがカテゴリーに属するすべてのアイテムが欲しいなら、我々はcategory_id 項目表で.しかし、アウトアイテムは主にタイプに属します.基本的に項目は直接型に接続されます.
それで、我々が使うべきであるケースは、多くを通してあります.このような関係により、別のモデルを介してデータを取得することができる.我々のシナリオのように、我々は直接タイプからカテゴリーからアイテムを取り出すことができます.
より良い理解のためにGitHub Repository . 必要に応じてこのリポジトリを訪問できます.そこにはたくさんの例がありました.
簡単に理解できるように、とても簡単な例をあげてみます.では、始めましょう.
私が以前に述べたように、我々はカテゴリー、タイプ、およびアイテムで働いています.
それで、私は私の移住を非常に単純にします.

Here is my migration for Category. Let's have a look at this.


<?php

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

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('categories');
    }
}
あなたは私がここでカテゴリー名だけを保ったのを見ることができます.

Let's have look at my Category Model.


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
    ];
}

Here is my types migration. Let's have a look.


<?php

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

class CreateTypesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('types', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->unsignedInteger('category_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('types');
    }
}
私はただcategory_id 外部キーとして.したがって、カテゴリはタイプに接続されます.

Here is the Type model.


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Type extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'category_id'
    ];
}
それはとても簡単かもしれない.

Let's jump to my Items Migration.


<?php

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

class CreateItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->longText('description');
            $table->unsignedInteger('type_id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('items');
    }
}
それで、ここで、あなたは私がちょうど保ったのを見ることができますtype_id 外部キーとして.だからタイプはアイテムと接続されます.私は何も守らなかったcategory_id 項目移行.ここでは、項目が直接カテゴリと接続されていないことを理解する必要が主な事実です.アイテムはカテゴリを介してカテゴリに接続されます.そういうわけで、それは呼ばれる理由が多くの終わりまでの関係です.

Now here is the model for Item.


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Item extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'description', 'type_id'
    ];
}
今、私たちはカテゴリーからアイテムまでの多くの相互関係を構築します.

Let's create this relation to Category model.


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
    ];

    /**
     * Get all of the items for the user.
     */
    public function items()
    {
        return $this->hasManyThrough(Item::class, Type::class);
    }
}
ヒアitems 型によってカテゴリに属するすべての項目を取得するリレーションシップです.

You can also use relation like this:


 return $this->hasManyThrough(
        Item::class,
        Type::class,
        'category_id', // Foreign key on the types table...
        'type_id', // Foreign key on the items table...
        'id', // Local key on the users table...
        'id' // Local key on the categories table...
 );
そこに行く.私たちの多くの相互関係が正常に構築されている.
私は、あなたにあなたのより良い理解のためにこの関係を使うことができるもう一つのシナリオを与えたいです.

Scenario


私たちには3つのモデルがあると仮定します.
チーム( id , userrest id , name )
user ( ID , name )
ゴール(ID、ユーザー単位ID、目標のナンバー)
だから、この関係はこうです.
チームhasmanyユーザteam_id ユーザモデル
ユーザーhasmanyゴールuser_id インサイドゴールモデル
この関係から、ユーザモデルは仲介モデルであることが分かる.
保存できませんgoal_id チームテーブルに直接goal_id ユーザー表で.
したがって、現在、ユーザーモデルはチームモデルとの関係にあります.したがって、ユーザモデルの中にteam_id .
最後に、チームがどのように多くのゴールを作成したかにアクセスする必要がある場合は、ユーザーモデルを使用できます.
そのようなシナリオがあなたのユースケースのために生成されるならば、そして、あなたが定義する必要があるならば、多くの終わりまでの関係があります.
今ははっきりしていますように.

読解
  • Laravel Documentaiton