在大会上获取每个用户注册的会议详细信息是行不通的

时间:2018-04-26 01:45:05

标签: php laravel

我想在会议图块,日期和注册日期下面的页面中显示。

为此,我有一个public function index() { $user = Auth::user(); $registrations = $user->with('registrations.congress'); return view('users.index', compact('user', 'registrations')); } ,它具有index()方法,该方法应该在用户的会议中进行注册,然后从每次注册中获取该会议的会议详细信息,以便可以在查看每次代表大会登记的会议名称,日期和登记日期。

我有下面的代码但是没有用。你知道问题出在哪里吗?

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

表格结构:

#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <iterator>
#include <assert.h>

template<class InputIt1, class InputIt2,
         class OutputIt1, class OutputIt2, class OutputIt3,
         class Compare>
OutputIt3 decompose_connections(InputIt1 first1, InputIt1 last1,
                               InputIt2 first2, InputIt2 last2,
                               OutputIt1 result1, OutputIt2 result2,
                               OutputIt3 result3, Compare comp)
{
    while (first1 != last1 && first2 != last2) {
        if (comp(*first1, *first2)) {
            *result1++ = *first1++;
        } else if (comp(*first2, *first1)) {
            *result2++ = *first2++;
        } else {
            *result3++ = *first1++;
            ++first2;
        }
    }
     std::copy(first1, last1, result1);
     std::copy(first2, last2, result2);
     return result3;
}

int main() {
// Simply as an example
    std::vector<std::string> A1, A2, A3, B1, B2, B3;
    std::vector<std::vector<std::string>> vecA, vecB, diff, del, red;

    std::string strA1_1 = "1101";
    std::string strA1_2 = "11";
    std::string strA1_3 = "110111";
    std::string strA2_1 = "1111";
    std::string strA2_2 = "10";
    std::string strA2_3 = "111110";
    std::string strA3_1 = "1001";
    std::string strA3_2 = "01";
    std::string strA3_3 = "100101";

    std::string strB1_1 = "1001";
    std::string strB1_2 = "11";
    std::string strB1_3 = "100111";
    std::string strB2_1 = "0111";
    std::string strB2_2 = "10";
    std::string strB2_3 = "011110";
    std::string strB3_1 = "1001";
    std::string strB3_2 = "01";
    std::string strB3_3 = "100101";

    A1.push_back(strA1_1); A1.push_back(strA1_2); A1.push_back(strA1_3);
    A2.push_back(strA2_1); A2.push_back(strA2_2); A2.push_back(strA2_3);
    A3.push_back(strA3_1); A3.push_back(strA3_2); A3.push_back(strA3_3);

    B1.push_back(strB1_1); B1.push_back(strB1_2); B1.push_back(strB1_3);
    B2.push_back(strB2_1); B2.push_back(strB2_2); B2.push_back(strB2_3);
    B3.push_back(strB3_1); B3.push_back(strB3_2); B3.push_back(strB3_3);

    vecA.push_back(A1); vecA.push_back(A2); vecA.push_back(A3);
    vecB.push_back(B1); vecB.push_back(B2); vecB.push_back(B3);
    // end example

    auto compare = [](std::vector<std::vector<std::string>>::const_reference lhs,
                  std::vector<std::vector<std::string>>::const_reference rhs)
    {
        assert(lhs.size() && rhs.size());
        return lexicographical_compare(lhs.begin(), prev(lhs.end(), 2),
                                       rhs.begin(), prev(rhs.end(), 2)
                                       );
    };

    std::sort(vecA.begin(), vecA.end());
    std::sort(vecB.begin(), vecB.end());

    decompose_connections(vecA.begin(), vecA.end(),
                          vecB.begin(), vecB.end(),
                          std::back_inserter(diff),
                          std::back_inserter(del),
                          std::back_inserter(red),
                          compare);

    std::cout << "diff: " << endl;
    for (int i = 0; i < diff.size(); i++) {
        for (int j = 0; j < diff[i].size(); j++) {
            std::cout << diff[i][j] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;


    std::cout << "del: " << endl;
    for (int i = 0; i < del.size(); i++) {
        for (int j = 0; j < del[i].size(); j++) {
            std::cout << del[i][j] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;

    std::cout << "red: " << endl;
    for (int i = 0; i < red.size(); i++) {
        for (int j = 0; j < red[i].size(); j++) {
            std::cout << red[i][j] << " ";
        }
        std::cout << std::endl;
    }
    std::cout << std::endl;
}

2 个答案:

答案 0 :(得分:0)

尝试foreach $registrations代替$registration,因为您传递给View

答案 1 :(得分:0)

问题在于:

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

^这将返回一个查询构建器,而不是关系的结果。

你可能想要的是:

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

这假定User模型具有名为registrations的关系。

此外,您正在传递$registrations,但正在循环$registration,正如@Muabazalm注意到的那样。

下次,您还会收到错误信息,这将会很有帮助。