php - Is it possible to do relationship eager loading in a DB query with laravel? -
i wondered if possible create db query , build query similar approach way eager loading works laravel eloquent.
i have query in controller gets offers relationship point days , venue. models follows:
day model:
use illuminate\database\eloquent\model; class day extends model { /** * offer days offer. */ protected $fillable = ['start_time','end_time']; public function offers() { return $this->belongstomany('app\offer'); } }
my offer model:
namespace app; use illuminate\database\eloquent\model; use illuminate\database\eloquent\softdeletes; use db; class offer extends model { /** * database table used model. * * @var string */ use softdeletes; protected $table = 'offers'; protected $dates = ['deleted_at']; /** * attributes mass assignable. * * @var array */ protected $hidden = ['map_location', 'regular', 'name']; protected $fillable = ['id','venue_id','type_id','day', 'venue_address','is_featured','repeater_days','image', 'venue_description', 'offer_headline','offer_subheader','offer_terms','venue_phone_number','venue_website', 'venue_name', 'venue_headline','venue_description','venue_latitude','venue_longitude','offer_when','offer_end_time','offer_start_time','offer_start_date','offer_end_date','featured_date','current_time','current_date']; public static $rules = array( 'image' => 'image|mimes:jpeg,jpg,png,bmp,gif,svg' ); protected $casts = [ 'is_featured' => 'boolean', 'is_regular' => 'boolean', 'offer_when' => 'array' ]; /** * offers offer. */ public function days() { return $this->belongstomany('app\day','day_offer', 'offer_id', 'day_id'); } public function types() { return $this->belongstomany('app\type', 'offer_type'); } public function venue() { return $this->belongstomany('app\venue', 'offer_venue', 'offer_id', 'venue_id'); } }
my venue model:
namespace app; use illuminate\database\eloquent\model; use illuminate\database\eloquent\softdeletes; class venue extends model { // use softdeletes; protected $table = 'venues'; protected $dates = ['deleted_at']; protected $fillable = ['id','address','description','phone_number','website','name', 'headline','lat','lng','days']; public static $rules = array( 'name' => '' ); protected $casts = [ 'lat' => 'float(10,8)', 'lng' => 'float(11,8)' ]; public function offers() { return $this->belongsto('app\offer','offer_venue'); } }
i want each relationship , load results query have main object , relationships so:
the problem looks great , works , eloquent simple cannot further more complex things on like:
"getting distance venue , loading offers based on distance of venue haversine formula used."
this require query , in ideal world distance in main returned offer object on every offer not within venue[] relationship:
$radius = '10000'; $query->having('distance', '<', $radius); $query->orderby('distance', 'desc');
where becoming stuck not know how write proper db query see in image above want , can "offers based on location" , order them.
i have started eloquent query below:
public function indexall(offer $offer) { $mytime = carbon::now('europe/london'); $time = $mytime->format('h:i'); $timeint = $this->hourmintointeger($time); $today = $mytime->format('m/d/y'); $day = strtolower($mytime->format('l')); $tomorrow = strtolower(carbon::now()->addday(1)->format('l')); // $offers = offer::all(); //$haversine = db::raw('3959 * acos( cos( radians(37) ) * cos( radians( $offer ) ) * cos( radians( $offer ) - radians(-122) ) + sin( radians(37) ) * sin( radians( $offer ) ) ) distance'); $offerswithdays = offer::with([ 'days' => function($alldays){ $alldays->select('id','day'); }, 'venue' => function($query){ $query->select('lat','lng','address','description','phone_number','website','name', 'headline') ->addselect(db::raw('3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) distance') ); } ]) ->get(); $response = [ 'now' => [], 'later' => [], 'tomorrow' => [], 'featured' => [] ]; // validoffers offers fall within stuff foreach ($offerswithdays $offerall) { $relation = $offerall->days()->get(); $relationvenues = $offerall->venue(); //var_dump($relation); $thedays = array(); foreach ($relation $theday) { $thedayobj = $theday->day; array_push($thedays,$thedayobj); } extract($thedays); if ($offerall->is_featured) { $response['featured'][] = $offerall; } if (in_array($day,$thedays) && $offerall->offer_start_time < $time) { $response['now'][] = $offerall; } if (in_array($day,$thedays) && $offerall->offer_start_time > $time) { $response['later'][] = $offerall; } if (in_array($tomorrow,$thedays)) { $response['tomorrow'][] = $offerall; } } return $response; }
what know code have created in eloquent done normal db query joins , selects in it?
Comments
Post a Comment