Laravel Eloquent display query log

后端 未结 9 1762
萌比男神i
萌比男神i 2020-11-30 04:46
use App\\Order;

public function show(Order $order){

        $data = $order->all();
        return dd($order->getQueryLog());

Is there any w

相关标签:
9条回答
  • 2020-11-30 05:21

    To use getQueryLog() you need to enable it first:

    DB::enableQueryLog();
    DB::getQueryLog();
    

    If you want to see real queries, you can use Laravel Debugbar, it will show all real queries Laravel created during current request.

    Sometimes ->toSql() is also useful.

    0 讨论(0)
  • 2020-11-30 05:27

    //write this before the query DB::enableQueryLog();

    DB::enableQueryLog();

    $data = $order->all();

    $query = DB::getQueryLog();

    dd($query);

    0 讨论(0)
  • 2020-11-30 05:27

    You can use ::toSql() or ->toSql() as demonstrated below:

    use App\Order;
    
    public function show(Order $order){
    
        return $order::toSql();
    

    Or

    use App\Order;
    
    public function show(Order $order){
    
        return $order::where("id", "<>", 0)->toSql();
    

    You might have to enable query log:

    DB::enableQueryLog();
    
    0 讨论(0)
提交回复
热议问题