Search code examples
phpmysqllaravellaravel-5.3

Laravel - Getting all records not just the first and putting together?


I have tried asking this question to everyone but nobody can see why I am trying to do it or why its not working but I will try my best to explain it properly this time.

I have a simple government page on my website that shows 3 panels in bootstrap with Higher Government, Senior Ministers and Junior Ministers. Inside each of them panels it shows a list of accounts assigned to that government rank.

Here is my government.blade.php:

@include('frontend.header')
        <div class="col-md-12" style="padding-bottom:24px;">
            <ul class="nav navtab nav-tabs" id="myTab" style="border-bottom:none;border-radius:1px;">
                <li style="padding-right:13px;" class="active"><a style="" data-target="#higher_government_team" data-toggle="tab"><i class="fa fa-diamond" aria-hidden="true"></i> &nbsp;The Monarch/PM</a></li>
                <li style="padding-right:13px;"><a style="" data-target="#senior_government_team" data-toggle="tab"><i class="fa fa-university"></i> &nbsp;Seniors</a></li>
                <li style="padding-right:13px;"><a style="" data-target="#junior_government_team" data-toggle="tab"><i class="fa fa-university"></i> &nbsp;Juniors</a></li>
            </ul>
        </div>
        <div class="col-md-8">
            <div class="tab-content">
                <div class="tab-pane active" id="higher_government_team">
                    <div class="panel panel-info">
                        <div class="panel panel-body">
                            higher gov here
                        </div>
                    </div>
                </div>
                <div class="tab-pane" id="senior_government_team">
                    <div class="panel panel-info">
                        <div class="panel panel-body">
                            senior gov here
                        </div>
                    </div>
                </div>
                <div class="tab-pane" id="junior_government_team">
                    <div class="panel panel-info">
                        <div class="panel panel-body">
                            @foreach ($juniorGovernment as $governmentMember)
                                {{ $governmentMember->user_id }}<br>
                            @endforeach
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">

        </div>
    </div>
@include('frontend.footer')

Here is my controller for displaying the page:

<?php

namespace App\Http\Controllers\Frontend\User;

use Auth;
use Cache;
use Illuminate\Http\Request;
use App\Database\Website\User\Roleplay;
use App\Database\Website\Roleplay\GovernmentRole;
use App\Database\Website\Roleplay\Life\LifeEvents;

class GovernmentController
{
    public function getView()
    {
        $higherGovernment = Cache::remember('government.higher_government', 1, function() {
            return GovernmentRole::where('government_type', 'higher_government')->first()->stats;
        });

        $seniorGovernment = Cache::remember('government.senior_government', 1, function() {
            return GovernmentRole::where('government_type', 'senior_ministers')->first()->stats;
        });

        $juniorGovernment = Cache::remember('government.junior_government', 1, function() {
            return GovernmentRole::where('government_type', 'junior_ministers')->first()->stats;
        });


        return view('frontend.community.government', compact('juniorGovernment', 'seniorGovernment', 'higherGovernment', 'royalty'));
    }
}

Now as you can see, I only select the first record where government_type matches. But the issue is I have multiple government roles with that government type and I want to get all the account stats for all records with that government type. I have looked at others suggestions and none of them work, either throwing an error or don't do what I want to do.

Now, I have modals for each Roleplay and GovernmentRoles, I have posted them both below.

GovernmentRole:

namespace App\Database\Website\Roleplay;

use Eloquent;

class GovernmentRole extends Eloquent
{
    protected $primaryKey   = 'id';
    protected $table        = 'srp_government_roles';
    public $timestamps      = false;
    protected $fillable     = [];

    public function stats(){
       return $this->hasMany('App\Database\Website\User\Roleplay', 'government_id');
   }
}

Roleplay:

use Eloquent;

class Roleplay extends Eloquent
{
    protected $primaryKey   = 'id';
    protected $table        = 'srp_user_statistics';
    public $timestamps      = false;
    protected $fillable     = [];

    public function user()
    {
        return $this->belongsTo('App\Database\Website\User\Player', 'user_id', 'id');
    }

    public function government_role()
    {
        return $this->belongsTo('App\Database\Website\Roleplay\GovernmentRole', 'government_id');
    }
}

So what I am trying to achieve is not just grabbing all the Roleplay instances for the FIRST government role record in the database it finds but putting together ALL roleplay instances for ALL records that match government_type either higher_government, senior_ministers, or junior_ministers.


Solution

  • If I understand what you are asking, can you not do something like fetching the stats on your loop through in blade? For example (I removed the cache, its adding complexity to the question/answer without any benefit):

    // controller
    $higherGovernment = GovernmentRole::where('government_type', 'higher_government')->get();
    
    // blade
    @foreach($higherGovernment as $g)
        {{ $g->id }}
        {{ $g->name }}
        @foreach($g->stats as $stat)
          {{ $stat->id }}
          {{ $stat->something }}
        @endforeach
    @endforeach