search query in laravel 8 code example
Example 1: laravel eloquent search query 2020
$result = Marriage::where('name','LIKE','%'.$email_or_name.'%')
->orWhere('email','LIKE','%'.$email_or_name.'%')
->get();
Example 2: search query in laravel
$searchTerm ='milad zamir Abc';
$reservedSymbols = ['-', '+', '<', '>', '@', '(', ')', '~'];
$searchTerm = str_replace($reservedSymbols, ' ', $searchTerm);
$searchValues = preg_split('/\s+/', $searchTerm, -1, PREG_SPLIT_NO_EMPTY);
$res = User::where(function ($q) use ($searchValues) {
foreach ($searchValues as $value) {
$q->orWhere('name', 'like', "%{$value}%");
$q->orWhere('family_name', 'like', "%{$value}%");
}
})->get();
Example 3: laravel 8 orderby
$flights = App\Models\Flight::where('active', 1)
->orderBy('name', 'desc')
->take(10)
->get();
Example 4: laravel find query
return Destination::orderByDesc(
Flight::select('arrived_at')
->whereColumn('destination_id', 'destinations.id')
->orderBy('arrived_at', 'desc')
->limit(1)
)->get();
Example 5: laravel find query
<?php
$flights = App\Models\Flight::all();
foreach ($flights as $flight) {
echo $flight->name;
}
Example 6: laravel find query
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Flight extends Model
{
}