简体   繁体   中英

How to retrieve view data in migration to controller?

I'm completely new to the views. I don't know how to get the view in controller. Below given is the view i have created using migration. I want to fetch the data in this view to my controller. How to do that? Any help is appreciated.

 class CourseMarketView extends Migration
 {
     public function up()
        {
            DB::statement("
          CREATE VIEW course_market_view AS
          (
            SELECT 
    users.id,
    users.name,
    course.courseId,
    course.courseUniqueName,
    skills.id,
    skills.skill,
    subjects.id,
    subjects.subject
     FROM users 
      LEFT JOIN course ON course.courseId=users.id
      LEFT JOIN skills ON skills.id=users.id
      LEFT JOIN subjects ON subjects.id = users.id

          )
        ");
        }
  }

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use App\Models\User;
use App\Models\Course;
use App\Models\Skill;
use App\Models\Subject;

class CourseMarketController extends Controller
{
    public function getcoursebysubjectID(){

        $users = User::select("*")
                        ->get()
                        ->toArray();             
        dd($users);    
    }
}

course table

Schema::create('course', function (Blueprint $table) {
            $table->integer('courseId')->autoIncrement();
            $table->string('courseDisplayName');
            $table->string('courseUniqueName')->unique();
            $table->string('courseAddedBy');
            $table->dateTime('createdOn');
            $table->dateTime('lastUpdated');
            $table->softDeletes();
        });

you can do that by creating new model like this :-

class CourseMarketView extends Eloquent {

    protected $table = 'course_market_view';
}

in controller you can do that :-

CourseMarketView::all();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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