Answers for "laravel search orwhere"

PHP
0

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();
Posted by: Guest on November-08-2020
0

search laravel

relationship
  $user = User::with('Profile')->where('status', 1)->whereHas('Profile', function($q){
    $q->where('gender', 'Male');
})->get();


 $searchText = 'test text';
        Product::with('owner')->where(function($query) use ($searchText)
        {
            $query->where('product_name', 'LIKE', '%' . $searchText . '%');

            $columns = ['product_code', 'place_location', 'remark'];

            foreach ($columns as $column ) {
                $query->orWhere($column, 'LIKE', '%' . $searchText . '%');
            }

            $query->orWhereHas('owner', function($q) use ($searchText) {
                $q->where(function($q) use ($searchText) {
                    $q->where('name', 'LIKE', '%' . $searchText . '%');
                    $q->orWhere('company_name', 'LIKE', '%' . $searchText . '%');
                });
            });

        });


$comments = Comment::query()->orderby('created_at', 'DESC');

if (!empty($id)){
    $comments = $comments->where('id', $request->input('id') );
}

if (!empty($name)){
   $comments = $comments->whereHas('author', function ($query) use ($name) { $query-
     >where('username', 'like', $name.'%'); });
}

  $comment = $comments->paginate(10);


->whereHas('Category', function($query) use($q){

                       $query->where('name', 'LIKE', '%'. $q .'%')

         })->paginate(1)->setPath('');
Posted by: Guest on October-18-2021

Browse Popular Code Answers by Language