Laravel一对一关系在某些数据上返回null

时间:2017-04-27 10:08:11

标签: php json laravel-5 eloquent relationship

我有两个相关的表,我需要使用laravel的eloquent来获取一些数据。这是我的table。 我创建了两个模型:User和UserCurrentPosition定义了这两个模型之间的关系。

user.php的     

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    public function position()
    {
        return $this->hasOne('App\UserCurrentPosition', 'user_id', 'id');
    }

}

UserCurrentPosition.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class UserCurrentPosition extends Model
{

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

    public function scopeDistance($query, $dist, $location)
    {
        return $query->select(DB::raw('ST_X(location) AS longitude, ST_Y(location) AS latitude, ST_DISTANCE_SPHERE(location, POINT('.$location.')) AS dist'))
            ->havingRaw('dist < '.$dist)
            ->orderBy('dist', 'ASC');
    }
}

我打算从用户表中获取姓名和电话,以及经度,纬度和距其他表的距离。这就是我所做的:

<?php

namespace App\Http\Controllers\Api;

use App\User;
use App\UserCurrentPosition;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use Dingo\Api\Routing\Helpers;

class DriverController extends Controller
{
    use Helpers;
    public function __construct(User $user, UserCurrentPosition $userpos, UserDetail $userdetail)
    {
        $this->user = $user;
        $this->userdetail = $userdetail;
        $this->userpos = $userpos;
    }

    public function getNearby(Request $request)
    {
        $dist = $request->get('dist');
        $lon = $request->get('lon');
        $lat = $request->get('lat');

        return $this->userpos
            ->with('user')
            ->distance($dist, $lon.', '.$lat)
            ->take(3)
            ->get()
            ->toJson();
    }
}

但是输出是这样的:

[
  {
    "longitude": 112.745978,
    "latitude": -7.286319,
    "dist": 5824.995681344659,
    "user": null
  },
  {
    "longitude": 112.744166,
    "latitude": -7.310827,
    "dist": 6862.04670609193,
    "user": null
  },
  {
    "longitude": 112.73957,
    "latitude": -7.249031,
    "dist": 7394.147599012171,
    "user": null
  }
]

我想获取用户数据以及经度,纬度和dist而不是“null”。我该怎么做?

提前致谢。

3 个答案:

答案 0 :(得分:1)

问题很简单,你使用了错误的外键配置:

Laravel官方文档:

return $this->hasOne('App\Phone', 'foreign_key', 'local_key');

您在两个模型中使用相同的配置:

UserCurrentPosition.php

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

user.php的

public function position()
{
    return $this->hasOne('App\UserCurrentPosition', 'user_id', 'id');
}

要解决此问题,我建议您检查一下按键。 马可

答案 1 :(得分:0)

您只需将此代码放在特定文件

上即可

<强> user.php的

public function position()
    {
        return $this->hasOne('App\UserCurrentPosition');
    }

<强> UserCurrentPosition.php

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

答案 2 :(得分:0)

在选择查询中添加user_id

public function scopeDistance($query, $dist, $location)
    {
        return $query->select(DB::raw('user_id , ST_X(location) AS longitude, ST_Y(location) AS latitude, ST_DISTANCE_SPHERE(location, POINT('.$location.')) AS dist '))
            ->havingRaw('dist orderBy('dist', 'ASC');
    }