我想知道是否有可能创建一个数据库查询并构建到该查询中的方法类似于渴望加载与laravel eloquent的工作方式。
我在我的控制器中有一个查询,可以获得他们之间关系的优惠,这就是日期和地点。我的模型如下:
日模型:
use Illuminate\Database\Eloquent\Model;
class Day extends Model
{
/**
* Get the offer days for the offer.
*/
protected $fillable = ['start_time','end_time'];
public function offers()
{
return $this->belongsToMany('App\Offer');
}
}
我的优惠模式:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use DB;
class Offer extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
use SoftDeletes;
protected $table = 'offers';
protected $dates = ['deleted_at'];
/**
* The attributes that are 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'
];
/**
* Get the offers for the 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');
}
}
我的场地模型:
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');
}
}
我想得到每个关系并将结果加载到查询中,以便我有主对象和关系,如下所示:
问题在于它看起来很棒且很有效,而且口才很简单,但我不能做更复杂的事情,如:
"离开场地并根据场地的距离加载优惠,并使用我使用的半正式公式。"
这需要像这样的查询,在理想的世界中,距离将在每个要约的主要退回的要约对象中,而不是在场地[]关系中:
$radius = '10000';
$query->having('distance', '<', $radius);
$query->orderBy('distance', 'desc');
我陷入困境的地方是我不知道如何编写正确的数据库查询来完成您在上图中看到的内容,这就是我想要的内容,最终我可以根据&#34;提供基于位置&#34;并订购它们。
我已经从下面的雄辩查询开始了:
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 ) ) ) as 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 ) ) ) as distance')
);
}
])
->get();
$response = [
'now' => [],
'later' => [],
'tomorrow' => [],
'featured' => []
];
// validOffers are all offers that fall within this stuff
foreach ($offersWithDays as $offerAll) {
$relation = $offerAll->days()->get();
$relationVenues = $offerAll->venue();
//var_dump($relation);
$theDays = array();
foreach ($relation as $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;
}
我想知道的是,我在eloquent中创建的代码与使用连接和选择的普通数据库查询一样容易完成吗?