Laravel Query构建器相当于SQL查询吗?

时间:2018-05-01 17:03:28

标签: laravel laravel-5.5 laravel-query-builder

我有两个表Country和State查询State是

select state.id,state.state_name,Country.Country_name from country,state where state.country=country.id

此查询工作正常,我想将其转换为laravel查询构建器

2 个答案:

答案 0 :(得分:0)

使用此:

DB::table('country')
    ->select('state.id', 'state.state_name', 'country.country_name')
    ->join('state', 'state.country', '=', 'country.id')
    ->get();

答案 1 :(得分:0)

在控制器功能中:

DB::table('country')->leftjoin('state', function ($join) {
            $join('state', 'state.country', '=', 'country.id');})->get();

在控制器导入DB外观中:

use Illuminate\Support\Facades\DB;