
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
- How to display pivot table column value in laravel
- Insert Comma Separated Values in laravel
- Ignore Records where a field has NULL value in Laravel
- Display option of select as selected with blade directive Laravel
- How to get database name in Laravel 9 ?
- Remove several global scope from query
- Method Illuminate\Http\Request::validated does not exist
- Always load the relationship data with eager loading in Laravel
- How to Access Array in blade laravel
- How to remove P tag from CkEditor in Laravel?
- Get current month records in laravel 7/8
- How to get route method name in Laravel
- Validation for multiple forms on same page in laravel
- Extract only time from datetime in laravel
- How to add active class to menu item in laravel
- There are no commands defined in the "route:" namespace
- Show old value while editing the form in Laravel
- Convert input array to comma-separated string in laravel controller
- How to call model in blade laravel
- Print query in laravel
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Generate unique username in Laravel
- How to get list of all views file in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Trying to get property 'title' of non-object