public function scopeOfLocale(Builder $builder, $locale = 'en'){
return $builder->where('code', '=', $locale);
}
public function handle($request, Closure $next)
{
Locale::addGlobalScope('locale', function (Builder $builder){
$builder->ofLocale(app()->getLocale());
});
}
public function locales(){
return $this->hasMany(Locale::class);
}
public function current_locale(){
return $this->hasOne(Locale::class)->ofLocale(app()->getLocale());
}
$categories = Category::with('current_locale')->get();
$categories = Category::with('locales')->get();
Model::insert([
[row1], [row2], [row3], [row4],
]);
public static function fromRequest(FormRequest $request): static
{
$data = $request->validated();
$properties = (new \ReflectionClass(self::class))->getProperties(\ReflectionProperty::IS_PUBLIC);
foreach ($properties as $property){
if(!isset($data[$property->name])){
$data[$property->name] = $property->getDefaultValue();
}
}
return new static(...$data);
}
select
users.id, users.name, group_concat(country.country) as visited
from
users
left join user_to_country on user_to_country.user_id = users.id
left join country on user_to_country.country_id = country.id
where u_status = 1
group by users.id, users.name;
$students = Students::all()->where('class','=',$i);
$students = Students::where('class','=',$i)->get();
if (isset($i)) {
$students = Students::all()->where('class','=',$i);
} else {
$students = Students::all();
}
$students = Students::when($i, function($builder, $i){ return $builder->where('class', '=', $i); } )->get();
try {
$dbconnect = DB::connection()->getPDO();
$dbname = DB::connection()->getDatabaseName();
} catch(Exception $e) {
return redirect('/exception');
}
Для того, чтобы напрямую изменять элементы массива внутри цикла, переменной $value должен предшествовать знак &. В этом случае значение будет присвоено по ссылке.
foreach ($result as &$article) {
if (mb_stripos($article['title'], 'заголовок', 0, 'UTF-8') !== false) {
$article['image'] = 'images/image.jpg';
}
}
$countries = Country::whereHas('cities', function($builder){
return $builder->where('population', '>', 100000);
})
->with(['cities' => function($builder){
return $builder->where('population', '>', 100000)->orderBy('population', 'asc');
}])
->get();