Получить информацию о конгрессе каждого пользователя регистрация в конгрессе не работает - PullRequest
0 голосов
/ 26 апреля 2018

Я хочу показать на странице под плиткой конгресса дату и дату регистрации.

Для этого у меня есть UserController, у которого есть метод index (), который должен получать регистрации в конгрессах.пользователя, а затем из каждой регистрации получить сведения о конгрессе этого конгресса, чтобы можно было отобразить в виде заголовка конгресса, дату и дату регистрации каждой регистрации конгресса.

У меня есть код ниже для этогоно не работает.Знаете ли вы, где проблема?

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

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

// страница, где я хочу показать название конгресса, дату и дату регистрации

<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>

Соответствующие модели к вопросу:

// Конгресс-модель

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');
    }
}

Структура таблиц:

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

Ответы [ 2 ]

0 голосов
/ 26 апреля 2018

Вот проблема:

$registrations = $user->with('registrations.congress');

^ Возвращает конструктор запросов, а не результаты отношений.

То, что вы, вероятно, хотите:

$registrations = $user->registrations()->with('congress')->get();

Это предполагает, что модель User имеет отношение, называемое registrations.

Кроме того, вы передаете $registrations, но зацикливаетесь на $registration, как заметил @Muabazalm.

В следующий раз будет полезно включить и полученную ошибку.

0 голосов
/ 26 апреля 2018

Попробуйте foreach $registrations вместо $registration из-за того, что вы передали View

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...