首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取与注册相关的注册类型的名称和价格?(雄辩)

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

Stack Overflow用户
提问于 2018-05-24 15:46:15
回答 1查看 115关注 0票数 0

当用户单击“支付”按钮时,他被重定向到像"http://proj.test/conference/2/conference-title/payment/registration/6“这样的支付页面,其中6是用户要支付的注册id,"2”是会议id,因此用户使用id "2“支付有关会议的注册。

在注册付款页面中应出现注册摘要。例如,如果用户在具有3种票证/注册类型、1种类型为"general“和2种类型为"plus”的会议上进行了注册,则应该出现一个摘要,如下所示:

代码语言:javascript
复制
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$

因此,当用户单击“支付”按钮时,我有此路由:

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

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

代码语言:javascript
复制
public function showSummary($id = "", $slug = "", $regID){

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

怀疑:我的疑问是如何更改以下查询,以获取与注册相关的每种注册类型的名称和价格,以及获取数量,以便能够像上面的摘要那样显示一个摘要。

通过以下方式:

代码语言:javascript
复制
$registrationTypeDetails = Registration::with([
            'participants' => function ($query) {
                $query->select('id', 'registration_type_id', 'registration_id')->where('registration_id', $regID);
            }
        ])->find($regID);

        dd($registrationTypeDetails);

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

代码语言:javascript
复制
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,因此应该可以获得关于每种注册类型的信息,但我不知道如何更改查询以实现这一点。

你知道如何做到这一点吗?

问题的相关表格结构:

代码语言:javascript
复制
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

这一问题的相关模式:

用户模型:

代码语言:javascript
复制
 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');
        }
    }

会议模式:

代码语言:javascript
复制
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');
    }
}

登记类型模型:

代码语言:javascript
复制
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');
    }
}

登记模式:

代码语言:javascript
复制
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');
    }
}

参与者模型:

代码语言:javascript
复制
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');
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-08 17:56:41

代码语言:javascript
复制
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()):

代码语言:javascript
复制
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
      ]
    ]
  ]
]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50513393

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档