2016-03-29 2 views
6

実際、私は下の手順を使用してPHP Artisanを使用してLaravelのSQLビューを作成することができました。コマンド以下Laravelでphp artisanを使用してデータベースビューの移行を作成する方法は?

ステップ1を実行します:

php artisan make:migration create_overall_report_views 

ステップ2.

オープン移行ファイルと以下のコードを追加:呼び出して実行するには

class CreateOverallReportView extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
    // 
    DB::statement(" 
     CREATE VIEW views_overall_report AS 
     (
     SELECT er.user_id as user_id, e.id AS entities_id, 
      c.status_id AS status_id, s.name AS status_name 

     FROM `user_roles` er 
      LEFT JOIN elists e ON e.id=er.entities_id 
      LEFT JOIN `clists` c ON c.id=e.checklists_id 
      LEFT JOIN `status` s ON s.id = c.overall_status_id 

     WHERE s.slug = 'completed' 
      AND c.deleted_at IS NULL 
    ) 
    "); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
    DB::statement('DROP VIEW IF EXISTS views_overall_report'); 
    } 

} 

ステップ3をLaravelクエリによるSQLビュー

$items = $DB::table('views_overall_report') 
      ->select('status_id', 'status_name', 
       $DB::raw('count(entities_id) as counts') 
      ) 
      ->groupBy('status_id') 
      ->orderBy('counts' , 'desc') 
      ->whereIn('user_id', Auth::user()->id()) 
      ->get(); 
print_r($items); 

希望に役立ちます。誰かがより良い解決策を持っているかどうか私に教えてください!

+0

https://laravel.com/docs/master/migrations –

+0

@FrancescodeGuytenaere @解決策を見つけました。 – nullwriter

+0

@nullwriterこの記事の歴史を見て、最初は彼がドキュメンテーションを見つけることができなかったので、私はコメントに投稿しました(実際の回答として掲示するのではなく)。あなたは私のコメントに時代遅れとフラグを立てることができます。 –

答えて

-6

あなたのモデルでは、laravel eloquentと関係を作成する必要があります。 例: 2つのテーブル(ロール、ユーザー)があり、それらの間の関係がロールモデルで作成されたロールモデルとロールモデルの間に多数ある場合、以下のコードを記述します。 public function users() { return $ this-> belongsaToMany( 'App/User'); }

とUserモデルに:その後

public function roles() 
{ 
return $this->belongsToMany('App/Role') 
} 

新しい移行を作成して、テーブル名ROLE_USERを入れて、このに以下のコードを置く:

public function up() 
{ 
Schema::create('category_post', function (Blueprint  $table){ 
$table->increments('id')->unsigned(); 
$table->integer('post_id')->unsigned()->index(); 
$table->integer('category_id')->unsigned()->index(); 
$table->timestamps(); 
$table->foreign('category_id') 
->references('id')->on('categories') 
->onUpdate('cascade') 
->onDelete('cascade');$table->foreign('post_id') 
->references('id')->on('posts') 
->onUpdate('cascade') 
->onDelete('cascade'); 
}); 
} 

は、それを保存して、PHPを実行します職人が移住する。 これで完成しました。あなたのcontrolerにあなたのユーザーおよびロールモデルを追加し、ユーザーに関連する役割のためにforexampleクエリをgetingのために、このすべての後 AN、このように実行します。

public function edit($id){ 
    $user=User::whereId($id)->firstOrFail(); 
    $roles=Role::all() 
    $selectedRole=$user->roles()->lists('id')->toArray(); 
    return view('your page   view',compact($user,$roles,$selectedRole)); 
    } 

とビューの書き込みに選択されたロールを示すために:

<select id="role" name="role[]" multiple> 
@foreach($roles as $role) 
<option value="{!! $role->id !!}" @if(in_array($role->id, $selectedRoles)) selected="selected" @endif > 
{!! $role->display_name !!} 
</option> 
@endforeach 
</select> 

終了しました。

+1

私はEloquentを介してリレーションシップを管理することに慣れていますが、ここでSQLビューを作成しています... – rcadhikari

6

は同じ問題につまずいたとドキュメントに任意のヘルプをリンクする方法http://programmingarehard.com/2013/11/10/eloquent_and_views.html/

class CreateCompaniesView extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     DB::statement("CREATE VIEW companiesView AS 
         SELECT *, 
         (
          SELECT GROUP_CONCAT(DISTINCT id SEPARATOR ',') 
          FROM people AS p 
          WHERE p.company_id = c.id 
         ) AS person_ids 
         FROM companies AS c"); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     DB::statement("DROP VIEW companiesView"); 
    } 
}
関連する問題