如何在#myTickets div中显示注册信息?

时间:2018-04-25 21:39:01

标签: laravel

我有一个页面" http://proj.test/user/profile?user=1"用户可以编辑与其帐户相关的内容。此页面的路线是:

Route::get('/user/profile', [
    'uses' => 'UserController@index',
    'as'   =>'user.index'
]);

在UserController的索引方法中只返回视图:

 public function index(){
        return view('users.index')->with('user', Auth::user());
    }

在此页面(http://proj.test/user/profile?user=1)中,有一些标签可用于编辑/管理用户帐户的不同方面(常规信息,登录信息,我的票证)。

的疑问:

我的疑问是在" #myTickets"选项卡我想显示用户拥有的注册。所以我在UserController中创建了一个方法,如下所示。我的疑问是如何在选项卡" My Tickets"中正确调用此方法来显示此信息。单击,如何处理这些路线。因为没有使用getRegistrations方法。

public function getRegistrations(){
        $user = Auth::user();
        $registrations = User::find($user->id)->registrations;

        dd($registrations);
        return view('users.index')->with('registrations', $registrations);
    }

//标签链接

<ul class="nav nav-pills bg-light-gray account_options" role="tablist">
     <li class="">
        <a class="nav-link active" href="#generalInfo" data-toggle="tab" role="tab">
            <i class="fa fa-user" aria-hidden="true"></i> <span class="d-none d-lg-inline-block">General Info</span></a>
    </li>
    <li class="disabled">
        <a class="nav-link" href="#myTickets" data-toggle="tab" role="tab">>MyTickets</span></a>
    </li>
    ...
    </ul>

然后标签如下:

 <div class="tab-content  bg-white" id="myTabContent">
        <div class="tab-pane fade show active clearfix" id="generalInfo" role="tabpanel" aria-labelledby="home-tab">
            <form method="post" action="{{route('user.updateGeneralInfo')}}" class="clearfix">
            {{csrf_field()}}
            ...
            </form>
        </div>

        <div class="tab-pane clearfix fade" id="myTickets" role="tabpanel" aria-labelledby="contact-tab">
        ...
        @foreach($registration as $reg)
        <ul>
          <li>here is to show congress title</li>
          <li>here is to show congress date</li>
          <li>here is to show registration date</li>
          ...
        </ul>
        @endforeach
        </div>
    </div>

问题的相关模型:

// Congress model
class Congress extends Model
{
    // A conference has many registration types
    public function ticketTypes(){
        return $this->hasMany('App\TicketType', 'congress_id');
    }

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

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

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

}

// TicketTypeModel
class TicketType extends Model
{

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

// user model
class User extends Authenticatable
{
    // A user can register in many conferences
    public function registrations(){
        return $this->hasMany('App\Registration','main_user_id');
    }

}

表格结构:

registrations table: id, status, congress_id, created_at
congresses table: id, name, created_at,...
ticket type table:id, name, congres_id, created_at, ....

1 个答案:

答案 0 :(得分:0)

public function index() {
    $user = Auth::user();
    $registrations = $user->with('registrations.congress');

    return view('users.index', compact('user', 'registrations'));
}