File: /var/www/vhost/disk-apps/comfama.sports-crowd.com/app/Http/Controllers/Api/BannersApiController.php
<?php
namespace App\Http\Controllers\Api;
use App\Banner;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
class BannersApiController extends Controller
{
public function getBanners($target)
{
$user_id = Auth::user()->id;
$now = Carbon::now();
$bannersForUser = Banner::select('banners.*')
->where('banners.active', true)
->join('banner_tags', 'banners.id', '=', 'banner_tags.banner_id')
->join('user_tags', 'user_tags.tag_id', '=', 'banner_tags.tag_id')
->where('user_tags.user_id', '=', $user_id)
->where('banners.target', 'LIKE', '%' . $target . '%')
->where(function ($query) use ($now) {
$query->whereNull('banners.start_date') // NO PROGRAMADOS
->orWhere(function ($subQuery) use ($now) { // PROGRAMADOS
$subQuery->where('banners.start_date', '<=', $now)
->where('banners.end_date', '>=', $now);
});
})
->orderBy('banners.order', 'ASC')
->get();
$bannersForAll = Banner::select('banners.*')
->where('banners.active', true)
->leftjoin('banner_tags', 'banners.id', '=', 'banner_tags.banner_id')
->whereNull('banner_tags.id')
->where('banners.target', 'LIKE', '%' . $target . '%')
->where(function ($query) use ($now) {
$query->whereNull('banners.start_date') // NO PROGRAMADOS
->orWhere(function ($subQuery) use ($now) { // PROGRAMADOS
$subQuery->where('banners.start_date', '<=', $now)
->where('banners.end_date', '>=', $now);
});
})
->orderBy('banners.order', 'ASC')
->get();
return response()->json($bannersForUser->merge($bannersForAll), 200);
}
}