在laravel检查这个?

我正在开发角色扮演游戏。 下面我有一个政府的控制器。 我想做的是获得一份高级政府成员,高级政府成员和初级政府成员(这些是不同类型的政府成员)的名单,例如...... </ p>

< p>这是我的数据库结构</ p>


  • 初级政府将成为部长</ li>
  • 高级政府将成为国务卿</ li>

  • 高级政府将成为首相和王室</ li>
    </ ul>

    我有一个名为 srp_rp_stats </ code>的角色扮演统计表 我有一个名为 government_id </ code>的列,用于匹配 government </ code>表中的id。 </ p>

    在我的 government </ code>表中,我有一个 id </ code>字段作为主键,一个 title </ code> for 政府的立场,以及 type </ code>。 type </ code>字段是枚举,下面是示例... </ p>

      government_type enum 
    'higher_government','senior_government','junior_government'
    </ code> </ pre>

    我下面要做的是获取每组政府的成员,但我需要使用 government_id </ code>。 rp </ code>表来检查 government </ code>表中的类型实际上是我要找的,我该怎么做?</ p>

    代码:</ p>

     命名空间App \ Http \ Controllers \ Frontend \ User; 
    
    

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

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

    $ seniorGovernment = Cache :: Remeber('government.senior_government',function(){
    返回Roleplay :: get();
    }); \ ñ
    $ juniorGovernment = Cache :: remember('government.junior_government',function(){
    返回Roleplay :: get();
    });

    返回视图('frontend.community.government',compact( 'HigherGovernment','seniorGovernment','juniorGovernment'));
    }
    }
    </ code> </ pre>
    </ div>

展开原文

原文

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

douchen1988
douchen1988 我认为您需要将您的角色扮演模型的关系添加到政府表格中。然后,您可以使用查询构建器查找具有政府类型的rp。那个听起来是对的吗?
接近 4 年之前 回复
duanhu7615
duanhu7615 是的,这个问题很容易解释我想要的东西。
接近 4 年之前 回复
douji8017
douji8017 角色扮演我认为这是一个雄辩的模型?不确定问题是否足够明确。你是否只需要做一些像返回Roleplay::where('government_type','higher_government')->get();例如?
接近 4 年之前 回复
doujiao1814
doujiao1814 不,它不是@CUGreen你想看到什么型号?
接近 4 年之前 回复
dongtu7567
dongtu7567 你能告诉我们模特吗?
接近 4 年之前 回复
dongluan0020
dongluan0020 角色扮演模型是否与政府表格有关系?
接近 4 年之前 回复

2个回答

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']);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问