【问题标题】:Relationships between tables in laravel using backpack packagelaravel中使用背包包的表之间的关系
【发布时间】:2016-08-25 18:44:26
【问题描述】:

我正在使用 backpack CRUD 包在 laravel 5.2 中创建我的网站项目

我想在两个表之间建立关系。第一个表称为客户,第二个表称为事务。每个客户都有很多交易(1:N 关系)。

客户表记录:

身份证名称

123456 xyz

交易表记录:

ID 客户ID

101010 123456

我知道我必须在客户模型中指定关系。但是,如何在 CRUD 中显示关系的结果?

【问题讨论】:

    标签: mysql laravel laravel-backpack


    【解决方案1】:

    您应该在 Transaction 和 Customer 模型上都有关系,因此您可以使用 $customer->transactions$transaction->customer

    class Customer extends Model
    {
        /**
         * Get the comments for the blog post.
         */
        public function transactions()
        {
            return $this->hasMany('App\Transactions', 'CustomerID', 'ID');
        }
    }
    

    class Transaction extends Model
    {
        /**
         * Get the comments for the blog post.
         */
        public function customer()
        {
            return $this->belongsTo('App\Customer', 'CustomerID', 'ID');
        }
    }
    

    花一些时间在Eloquent Relationships Documentation。如果您想成为 Laravel 开发人员,了解它们非常重要。

    为了在 CRUD 中显示关系,您可以使用 Backpack 的 select column type 将其显示在表格视图中,并使用 selectselect2 字段类型将其显示在添加/编辑视图中。阅读CRUD Example Entity 以更好地了解其工作原理。

    【讨论】:

      【解决方案2】:

      首先,当您为两个表创建迁移时,包含外键 (FK) 的表必须具有如下字段:

      public function up(){
         $table->increments('id');
         $table->integer('customerID')->unsigned();
      }
      

      之后你需要调用下一个命令到控制台

      php artisan migrate
      

      接下来是下一条命令:

      php arisan backpack:crud customers
      php arisan backpack:crud transactions
      

      之后,您需要在模型中定义从其他表返回值的函数。客户模型需要有下一个功能

      public function transactions(){
         return $this->hasMany('Transaction');
      }
      

      事务模型必须有下一个函数

      public function customer() {
          return $this->belongsTo('Customer');
      }
      

      接下来您必须在客户控制器中添加 CRUD 字段以显示 选择框中的交易。

      $this->crud->addField([
         'label' => 'Transactions', // Label for HTML form field
         'type'  => 'select2',  // HTML element which displaying transactions
         'name'  => 'customerID', // Table column which is FK for Customer table
         'entity'=> 'customer', // Function (method) in Customer model which return transactions
         'attribute' => 'ID', // Column which user see in select box
         'model' => 'Transaction' // Model which contain FK
      ]);
      

      希望这会有所帮助:)

      【讨论】:

        【解决方案3】:

        与事务建立单对多关系后,即可得到结果。

        $customer=Customer::where(['id'=>'123456'])->with('transaction')
                                                   ->first();
        
        print_r($customer->Name);  // gives the customer name
        foreach($customer->transaction as $cid)
        {
             print_r($cid->CustomerID);       // gives the customer id
        }
        

        Laravel Relationships Documentation 总是有帮助的。过一遍。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-18
          • 1970-01-01
          • 2011-04-14
          • 1970-01-01
          • 1970-01-01
          • 2021-09-11
          相关资源
          最近更新 更多