Skip to content

Instantly share code, notes, and snippets.

@Benyaminrmb
Created October 14, 2023 20:22
Show Gist options
  • Save Benyaminrmb/40027ff91efd352f20c6bf79c3fb3198 to your computer and use it in GitHub Desktop.
Save Benyaminrmb/40027ff91efd352f20c6bf79c3fb3198 to your computer and use it in GitHub Desktop.
this is home method
public function home(Request $request)
{
/** @var User $user */
$user = auth()->user();
$doesHaveCategories = $user->categories->isNotEmpty();
$doesHaveCities = $user->cities->isNotEmpty();
$events = Event::approved()->with(['dateArray', 'channel', 'categories', 'eventLocation'])
->whereHas('dateArray', function (Builder $query) use ($request) {
if (empty($request->type) || $request->type !== 'past') {
$query->where('start_date', '>', Carbon::now()->toDateTimeString());
}
if (!empty($request->jump_date)) {
$date = Carbon::now()->add($request->jump_date)->format('Y-m-d');
$query->where(DB::raw('DATE_FORMAT(start_date, "%Y-%m-%d")'), $date);
}
if (!empty($request->date)) {
$date = $request->date;
$query->whereDate('start_date', Carbon::create($date)->format('Y-m-d'));
}
if (!empty($request->type)) {
if($request->type === 'week'){
$startOfWeek = Carbon::now()->locale('fa-IR')->startOfWeek()->format('Y-m-d');
$endOfWeek = Carbon::now()->locale('fa-IR')->endOfWeek()->format('Y-m-d');
$query->whereBetween('start_date', [$startOfWeek, $endOfWeek]);
}
}
})
->whereIn('channel_id', $user->followings->pluck('id')->push($user->channel->id))
->where(function (Builder $query) {
/*if ($doesHaveCategories) {
$query->whereHas('categories', function (Builder $query) use ($user) {
$query->whereIn('category_id', $user->categories->pluck('id'));
});
}
if ($doesHaveCities) {
$query->whereHas('eventLocation', function (Builder $query) use ($user) {
$query->whereIn('city_id', $user->cities->pluck('id'));
});
}*/
})
->groupBy('id')
->orderBy('id', 'desc')
->paginate(config('evento.per_page_channels'))
->through(function ($item) {
return TinyEventChannelResource::make($item);
});
return $this->generateResponse(
result: $events,
message: __('events.list.success')
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment