Home > Enterprise >  How To get data from database but using conditions in Laravel
How To get data from database but using conditions in Laravel

Time:08-03

Hello guys i have database like this

Language    category
en          clothes
ar          ملابس

I want to show all categories Has en Language and make foreach to categories i tryied

@if($categories && $categories -> count() > 0)

@foreach($categories as $category)

<option

 value="{{$category -> id }}">{{$category->name}}</option>

 @endforeach

 @endif
but it returns all categories with en and ar Languages I Want only to show categories with en Language i think i'm forget some basics but i'm really want your help please

Controller:

    public function index() {

        $dufaultLang = get_dufault_lang(); // en
$categories =
mainCategory::where(
    'translation_lang',

    $dufaultLang
)
    -> Selection()  // Select From Selection Scope
    -> get();       // Get Selection Data

return view(
    'admin.maincategories.index',
     compact('categories')
);
    }
    // End Index Function

SelctionScope:

public function scopeSelection($query) {
    return $query -> select('id' , 'translation_lang' , 'translation_of' , 'name' , 'slug'  , 'photo' , 'active');
}

CodePudding user response:

Why You're Get Selectionscope it's wrong you can change $categories variable to

$categories = mainCategory::where('translation_lang' , 'en')->get();
  • Related