php – Laravel 5.4:执行原始查询并在视图中显示数据

我想运行一个我在控制器中编写的原始查询,并希望在我的视图中显示数据库中的数据.

这是我的控制器功能:

public function unverified_jobs_page()
{
  $query = "SELECT jd.*, cd.`company_name`, jc.`category_title`, jt.`job_type_title`, cc.`city_name`
            FROM `job_details` AS jd
            JOIN `company_details` AS cd ON cd.`company_id`=jd.`company_id`
            JOIN `job_category` AS jc ON jc.`category_id`=jd.`category_id`
            JOIN `job_type` AS jt ON jt.`job_type_id`=jd.`job_type_id`
            JOIN `city` AS cc ON cc.`city_id`=jd.`city_id`
            WHERE jd.`is_verified`='NO'
            ORDER BY jd.`date_posted` DESC";

    $sql = General_model::rawQuery($query);

    return view('adminpanel.jobs.unverfied_jobs',compact($sql));        

}

这是我的模型函数rawQuery()

 public static function rawQuery($query)
 {
 return DB::select( DB::raw($query) );  
 }

这是我的看法:

 <table class="table table-striped border-top" id="sample_1">
                    <thead>
                        <tr> 
                        <th>JOB ID </th>
                        <th>TITLE</th>
                        <th>COMPANY</th>
                        <th>CITY</th>
                        <th>DATE POSTED</th>
                        <th>CLOSING DATE</th>
                        <th>&nbsp;</th>
                        </tr>
                    </thead>
                    <tbody>

                       @foreach($sql as $d)

                       <tr>
                       <td>{{$d->job_id}}</td>
                       <td>{{$d->job_title}}</td>
                       <td>{{$d->company_name}}</td>
                       <td>{{$d->city_name}}</td>
                       <td>{{$d->date_posted}}</td>
                       <td>{{$d->expiry_date}}</td>
                        <td><a class='btn btn-info' href='#'>DETAILS</a></td>
                       </tr>

                       @endforeach
                    </tbody>
                </table>  

现在问题是,每次我尝试运行该页面时,它都会给我以下错误:

ErrorException in 10f46e45beef75fdd05b083aa636ae7821fe6936.php line 30:
Undefined variable: sql (View: C:\AppServ\www\jobs\jobs\unverfied_jobs.blade.php)

请帮忙.

最佳答案 您需要将变量$sql正确传递到视图中.

尝试

紧凑( ‘SQL’);

点赞