Request Elequent with a default value. How to do?

Is there a website in several languages. You need to make, when you specify language ($languageId) derived categories of that language. If not given, all categories.
Osprey wrote this, but if you do not specify a language, not what displays

public function scopeGetCatLeng ($query, $languageId = '*')
{
 return $query->where('parent_id', null)->where('language_id', $languageId);
 }


$categories = Category::getCatLeng(1)->get(); // prints the category language 1;
$categories = Category::getCatLeng()->get(); // does not display categories at all;


I can not properly use the * character ?
July 2nd 19 at 17:49
1 answer
July 2nd 19 at 17:51
Solution
Um, it's obvious.

1) make a NULL value by default
2) add where('language_id') only if this parameter is not NULL

// EDIT

public function scopeGetCatLeng ($query, $languageId = null)
{
 $query->where('parent_id', null);

 if ($languageId !== null) {
 $query->where('language_id', $languageId);
}

 return $query;
}
This is just a check if($languageId) { //query with where('language_id') } else { //the query without where('language_id') } ? But it turns out more code... but fewer audits in SQL - Vince89 commented on July 2nd 19 at 17:54
: more code work? I beg you, this is absolutely normal. - vito.Hoeg commented on July 2nd 19 at 17:57
: write in one line - Lincoln.Will commented on July 2nd 19 at 18:00

Find more questions by tags Laravel