laravel-repositories icon indicating copy to clipboard operation
laravel-repositories copied to clipboard

How to get the query executed in Laravel 5.3 ?

Open moschel26 opened this issue 7 years ago • 0 comments

My service to delete product id like this :

<?php
...
use App\Repositories\ProductRepository;
class ProductService
{
    private $product_repository;
    public function __construct(ProductRepository $product_repository)
    {
        $this->product_repository = $product_repository;
    }

    public function destroy($id)
    {
        DB::enableQueryLog();
        # your laravel query builder goes here
        $result = $this->product_repository->delete($id);
        $laQuery = DB::getQueryLog();
        $lcWhatYouWant = $laQuery[0]['query']; # <-------
        dd($lcWhatYouWant);
        # optionally disable the query log:
        DB::disableQueryLog();
    }
}

My repository like this :

<?php
...
use App\Models\Product;
use Illuminate\Contracts\Container\Container;
use Rinvex\Repository\Repositories\EloquentRepository;
class ProductRepository extends EloquentRepository
{
    public function __construct(Container $container)
    {
        $this->setContainer($container)
             ->setModel(Product::class)
             ->setRepositoryId('rinvex.repository.uniqueid');
    }
}

On the service, I try like that. I want to debug the query to get the error. But there is no error

The result of dd($lcWhatYouWant); not display

How to get the query executed?

I get reference to debug the query from here : https://stackoverflow.com/questions/27753868/how-to-get-the-query-executed-in-laravel-5-dbgetquerylog-returning-empty-arr

I try like that, but it does not work

Seems the debug query on this repository is different

@Omranic, Seems you can help me

moschel26 avatar Oct 29 '17 18:10 moschel26