【问题标题】:How can I create a dual has_many relationship in Laravel?如何在 Laravel 中创建双重 has_many 关系?
【发布时间】:2013-06-01 18:48:54
【问题描述】:

我有一个包含 Employee 表和 Customer 表的数据库。 Employee 表与 Customer 表有 2 个 one_to_many 关系; Customer 表中的外键是“primary_sales_contact_id”和“primary_service_contact_id”。两者显然都引用了 Employee 表上的 id 字段。 如何为此设置迁移,以及随后如何为其创建模型?我是 Laravel 的新手,如果它太明显了,我深表歉意,感谢您的宝贵时间。

【问题讨论】:

    标签: database model migration laravel eloquent


    【解决方案1】:

    员工迁移

    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateEmpoyeeTable extends Migration {
    
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('employee', function(Blueprint $table)
            {
                $table->engine = 'InnoDB';
    
                $table->increments('id');
                $table->string('name');
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('employee');
        }
    
    }
    

    客户迁移

    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;
    
    class CreateCustomerTable extends Migration {
    
        /**
         * Run the migrations.
         *
         * @return void
         */
        public function up()
        {
            Schema::create('customer', function(Blueprint $table)
            {
                $table->engine = 'InnoDB';
    
                $table->increments('id');
                $table->string('name');
                $table->integer('primary_sales_contact_id')->unsigned();
                $table->integer('primary_service_contact_id')->unsigned();
    
                $table->foreign('primary_sales_contact_id')->references('id')->on('employee');
                $table->foreign('primary_service_contact_id')->references('id')->on('employee');
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::drop('customer');
        }
    
    }
    

    员工模型

    class Employee extends Eloquent
    {
        protected $table = 'employee';
    
        public $timestamps = false;
    
        public function customersService() {
            return $this->hasMany('Customer', 'primary_service_contact_id');
        }
    
        public function customersSale() {
            return $this->hasMany('Customer', 'primary_sales_contact_id');
        }
    }
    

    客户模型

    class Customer extends Eloquent
    {
        protected $table = 'customer';
    
        public $timestamps = false;
    
        public function primarySalesContact() {
            return $this->belongsTo('Employee', 'primary_sales_contact_id');
        }
    
        public function primaryServiceContact() {
            return $this->belongsTo('Employee', 'primary_service_contact_id');
        }
    }
    

    所有东西都使用类似:

    $customer = Customer::find(1);
    echo $customer->primaryServiceContact;
    $employee = Employee::find(1);
    echo $employee->customersSale;
    

    【讨论】:

    • 感谢一百万 kr4Y;我知道一旦我看到它会很容易。上帝,我爱 Laravel!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 2016-04-14
    • 2021-09-24
    • 2015-09-21
    相关资源
    最近更新 更多