
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
- Submit form without CSRF token in Laravel
- How to start websocket server in laravel
- Laravel create multiple records in Pivot table
- How to set column as primary key in Laravel model
- Route [password.request] not defined
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Conditional validation in laravel
- Define variable and use in Laravel controller method
- Laravel onclick function not working
- Send id with route Laravel
- Laravel specific table Migration
- Composer create project laravel/laravel example app
- Datetime field in Laravel migration
- PhpMyAdmin - Error The mysqli extension is missing
- How to get database name in Laravel 9 ?
- How to get data from two tables in laravel
- Route group with URI prefix using middleware and route name prefixes
- Remove array keys and values if it does not exist in other array in Laravel
- How to show data by ID in laravel?
- Class 'App\Providers\Auth' not found
- Include External CSS and JS file in Laravel
- Non-static method App\Http\Helper::myFunction() should not be called statically
- Laravel file size validation not working
- How to change default timestamp fields name in Laravel
- How to get random string in Laravel