Search
 
SCRIPT & CODE EXAMPLE
 

PHP

eloquent search from child table column

$fields = array('membership' => ['expiration'], 'firstname', 'middlename', 'lastname', 'email', 'dlnumber', 'membership_id');

// orWhereHas will use joins, so we'll start with fields foreach
foreach ($fields as $relation => $field)
{
  if (is_array($field))
  {
    // here we join table for each relation
    $query->orWhereHas($relation, function ($q) use ($field, $search) {

      // here we need to use nested where like: ... WHERE key = fk AND (x LIKE y OR z LIKE y)
      $q->where(function ($q) use ($field, $search) {
        foreach ($field as $relatedField)
        {
          foreach ($search as $term)
          {
            $q->orWhere($relatedField, 'like', "%{$term}%");
          } 
        } 
      });
    });
  } 
  else
  {
    foreach ($search as $term)
    {
      $query->orWhere($field, 'like', "%{$term}%"); 
    } 
  } 
}
Comment

eloquent search from child table column

Member::whereHas('membership', function ($q) {
   $q->where('expiration', 'like', 'somethingToSearchFor');
})->get();
Comment

PREVIOUS NEXT
Code Example
Php :: onde fica o php ini ubuntu 
Php :: Drupal sync directory in settings.php 
Php :: how use same method in another class in laravel 
Php :: laravel livewire refresh computed property 
Php :: Display a variable containing html in laravel 
Php :: redirect from controller to named route with prams in URL 
Php :: Relationship 1-n multiple BACKPACK Laravel 
Php :: wordpress get_permalink not working 
Php :: how to set db table type in laravel 
Php :: php script auf serve alle 5 minuten ausführen 
Php :: array_map with user functions php and parameter php 
Php :: Add custom column at custom posts list 
Php :: Laravel docker-compose 404 not found Nginx 
Php :: laravel easy form 
Php :: Rewrite .php file without .php extension with .htaccess ULTIMATE SOLUTION 
Php :: seeder name singular or plural laravel 
Php :: laravel collection modelKeys 
Php :: not have permision elgg settings.php 
Php :: provenienza geografica di un utente php webmaster 
Php :: create a product table 
Php :: php variable array for json encode data 
Php :: how to write a php program for an electricity bill using if-else conditions 
Php :: Laravel efficient way to remove X records if there are duplicates 
Php :: laravel model relationships with two columns match 
Php :: route laravel 
Php :: php trait 
Php :: simple php round example 
Php :: Turn error log WP 
Php :: wpdb count 
Php :: post request axios php 
ADD CONTENT
Topic
Content
Source link
Name
8+1 =