Skip to content

Instantly share code, notes, and snippets.

@dura0ok
Created March 17, 2019 07:48
Show Gist options
  • Save dura0ok/9e1342691f9fdb1b0674cdafc144b58e to your computer and use it in GitHub Desktop.
Save dura0ok/9e1342691f9fdb1b0674cdafc144b58e to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers\v2;
use App\Banner;
use App\Category;
use App\Faq;
use App\Shop;
use App\Shoplocation;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class IndexController extends Controller
{
public function index(Request $request)
{
// DB::listen(function ($query) {
// dump(str_replace_array(
// $search = '#' . str_random() . '#',
// $query->bindings,
// preg_replace('/\B\?/', $search, $query->sql))
// );
// });
$companies = Shop::byCity($request)
->visible()
->orderBy('rating', 'desc')
->orderBy('id', 'desc')
->limit(8)
->get();
$categories = Category::all();
$faqs = Faq::isVisible()->get();
$banners = Banner::byCity()->byVisibleBanners()->get();
$mapPoints = Shoplocation::byCity($request)->orderBy('id', 'DESC')
->with('shop:id,name')
->limit(50)->get();
return view('v2.index.index')
->with('companies', $companies)
->with('categories', $categories)
->with('faqs', $faqs)
->with('banners', $banners)
->with('mapPoints', $mapPoints);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
class Shoplocation extends Model
{
protected $guarded = [];
protected $hidden = ['created_at', 'updated_at', 'shop_id'];
public function shop(){
return $this->belongsTo(Shop::class);
}
public function scopeByCity($query)
{
return $query->whereHas('shop', function ($query) {
return $query->where('city_id', request()->hasCookie('loc') ? request()->cookie('loc') : 1);
});
}
}
"select * from `shoplocations` where exists (select * from `shops` where `shoplocations`.`shop_id` = `shops`.`id` and `city_id` = 1) order by `id` desc limit 50"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment