雄辩的查询不使用where / first条件。为什么?

时间:2019-07-19 13:03:25

标签: laravel eloquent

简单易懂的口才查询不使用where条件。

  

Players :: where('id_player',“ =”,3113)-> first()-> toSql();

返回:

  

“从players中选择*”

我的控制器

namespace App\Http\Controllers\Office;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Players;

class OfficeController extends Controller{
   public function playerLineup(Request $request){
      $e = Players::where('id_player',"=" ,3113)->first()->toSql(); 
      dd($e);
   }
}

我的模态

namespace App;

use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;

class Players extends Model{
    protected $primaryKey = 'idp';
}

2 个答案:

答案 0 :(得分:2)

运行->first()后,结果将是Player模型的新实例。那么在新模型上的->toSql()就像Player::query()->toSql()

在此之前,您需要致电->toSql()

Players::where('id_player',"=" ,3113)->toSql();

Players::where('id_player',"=" ,3113)->limit(1)->toSql();

答案 1 :(得分:0)

/ 尝试执行此操作 /

namespace App\Http\Controllers\Office;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Players;

use DB;

class OfficeController extends Controller{
   public function playerLineup(Request $request){
      $e = DB::table('Players')->where('id_player',"=" ,3113)->get()->first(); 
      dd($e);
   }
}