在Laravel 5.7中获取与各自的关系到关系数据

萨斯瓦特

让我给您一个基本的概述:

有一个应用程序,人们可以在其中注册宠物,并在宠物丢失时报告。此外,看过丢失宠物的人会报告发现该宠物,以便通知宠物主人,并可以取回宠物。

我有五个模型:

颜色模型

class Color extends Model
{
    public function colorPet()
    {
        return $this->hasMany('App\Models\Pet');
    }
}

宽模型

class Breed extends Model
{
    public function breedPetType()
    {
        return $this->belongsTo('App\Models\PetType', 'pet_type_id');
    }

    public function breedPet()
    {
        return $this->hasMany('App\Models\Pet');
    }
}

宠物类型模型

class PetType extends Model
{
    public function petTypeBreed()
    {
        return $this->hasMany('App\Models\Breed');
    }
}

宠物模型

class Pet extends Model
{
    public function petBreed()
    {
        return $this->belongsTo('App\Models\Breed', 'breed_id');
    }

    public function petPetType()
    {
        return $this->belongsTo('App\Models\PetType', 'pet_type_id');
    }

    public function petColor()
    {
        return $this->belongsTo('App\Models\Color', 'color_id');
    }

    public function petUser()
    {
        return $this->belongsTo('App\User', 'user_id');
    }
}

遗失报告

class LostReport extends Model
{
    public function lostReportPet()
    {
        return $this->belongsTo('App\Models\Pet', 'pet_id');
    }

    public function lostReportUser()
    {
        return $this->belongsTo('App\User', 'user_id');
    }

    public function lostReportPetSighting()
    {
        return $this->hasMany('App\Models\PetSighting', 'lost_report_id');
    }
}

宠物瞄准

class PetSighting extends Model
{
    public function petSightingLostReport()
    {
        return $this->belongsTo('App\Models\LostReport', 'lost_report_id');
    }
}

这是纽约查询:

$petSightingRadiusQueryString = "( $unitDistance * acos( cos( radians($latitude) ) * cos( radians( pet_sightings.latitude ) ) 
    * cos( radians( pet_sightings.longitude ) - radians($longitude) ) + sin( radians($latitude) ) * sin(radians(pet_sightings.latitude)) ) )";

$lostPetQuery   = LostReport::with('lostReportPet')
                            ->where(array(
                                'lost_reports.is_found' => Globals::SMALL_CHAR_NO))
                            ->whereHas('lostReportPet', function($queryReportPet) {
                                $queryReportPet->where(array(
                                            'pets.status'       => Globals::SMALL_CHAR_ACTIVE,
                                            'pets.is_delete'    => Globals::SMALL_CHAR_NO,
                                            'pets.is_lost'      => Globals::SMALL_CHAR_YES
                                ));
                            });

$lostPetQuery   = $lostPetQuery->where(function($orQuery) use($userId, $petTypeArrayForLostPet, $lostPetRadiusQueryString, $lostPetRadius, $petSightingRadiusQueryString, $petSightingRadius){
                        $orQuery->where('lost_reports.user_id', $userId) // where the post report is by owner
                                ->orWhere(function($lostPetNotificationQuery) use($petTypeArrayForLostPet, $lostPetRadiusQueryString, $lostPetRadius){
                                    $lostPetNotificationQuery->whereIn('pets.pet_type_id', $petTypeArrayForLostPet)
                                                    ->whereRaw($lostPetRadiusQueryString . ' < ' . $lostPetRadius);
                                }) // where the lost report is of same pet type
                                ->orWhereHas('lostReportPetSighting', function($petSightingNotificationQuery) use ($petSightingRadiusQueryString, $petSightingRadius){
                                    $petSightingNotificationQuery->whereRaw("pet_sightings.id = (SELECT MAX(pet_sightings.id) FROM pet_sightings WHERE pet_sightings.lost_report_id = lost_reports.id) AND " .  $petSightingRadiusQueryString . " < " . $petSightingRadius);
                                }); // where pet sighting is enabled
                    });

这是我得到的结果:

Array
(
    [id] => 1
    [pet_id] => 3
    [user_id] => 2
    [phone] => 6290453837
    [latitude] => 22.572645
    [longitude] => 88.363892
    [missing_date] => 2020-03-03 00:00:00
    [found_date] => 
    [is_found] => n
    [is_delete] => n
    [created_date] => 2020-03-03 15:08:03
    [modified_date] => 2020-03-03 15:08:03
    [created_at] => 2020-03-03 15:08:03
    [updated_at] => 2020-03-03 15:08:03
    [lost_report_pet] => Array
        (
            [id] => 3
            [name] => Micky
            [image] => p-1583228283-3.jpg
            [pet_type_id] => 1
            [breed_id] => 1
            [color_id] => 1
            [age] => 2
            [weight] => 7
            [description] => Very cute doggo
            [is_approachable] => y
            [user_id] => 2
            [status] => a
            [is_delete] => n
            [is_lost] => y
            [created_date] => 2020-03-03 15:08:03
            [modified_date] => 2020-03-03 15:08:03
            [created_at] => 2020-03-03 15:08:03
            [updated_at] => 2020-03-03 15:08:03
        )

)

如您所见,这种关系'lost_report_pet'如何从中获得品种,颜色和pet_type关系lost_report_pet

铜绿

我很确定您可以尝试使用关系点表示法:

LostReport::with([
    'lostReportPet.petBreed', 
    'lostReportPet.petPetType',
    'lostReportPet.petColor'
]);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章