使用多个表进行查询(获取注册详情)

时间:2018-06-12 22:00:38

标签: php laravel eloquent

我希望使用laravel进行查询,以便获取有关注册的详细信息。我有这条路线:

Route::get('/conference/{id}/{slug?}/registration/{regID}/info', [
    'uses' => 'RegistrationController@getRegistrationInfo',
    'as'   =>'conferences.registrationInfo'
]);

当用户访问此路线时,我想显示该用户的特定注册的详细信息。

例如,如果用户John W.在会议中进行了注册,其中:

  • 为他选择了1张“普通”的票/登记(John W.)和1张“加号”的票/登记(对于Jake W。)
  • 并且注册类型“general”的价格为“0”
  • 且注册类型“plus”的价格为“1”
  • 会议桌中的
  • 和all_participants为“1”,这意味着需要收集每个参与者的姓名和姓氏,“0”表示只需要收集正在进行注册的用户的姓名和姓氏(auth用户)

在注册表中将插入:

id       status        conference_id         main_participant_id
1          I                 1                          1 

在参与者表中:

id    registration_id    registration_type_id       name        surname
1           1                   1                     John           W
2           1                   2                    Jake            W

注册类型表如下:

id        name       price     ...
1         general       0
2          plus        1

会议桌就像:

id       name                            date
1         conference name        2018-06-13

我希望有一个允许显示特定注册的查询,当用户点击与上述路线“Route :: get('/ / id / {{ / {regID} / info“,显示与该注册ID相关联的每个票证/注册类型,在这种情况下,是2种注册类型(2个参与者),因此查询允许显示包含两个列表项的列表,显示注册信息如:

<ul>
    <li>
        <span>show the registration ID here</span>
        <span>Conference name: conference name</span>
        <span>Conference date: 2018-06-13</span>
        <span>Registration type: general</span>
        <span> Participant: John W</span>
        <span>Price: 0<span>
    </li>
    <li>
        <span>show the registration ID here</span>
        <span>Conference name: conference name</span>
        <span>Conference date: 2018-06-13</span>
        <span>Registration type: plus</span>
        <span> Participant: Jake W</span>
        <span>Price: 1<span>
    </li>
<ul>

你知道如何实现这一目标吗?我不明白如何正确地做这件事,如果它应该只是一个查询或多个查询。你知道如何正确地实现这个上下文的查询吗?

问题的相关模型:

会议模式:

class Conference extends Model
{
    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');
    }

    public function participants(){
        return $this->hasMany('App\Participant');
    }

    public function registrations(){
        return $this->belongsToMany('App\Registration', 'registration_registration_types');
    }
}

注册模式:

class Registration extends Model
{

    public function customer(){
        return $this->belongsTo(User::class, 'main_participant_id', 'id');
    }

    public function participants(){
        return $this->hasMany('App\Participant');
    }

    public function registration_types(){
        return $this->belongsToMany('App\RegistrationType', 'registration_registration_types');
    }

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

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

参与者模型:

class Participant extends Model
{
    public function registration(){
        return $this->belongsTo('App\Registration');
    }
    public function registration_type(){
        return $this->belongsTo('App\RegistrationType');
    }

}

用户模型:

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

喜欢:

public function getRegistrationInfo($regID){


        $q = Registration::
         with('conference', 'registration_types.participants')
         ->find($regID);

    }

它显示:

Table 'project.registration_registration_types'
doesn't exist (SQL: select `registration_types`.*, 
`registration_registration_types`.`registration_id` as 
`pivot_registration_id`, 
`registration_registration_types`.`registration_type_id` as 
`pivot_registration_type_id` from `registration_types` inner join 
`registration_registration_types` on `registration_types`.`id` = 
`registration_registration_types`.`registration_type_id` where 
 `registration_registration_types`.`registration_id` in (1))

2 个答案:

答案 0 :(得分:1)

操作。我在import matplotlib.pyplot as plt import numpy as np import pylab as pl import math data = open('input.dat') lines = data.readlines() data.close() x1=[] y1=[] z1=[] plt.plot(1) for line in lines[2:]: p= line.split() x1.append(3*math.sqrt(float(p[0]))) y1.append(3*math.sqrt(float(p[1]))) z1.append(3*math.sqrt(float(p[2]))) x=np.array(x1) y=np.array(y1) z=np.array(z1) plt.subplot(311) plt.plot(x,'b',label=" X figure ") plt.subplot(312) plt.plot(y,'r',label=" Y figure ") plt.subplot(313) plt.plot(x,z,'g',label=" X,Z figure ") plt.show() 模型和函数Registration中看到您调用registration_types但该函数的参数如下:

BelongsToMany

可能是您错误的表belongsToMany($related, $table, $foreignKey, $relatedKey, $relation)

答案 1 :(得分:1)

我希望我完全明白你的意思

controller

<?php

namespace App\Http\Controllers;

use App\Registration;
use Illuminate\Http\Request;

class RegistrationController extends Controller
{
    public function getRegistrationInfo($id ,$slug, $regID)
    {

        $registration = Registration::with('conference','Conference.registrationTypes','Conference.registrationTypes.participants')
        ->where('id',$regID)->first();
    return view('your_view_name',compact('registration'));

}

}

您的view

<ul>
@foreach($registration->conference->registrationTypes as $key=>$registrationType)

     <li>
             <span>show the registration ID here : {{$registration->id}}</span> <br>
             <span>Conference name: conference name : {{$registration->conference->name}}</span><br>
             <span>Conference date: {{$registration->conference->date}}</span><br>
             <span>Registration type: {{$registration->conference->registrationTypes[$key]['name']}}</span><br>
             <span> Participant: {{$registration->conference->registrationTypes[$key]->participants[0]->name .' '.$registration->conference->registrationTypes[$key]->participants[0]->surname}}</span><br>
             <span>Price: {{$registration->conference->registrationTypes[$key]['price']}}</span><br>
    </li>

@endforeach
</ul>

输出:

<ul>

         <li>
                 <span>show the registration ID here : 1</span> <br>
                 <span>Conference name: conference name : conferanse shomare 1</span><br>
                 <span>Conference date: 2018-06-06 00:00:00</span><br>
                 <span>Registration type: general</span><br>
                 <span> Participant: John w</span><br>
                 <span>Price: 0</span><br>
        </li>


         <li>
                 <span>show the registration ID here : 1</span> <br>
                 <span>Conference name: conference name : conferanse shomare 1</span><br>
                 <span>Conference date: 2018-06-06 00:00:00</span><br>
                 <span>Registration type: plus</span><br>
                 <span> Participant: Jake w</span><br>
                 <span>Price: 1</span><br>
        </li>

</ul>