I have a foreach loop and I want to sort the list by showing records where is_active is null first, then anything else after.
@foreach ($registrations->sortBy('is_active') as $registration)
Any ideas?
I have a foreach loop and I want to sort the list by showing records where is_active is null first, then anything else after.
@foreach ($registrations->sortBy('is_active') as $registration)
Any ideas?
first of all make a query that returns records in ascending order according to is_active
value( I'm assuming that 0 is for inactive and 1 is for active)
Query should be:
$records=DB::table('table_name')->orderBy('is_active','asc')->all();
return view('viewblade')->with('records',$records);
now you can display data in viewblade like:
@foreach($records as $record){
{{$record->fieldName}}
}
@endforeach
You can try this way,
// specify second_column that will be used if multiple null values found
$registrations = $registrations->sortByDesc('second_column')->sortBy('is_active');
@foreach ($registrations->all() as $registration)