代码之家  ›  专栏  ›  技术社区  ›  Sasha

返回空的雄辩

  •  0
  • Sasha  · 技术社区  · 6 年前

    我需要选择国家/地区名称(全名)作为国家/地区名称,但我将得到空值。有人知道我做错了什么吗?

    我有以下疑问:

    $clinics = Clinic::select(
                'name', 'description', 'special_notes', 'email', 'phone_number', 'emergency_number', 'city', 'address',
                'zip', 'state', 'lat as latitude', 'lng as longitude', 'gmaps_link',
                'url as web_site', 'social_media', 'opening_hours', 'general_practice', 'specialist_and_emergency',
                'subscribe', 'accessibility'
            )->with(['country' => function($query) {
                $query->select('full_name as country_name');
            }])
            ->get()->toArray();
    
            dd($clinics);
    

    具有以下关系:

    诊所:

    public function country()
        {
            return $this->belongsTo(Country::class, 'country_id', 'id');
        }
    

    国家:

    public function clinic()
        {
            return $this->hasMany(Clinic::class);
        }
    

    尽职调查 把这个给我 阵列 :

    0 => array:21 [▼
        "name" => "Woodridge Vet Surgery"
        "description" => "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        "special_notes" => "Holiday Special Notes for working hours"
        "email" => "info@woodridgevet.com.au"
        "phone_number" => "(07) 3209 4242"
        "emergency_number" => "Emergency Number"
        "city" => "Woodridge"
        "address" => "Shop 8, 91 Ewing Rd"
        "zip" => "QLD 4114"
        "state" => null
        "latitude" => "-27.6325612"
        "longitude" => "153.1091419"
        "gmaps_link" => null
        "web_site" => "http://www.woodridgevet.com.au"
        "social_media" => "[]"
        "opening_hours" => "{"friday-to": "18:00", "monday-to": "18:00", "sunday-to": "06:00", "friday-to2": "06:00", "monday-to2": "06:00", "sunday-to2": "06:00", "tuesday-to": "18:00", " ▶"
        "general_practice" => 1
        "specialist_and_emergency" => 1
        "subscribe" => 1
        "accessibility" => 0
        "country" => null
      ]
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Marcin Nabiałek    6 年前

    你应该确保包括 id 所选国家的。否则雄辩的人就无法约束你所发现的国家 country_id 临床模型专栏。所以不是

    ->with(['country' => function($query) {
         $query->select('full_name as country_name');
     }])
    

    您应该使用:

    ->with(['country' => function($query) {
         $query->select('id', 'full_name as country_name');
     }])
    
    推荐文章