Php 拉雷维尔:雄辩,多对多的关系,我如何只加载相关模型的id?

Php 拉雷维尔:雄辩,多对多的关系,我如何只加载相关模型的id?,php,mysql,laravel,laravel-4,Php,Mysql,Laravel,Laravel 4,我正在使用Eloquent从一个名为“businesss”的表中选择一行。任何业务都可以与一个或多个“行业”相关联。我在商业模式中建立了这样一种模式: public function industries() { return $this->belongsToMany('Industry'); } 在控制器中,当我选择模型时,我渴望加载行业: $mdl = Business::find($business_id); $mdl->load('industries'); ret

我正在使用Eloquent从一个名为“businesss”的表中选择一行。任何业务都可以与一个或多个“行业”相关联。我在商业模式中建立了这样一种模式:

public function industries() {
    return $this->belongsToMany('Industry');
}
在控制器中,当我选择模型时,我渴望加载行业:

$mdl = Business::find($business_id);
$mdl->load('industries');
return Response::json($mdl);
这将返回如下内容:

{"id":123,"name":"My Business","address":"123 Easy Street","industries":[{"id":1,"name":"Technology"},{"id":7,"name":"Research"}]}
$ind = array();
foreach($mdl->industries as $industry) $ind[] = $industry->id;
$mdl->industries = $ind;
$mdl->industries()->sync(Input::get('industries'));
这几乎就是我需要的。我只是想让回应看起来像这样:

{"id":123,"name":"My Business","address":"123 Easy Street","industries":[1,7]}
我知道我可以这样做:

{"id":123,"name":"My Business","address":"123 Easy Street","industries":[{"id":1,"name":"Technology"},{"id":7,"name":"Research"}]}
$ind = array();
foreach($mdl->industries as $industry) $ind[] = $industry->id;
$mdl->industries = $ind;
$mdl->industries()->sync(Input::get('industries'));
不过我还是不想那样做。这是丑陋的,它使我的代码看起来非常不拉威尔

我之所以想这样做,首先是因为当我保存业务时,我将使用以下内容:

{"id":123,"name":"My Business","address":"123 Easy Street","industries":[{"id":1,"name":"Technology"},{"id":7,"name":"Research"}]}
$ind = array();
foreach($mdl->industries as $industry) $ind[] = $industry->id;
$mdl->industries = $ind;
$mdl->industries()->sync(Input::get('industries'));
SYNC方法只接受一个id数组,因此我希望服务器上的JSON对象具有相同的格式。我真的不希望在加载时使用对象数组,而希望在保存时使用ID数组。对于这类事情,有没有一个最佳的实践方法或者一个更简单的方法来完成我想要做的事情

谢谢

$mdl = Business::with('industries')->find($business_id)->toArray();
$mdl['industries'] = array_pluck($mdl['industries'], 'id');
return $mdl;

如果您经常使用此方法,请将以下方法添加到您的
业务
模型中:

public static function getWithIdustryIds ($business_id)
{
    $mdl = Business::with('industries')->find($business_id)->toArray();
    $mdl['industries'] = array_pluck($mdl['industries'], 'id');
    return $mdl;
}

如果您经常使用此方法,请将以下方法添加到您的
业务
模型中:

public static function getWithIdustryIds ($business_id)
{
    $mdl = Business::with('industries')->find($business_id)->toArray();
    $mdl['industries'] = array_pluck($mdl['industries'], 'id');
    return $mdl;
}

你的模范关系应该是

public function industries() {
    return $this->belongsToMany(Industry::class);
}
假设透视表有一个行业id列,则可以使用

$mdl = Business::with(['industries' => function($query){
    $query->select('industry_id');
}])->get()

你的模范关系应该是

public function industries() {
    return $this->belongsToMany(Industry::class);
}
假设透视表有一个行业id列,则可以使用

$mdl = Business::with(['industries' => function($query){
    $query->select('industry_id');
}])->get()

有没有办法在模型中做到这一点,这样我就不需要在每个控制器中重复这些代码?@BenjaminOman-只需在模型中添加一个静态方法。我更新了答案。有没有办法在模型中做到这一点,这样我就不需要在每个控制器中重复这些代码?@BenjaminOman-只需在模型中添加一个静态方法。我更新了答案。