在laravel检查这个?

I am developing a roleplay game. Below I have a controller for the government. What I am trying to do is to get a list of the higher government members, senior government members, and the junior government members (these are a different type of government members) for example...

Here is my database structure

  • Junior Government would be the Ministers
  • Senior Government would be the Secretary of States
  • Higher Government would be the Prime Minister and The Crown

I have a table for roleplay statistics called srp_rp_stats and in there I have a column called government_id which is meant to match the id in the government table.

In my government table, I have an id field as the primary key, a title for the government position, and the type it is. The type field is an enum, example below...

government_type enum
'higher_government','senior_government','junior_government'

What I want to do below is to get the members of each of the set of governments, but I need to use the government_id in the rp table to check that in the government table the type is actually what I am looking for, how can I do this?

Code:

namespace App\Http\Controllers\Frontend\User;

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

class GovernmentController
{
    public function getView()
    {
        $higherGovernment = Cache::remember('government.higher_government', function() {
            return Roleplay::get();
        });

        $seniorGovernment = Cache::Remeber('government.senior_government', function() {
            return Roleplay::get();
        });

        $juniorGovernment = Cache::remember('government.junior_government', function () {
            return Roleplay::get();
        });

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

I think your better off using Eloquent for this, not sure what version or Laravel your doing this on but here is the guide for 5.3 Eloquent: https://laravel.com/docs/5.3/eloquent

$higherGovernment = Goverment::where('government_type', 'higher_government')->get();

This will give you all of the entries that have 'Higher Government' in the type field.

In Laravel, you are describing a OneToOne and OneToMany relationship. A government type will have many srp_rp_stats. Where as srp_rp_stats will have only one government type. If this sounds correct, this is how you can do it (reference https://laravel.com/docs/5.3/eloquent-relationships).

First, you need to make sure you have a Model for both databases. Laravel has a default table naming convention that we will need to override for these most likely (based on your table names).

php artisan make:model Stat

then inside Stat.php

    <?php namespace App;

    use Illuminate\Database\Eloquent\Model;

    class Stat extends Model
    {
       public $table = "srp_rp_stats";
       public $fillable = ['government_id']; // Add any other fields in your database excluding timestamps and primary id

       public function government_type(){
           return $this->belongsTo(GovernmentType::class, 'government_id');
       }
    }

next

php artisan make:model GovernmentType

again, inside GovernmentType.php

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class GovernmentType extends Model
{
   public $table = "government";
   public $fillable = ['title', 'type'];

   public function stats(){
       return $this->hasMany(Stat::class, 'government_id');
   }
}

Once this is all setup, you should be able to do things such as

$stats = Stat::all();
foreach($stats as $stat){
  $government = $stat->government_type; // This will tell you the government type for this stat
}

or

    $government_type = GovernmentType::where('name', 'higher_government')->first();
    // This will return all stats for higher government
    $stats = $government_type->stats; 

or

    $government_type = GovernmentType::where('name', 'higher_government')->first();
    // This will automatically add the correct government_type_id to the stat that is being added to the database.
    $government_type->stats()->create(['some_field' => 'some fields value']);