如何获得与注册相关的注册类型的名称和价格? (雄辩)

时间:2018-05-24 15:46:16

标签: php laravel eloquent

当用户点击付费按钮时,他会被重定向到支付页面,例如“http://proj.test/conference/2/conference-title/payment/registration/6”,其中6是用户想要支付的注册ID,“2”是会议ID,所以用户正在支付关于会议的注册,ID为“2”。

在注册付款页面中应出现注册摘要。例如,如果用户在会议中注册了3个票据/注册类型,1个类型为“常规”,2个类型为“加号”,则应显示如下摘要:

Title of the conference (ex: Conference test)
Date of the conference

Registration Type   Quantity         Price     Subtotal
 general             1            0.00 €      0.00$
 plus                2            1.00 €      2.00$

所以当用户点击“付款”按钮时,我有这条路线:

Route::get('/conference/{id}/{slug?}/payment/registration/{regID}', [
    'uses' => 'PaymentController@showSummary',
    'as'   =>'conferences.showSummary'
]);

在showSummary()中,我已经获得了会议名称和日期:

public function showSummary($id = "", $slug = "", $regID){

    $registration = Registration::with([
        'conference' => function ($query) {
            $query->select('id', 'name', 'date');
        }
    ])->find($regID);       
}

怀疑:我的疑问是如何更改以下查询以获取与注册相关联的每种注册类型的名称和价格,并且还获取数量以便可以显示类似的摘要上面的摘要。

使用:

$registrationTypeDetails = Registration::with([
            'participants' => function ($query) {
                $query->select('id', 'registration_type_id', 'registration_id')->where('registration_id', $regID);
            }
        ])->find($regID);

        dd($registrationTypeDetails);

$ registrationTypeDetails显示与注册相关联的参与者:

Registration {#264 ▼
...
  #relations: array:1 [▼
    "participants" => Collection {#261 ▼
      #items: array:2 [▼
        0 => Participant {#269 ▼
          #fillable: array:4 [▶]
          ...
          #attributes: array:3 [▼
            "id" => 3
            "registration_type_id" => 2
            "registration_id" => 2
          ]
         ...
        }
        1 => Participant {#271 ▼
          #fillable: array:4 [▶]
          ....
          #attributes: array:3 [▼
            "id" => 4
            "registration_type_id" => 3
            "registration_id" => 2
          ]
         ....
        }
      ]
    }
  ]
}

因此,通过上述查询可以获得与注册相关联的参与者的registration_type_id,因此应该可以获取有关每种注册类型的信息,但我不了解如何更改查询以实现该目的。

你知道如何实现这个目标吗?

问题的相关表格结构:

Registration: id, status, conference_id, main_participant_id   
    (main_participant_id is the id of the user that did the regitration)

Registration Types: id, name, price, conference_id

Conference: id, name, date, organizer_id

Participant: id, registration_id, registration_type_id, name, surname

问题的相关模型:

用户模型:

 class User extends Authenticatable
    {
        public function conferences(){
            return $this->hasMany('App\Conference', 'organizer_id');
        }
        public function registrations(){
            return $this->hasMany('App\Registration','main_participant_id');
        }
    }

会议模式:

class Conference extends Model
{
    public function organizer(){
        return $this->belongsTo('App\User', 'organizer_id');
    }
    public function registrationTypes(){
        return $this->hasMany('App\RegistrationType', 'conference_id');
    }
    public function registrations(){
        return $this->hasMany('App\Registration', 'conference_id');
    }
}

注册类型模型:

class RegistrationType extends Model
{
    public function conference(){
        return $this->belongsTo('App\Conference');
    }

    // a registration can have many participants
    public function participants(){
       return $this->hasMany('App\Participant');
    }
}

注册模式:

class Registration extends Model
{
    // a registration has one user that do the registration
    public function customer(){
        return $this->belongsTo(User::class, 'main_participant_id', 'id');
    }

    // a registration can have many participants
    public function participants(){
       return $this->hasMany('App\Participant');
    }

    public function conference(){
        return $this->belongsTo('App\Conference');
    }

    public function payment()
    {
        return $this->hasOne('App\Payment');
    }
}

参与者模型:

class Participant extends Model
{

    // a participant belongs to a registration
    public function registration(){
        return $this->belongsTo('App\Registration');
    }

    public function registration_type(){
        return $this->belongsTo('App\RegistrationType');
    }
}

1 个答案:

答案 0 :(得分:1)

class PaymentController extends Controller
{
    public function showSummary($id = "", $slug = "", $regID){


        $registrationTypeDetails = Registration::with(['participants.registration_type',
        'participants' => function ($query) use($regID){
            $query->select('id', 'registration_type_id', 'registration_id')->where('registration_id', $regID);
        }
        ])->find($regID);

        dd($registrationTypeDetails->toArray());

}
}

现在我转换为输出array($ registrationTypeDetails-> toArray()):

array:7 [▼
  "id" => 1
  "status" => 1
  "conference_id" => 1
  "main_participant_id" => 1
  "created_at" => "2018-05-28 00:00:00"
  "updated_at" => "2018-05-28 00:00:00"
  "participants" => array:1 [▼
    0 => array:4 [▼
      "id" => 1
      "registration_type_id" => 1
      "registration_id" => 1
      "registration_type" => array:4 [▼
        "id" => 1
        "name" => "general"
        "price" => 10000
        "conference_id" => 1
      ]
    ]
  ]
]