Skip to content

Commit f98aca9

Browse files
add dynamic where clause tip
1 parent ff0665f commit f98aca9

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

db-models-and-eloquent.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
- [Select specific columns](#select-specific-columns)
101101
- [Chain conditional clauses to the query without writing if-else statements](#chain-conditional-clauses-to-the-query-without-writing-if-else-statements)
102102
- [Override Connection Attribute in Models](#override-connection-attribute-in-models)
103+
- [Using Column Names in Where Clauses](#using-column-names-in-where-clauses)
103104

104105
### Reuse or clone query()
105106

@@ -2152,4 +2153,24 @@ class CustomModel extends Model
21522153
{
21532154
protected $connection = 'your_custom_connection';
21542155
}
2155-
```
2156+
```
2157+
### Using Column Names in Where Clauses (Dynamic Where Clauses)
2158+
2159+
You can use column names in where clause to make dynamic where clauses. In the following example, we use ```whereName('John')``` instead of ```where('name', 'John')```.
2160+
2161+
```php
2162+
<?php
2163+
2164+
namespace App\Http\Controllers;
2165+
2166+
use App\Models\User;
2167+
2168+
class UserController extends Controller
2169+
{
2170+
public function example()
2171+
{
2172+
return User::whereName('John')->get();
2173+
}
2174+
}
2175+
```
2176+
Tip given by [@MNurullahSaglam](https://twitter.com/MNurullahSaglam/status/1699763337586749585)

0 commit comments

Comments
 (0)