简体   繁体   English

PHP-Laravel 5从3个SQL数据库表以DATA列作为标题将数据获取到html表

[英]PHP - Laravel 5 get data to html table from 3 SQL database tables with column DATA as headers

I'm very new to PHP so please bear with me - this may be completely obvious to you but unfortunately I am pulling my hair out!... For info I am using Laravel 5. 我对PHP还是很陌生,所以请多多包涵-这对您来说可能是完全显而易见的,但不幸的是,我正在拔头发!...有关信息,我正在使用Laravel 5。

I want to use data from SQL database tables (teams and user_profiles) to list profile images followed by users names (both stored in the user_profiles table) in columns, below headers that are titled with the relevant team name (stored in the teams table). 我想使用SQL数据库表(teams和user_profiles)中的数据在列中标有相关团队名称(存储在teams表中)的标题下方列出配置文件图像,然后列出用户名(均存储在user_profiles表中)。 。

I've managed to do this by manually creating a variable for each team in my 'admin' controller, each contains a query to get the data based on the id number in the teams table 我设法通过在“管理员”控制器中为每个团队手动创建一个变量来做到这一点,每个变量都包含一个查询,以根据teams表中的ID号获取数据

Example of a variable in my admincontroller.php 我的admincontroller.php中的变量示例

$team1 = DB::table('user_profiles')
->join('users', 'users.id', '=', 'user_profiles.user_id')
->join('teams', 'teams.id', '=', 'user_profiles.teams_id')
->select('users.*', 'user_profiles.*', 'teams.*')
->where(['teams.id' => 1])
->get();

Calling this in the admin.blade.php (trimmed to show 'team 1' only) 在admin.blade.php中调用它(修剪为仅显示“团队1”)

<div class="col-md-4 text-center">
    <h3>Team 1</h3>
</div>

<div class="panel-inner-container">
    <div class="col-md-4 panel-inner-left">

    @foreach ($team1 as $profile)
        <img src="{{ $profile->profile_pic }}" alt="Avatar" class="avatar center-block" />
        <div class="lead text-center">
            <p>{{ $profile->name }}</p>
        </div>
    @endforeach
</div>        

I have considered using a html table, where I have used a foreach to create as many columns as there are rows on the teams table, and populate the table head for each column with the team name. 我考虑过使用html表,在其中使用了foreach来创建与teams表上的行一样多的列,并使用team name填充每一列的表头。 However, what I cannot figure out is how to populate each column with the users_profiles data based on each team - what I have at the moment - which I knew wouldn't work - is the duplicated profile pic and name of user 1 in every column. 但是,我无法确定的是如何根据每个团队的users_profiles数据填充每一列-目前,我知道这是行不通的-是每列中重复的个人资料图片和用户1的名称。

Assume user_profiles table has 9 rows, and each user profile row has id, name, and teams_id AND teams table has 4 rows, each with teams_id and team_name 假设user_profiles表有9行,并且每个用户配置文件行都有id,name和team_id,而teams表有4行,每行分别包含team_id和team_name

What I have 我有的

|------------|---------------|------------|-------------|
|team 1      |   team 2      |   team 3   |   team 4    |
|------------|---------------|------------|-------------|
|user 1 img  |  user 1 img   | user 1 img | user 1 img  |
|User 1 name | user 1 name   | user 1 name| user 1 name |
|------------|---------------|------------|-------------|
|user 1 img  |  user 1 img   | user 1 img | user 1 img  |
|User 1 name | user 1 name   | user 1 name| user 1 name |
|------------|---------------|------------|-------------|

What I want 我想要的是

|------------|---------------|------------|-------------|
|team 1      |   team 2      |   team 3   |   team 4    |
|------------|---------------|------------|-------------|
|user 1 img  |  user 2 img   | user 3 img | user 4 img  |
|User 1 name | user 2 name   | user 3 name| user 4 name |
|------------|---------------|------------|-------------|
|user 5 img  |  user 6 img   | user 7 img | user 8 img  |
|User 5 name | user 6 name   | user 7 name| user 8 name |
|------------|---------------|------------|-------------|

Please tell me if I need to give any extra information, any ideas hugely appreciated. 请告诉我是否需要提供任何其他信息,任何想法,我们将不胜感激。

Here is an example dump of $team1 : 这是$team1转储$team1

Collection {#213 ▼
  #items: array:2 [▼
    0 => {#219 ▼
      +"id": 1
      +"name": "Tom"
      +"email": REMOVED
      +"password": REMOVED
      +"admin": 1
      +"remember_token": REMOVED
      +"created_at": "2017-10-19 18:07:00"
      +"updated_at": "2017-10-19 18:08:38"
      +"user_id": 1
      +"teams_id": 1
      +"profile_pic": "http://laravel.dev/uploads/dartboard.png"
      +"team_name": "Barkers"
      +"venue": "Barker Butts"
      +"venue_post_code": "CV5 9AR"
    }
    1 => {#216 ▼
      +"id": 1
      +"name": "John Smith"
      +"email": REMOVED
      +"password": REMOVED
      +"admin": 0
      +"remember_token": REMOVED
      +"created_at": "2017-10-19 18:07:00"
      +"updated_at": "2017-10-19 18:08:38"
      +"user_id": 3
      +"teams_id": 1
      +"profile_pic": "http://laravel.dev/uploads/dartboard.png"
      +"team_name": "Barkers"
      +"venue": "Barker Butts"
      +"venue_post_code": "CV5 9AR"
    }
  ]
}

If all of your teams are going to have the same amout of users then you can add this variables to your controller: 如果您所有团队的用户人数都相同,则可以将此变量添加到控制器中:

// Group all of your teams in a single array
$teams = [
    $team1,
    $team2,
    $team3,
    $team4
];

// Create an array with a length equal
// to the number of necessary rows
$rows = range(0, $team1->count() - 1);

Then pass them to your view: 然后将它们传递给您的视图:

return view('someview')->with([
    'teams' => $teams,
    'rows' => $rows
]);

And finally use them like: 最后像这样使用它们:

@foreach ($rows as $row)
    <tr>
        @foreach ($teams as $team)
            <td>
                {{ $team[$row]->profile_pic }} <br>
                {{ $team[$row]->name }}
            </td>
        @endforeach
    </tr>
@endforeach

Hopefully this should give you an idea how to achieve what you are trying to build. 希望这应该给您一个想法,如何实现您要构建的东西。

I suggest giving the Laravel's collections documentation a read. 我建议阅读Laravel的藏品文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM