Laravel 5 查询关系
Laravel 5 Querying Relationship
这里是关系 1 的代码:
/**
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function address()
{
return $this->hasMany('App\IPAddress', 'group_id');
}
和关系2代码:
/**
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function group()
{
return $this->belongsTo('App\IPGroups');
}
I want to get all ip addresses that belongs to specified group. I don't want to write raw queries, I need to be done with querying relationship. Does anyone has an idea?
我试过这样做:
/**
* Get IP Addresses of specified group
* @param Request $request
* @return mixed
*/
public function getIP(Request $request)
{
$group = IPGroups::findOrFail($request->group_id);
return $group->address;
}
but I need to add one where statement where I can pick only active ip addresses.
这是模型 1 代码:
namespace App;
use Illuminate\Database\Eloquent\Model;
class IPGroups extends Model
{
/**
* Working Table
* @var string
*/
protected $table = 'ip_groups';
/**
* Guarded Values From Mass Assignment
* @var array
*/
protected $guarded = [ 'id' ];
/**
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function address()
{
return $this->hasMany('App\IPAddress', 'group_id');
}
}
和第二个模型代码:
namespace App;
use Illuminate\Database\Eloquent\Model;
class IPAddress extends Model
{
/**
* Working Table
* @var string
*/
protected $table = 'ips';
/**
* Protected Values From Mass Assignment
* @var array
*/
protected $fillable = [ 'group_id', 'ip', 'description', 'status' ];
/**
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function group()
{
return $this->belongsTo('App\IPGroups');
}
}
试试这个,只获取状态为 'Active':
的地址
return $group->address->where('status','Active');
这不起作用的原因:
return $group->address->where('status','=','Active');
是我们这里使用的where
是classCollection
的where
,它不接受比较器作为第二个参数作为where
的 Model
做。
这里是关系 1 的代码:
/**
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function address()
{
return $this->hasMany('App\IPAddress', 'group_id');
}
和关系2代码:
/**
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function group()
{
return $this->belongsTo('App\IPGroups');
}
I want to get all ip addresses that belongs to specified group. I don't want to write raw queries, I need to be done with querying relationship. Does anyone has an idea?
我试过这样做:
/**
* Get IP Addresses of specified group
* @param Request $request
* @return mixed
*/
public function getIP(Request $request)
{
$group = IPGroups::findOrFail($request->group_id);
return $group->address;
}
but I need to add one where statement where I can pick only active ip addresses.
这是模型 1 代码:
namespace App;
use Illuminate\Database\Eloquent\Model;
class IPGroups extends Model
{
/**
* Working Table
* @var string
*/
protected $table = 'ip_groups';
/**
* Guarded Values From Mass Assignment
* @var array
*/
protected $guarded = [ 'id' ];
/**
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function address()
{
return $this->hasMany('App\IPAddress', 'group_id');
}
}
和第二个模型代码:
namespace App;
use Illuminate\Database\Eloquent\Model;
class IPAddress extends Model
{
/**
* Working Table
* @var string
*/
protected $table = 'ips';
/**
* Protected Values From Mass Assignment
* @var array
*/
protected $fillable = [ 'group_id', 'ip', 'description', 'status' ];
/**
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function group()
{
return $this->belongsTo('App\IPGroups');
}
}
试试这个,只获取状态为 'Active':
的地址return $group->address->where('status','Active');
这不起作用的原因:
return $group->address->where('status','=','Active');
是我们这里使用的where
是classCollection
的where
,它不接受比较器作为第二个参数作为where
的 Model
做。