Debug School

rakesh kumar
rakesh kumar

Posted on

How to use query() function in laravel with example

In Laravel, the query() function is used to start building a new query using Laravel's query builder. It is typically used when you want to dynamically construct a query based on certain conditions or criteria. Here's an example of how you can use the query() function in Laravel:

use App\Models\Product;

public function index(Request $request)
{
    $query = Product::query();

    if ($request->has('category')) {
        $category = $request->input('category');
        $query->where('category', $category);
    }

    if ($request->has('price')) {
        $price = $request->input('price');
        $query->where('price', '>=', $price);
    }

    $products = $query->get();

    return view('products.index', compact('products'));
}
Enter fullscreen mode Exit fullscreen mode

In this example, we have an index method in a controller that retrieves products from the Product model. The $query variable is assigned the result of the query() function called on the Product model. This creates a new query builder instance.

Based on the conditions provided in the request, we can dynamically add additional constraints to the query. If the request contains a category parameter, we add a where clause to filter the products by category. Similarly, if the request contains a price parameter, we add a where clause to filter the products by price.

Finally, we retrieve the products by calling get() on the $query object. This executes the query and retrieves the matching products.

The resulting collection of products is then passed to the products.index view using the compact function.

By using the query() function and dynamically adding constraints based on request parameters, you can build flexible and customizable queries in your Laravel application. This allows you to filter and retrieve data based on various conditions or criteria specified by the user.

Another Example

 $categories_query =Franchise_category::query();
  $categories_query->select('franchise_category.*');
$categories_query->distinct('franchise_category.id');        
   $categories_count = $categories_query->count();
Enter fullscreen mode Exit fullscreen mode

Another Example

 $categories_query =Franchise_category::query();

        $categories_query->select('franchise_category.*');

        // search query
        if(is_array($search_values) && count($search_values) > 0)
        {
            $categories_query->where(function ($query) use ($search_values) {
                foreach($search_values as $search_values_key => $search_value)
                {
                    $query->orWhere('franchise_category.category_name', 'LIKE', "%".$search_value."%");
                }
            });
        }

        // sort by
        if($filter_sort_by == Category::CATEGORIES_SORT_BY_NEWEST_CREATED)
        {
            $categories_query->orderBy('franchise_category.created_at', 'DESC');
        }
        elseif($filter_sort_by == Category::CATEGORIES_SORT_BY_OLDEST_CREATED)
        {
            $categories_query->orderBy('franchise_category.created_at', 'ASC');
        }
        elseif($filter_sort_by == Category::CATEGORIES_SORT_BY_NEWEST_UPDATED)
        {
            $categories_query->orderBy('franchise_category.updated_at', 'DESC');
        }
        elseif($filter_sort_by == Category::CATEGORIES_SORT_BY_OLDEST_UPDATED)
        {
            $categories_query->orderBy('franchise_category.updated_at', 'ASC');
        }
        elseif($filter_sort_by == Category::CATEGORIES_SORT_BY_CATEGORY_NAME_A_Z)
        {
            $categories_query->orderBy('franchise_category.category_name', 'ASC');
        }
        elseif($filter_sort_by == Category::CATEGORIES_SORT_BY_CATEGORY_NAME_Z_A)
        {
            $categories_query->orderBy('franchise_category.category_name', 'DESC');
        }

 $categories_query->distinct('franchise_category.id');        
 $categories_count = $categories_query->count();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)