Conditional where clause in Laravel
Conditional where clause in Laravel
In this tutorial, we will try to find the solution to "Conditional where clause in Laravel" through programming.
Sometimes we need to filter records of tables based on input parameters. In that case, we can use conditional where clause in Laravel to filter the records with that particular input text.-
Filter rows of table with where clause in Laravel 8
--PATH app\Http\Controllers\<PostController>.php<?php namespace App\Http\Controllers; use App\\Models\Post; use Illuminate\Http\Request; class PostController extends Controller{ public function index(Request $request){ $posts = Post::where('is_active', true); //Filter post by category_id if ($request->has('category_id') ) { $posts->where('category_id', $request->category_id); } //Filter post by created_at column if ($request->has('created_at')) { $posts->where('created_at','>=', $request->created_at); } return $posts->get(); } } }
You can filter rows of tables using the where clause in Laravel. You have to check the request parameters if the request parameter exists then you can run the query with the where clause and after that you can get data using the get() method.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Laravel insert query not working
- Call to undefined relationship [user] on model [App\Models\Post]
- Get duplicate records in laravel
- Where to use whereNotNull eloquent in laravel
- Ajax GET request in laravel
- How to get single column value in laravel
- How to check data inserted or deleted in pivot after toggle method
- How to start websocket server in laravel
- Wheredate in laravel not working
- Laravel 10 Breeze Authentication Example
- Laravel get all records with pagination
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- How to create laravel project using jetstream
- The use statement with non-compound name 'Auth' has no effect
- Add class to body in laravel view
- Calculate age from date of birth in Laravel
- How to create static page in Laravel
- How to pass query string with pagination in laravel
- Laravel delete all rows older than 30 days
- Laravel get count with where condition
- How to pass link from controller to view in laravel on ajax call
- How to include header file in laravel
- How to print form data in laravel
- How to return a column with different name in Laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"