groupBy只获得第一个ID

时间:2016-08-12 17:15:06

标签: php laravel eloquent laravel-5.2

部门用户之间存在多对多的关系,我的数据透视表是 department_user 。我希望使用 groupBy 方法选择所有department_name,具体取决于用户的部门,将所有department_name合并为一个。见下面我的陈述。

$departmentRecipient = DB::table('users')->select('departments.department_name', 'users.id')
    ->join('department_user', 'users.id', '=', 'department_user.user_id')
    ->join('departments', 'department_user.department_id', '=', 'departments.id')
    ->groupBy('departments.department_name')
    ->get();

使用die和dump的结果。

SC

正如你在这里看到的,我在“工程系”下有一个 4 的id。我的主要问题是它没有获取“工程部”下的所有 id 。但在我的SQL中,我的id 5 不仅 4 。我怎么解决这个问题?任何帮助将非常感谢。请看下面的结果。

SC2

输出:

SC3

这是我列表的输出。我想让所有users id属于用户的特定选项。但如果我选择“工程系”,它只会得到 4 而不是 5 的ID。我希望一次 4 5

Controlller:

public function getDocuments()
{
  $departmentRecipient = DB::table('departments')->get();
  return view ('document.create')->with('departmentRecipient', $departmentRecipient);
}

public function postDocuments(Request $request)
{
    $this->validate($request,
    [
        'title' => 'required|regex:/(^[A-Za-z0-9 ]+$)+/|max:255',
        'content' => 'required',
        'category' => 'required',
        'recipient' => 'required',
    ]);             


    $document = new Document();
                                //Request in the form
    $document->title = $request->title;
    $document->content = $request->content;
    $document->category_id = $request->category;

    $document->save();

    $user = Auth::user();


    foreach($request->recipient as $recipientId)
    {
        $document->sentToUsers()->sync([ $recipientId => ['sender_id' => $user->id]],false );
    }

}

模型

用户

public function departments()
{
    return $this->belongsToMany('App\Models\Department', 'department_user');
}

public function users()
{
    return $this->belongsToMany('\App\Models\User', 'department_user');
}

查看

<div class = "form-group">

        <label for = "recipient" class = "control-label">Recipient:</label>

        <select name = "recipient[]" multiple class = "form-control select2-multi" id = "myUserList">


            @foreach ($departmentRecipient as $list)
                <option value = "{{ $list->id }}">{{ $list->department_name }}</option>
            @endforeach

        </select>

</div>

1 个答案:

答案 0 :(得分:0)

根据您的代码,您似乎没有使用 Eloquent ORM ,而是使用查询生成器进行操作。 如果您现在没有性能问题,可以使用单独的查询来执行此操作。象 -

$departmentRecipient = DB::table('departments')->all();
foreach($departmentRecipient as $department){
$department->users = DB::table('department_user')->where('department_id',$department->id)->pluck('user_id');
}

但更好的方法是使用雄辩与关系。在您的用户部门的雄辩模型中定义多对多关系(假设您拥有雄辩的模型)。您可以在laravel documentation找到有关雄辩关系的详细信息。

<强>更新

从您的帖子更新来看,实际上很容易做到你想要的。如果请求包含所选的部门ID,则可以执行以下操作:

public function postDocuments(Request $request)
{

    $document = new Document();

    $document->title = $request->title;
    $document->content = $request->content;
    $document->category_id = $request->category;

    $document->save();
    //get the users list of the selected department id
    $selected_department = $request->department_id; //this must be included in your POST data
    $users = DB::table('department_user')->where('department_id',$selected_department)->pluck('user_id');
    //now you have the list of the users id
    foreach($users as $user){
    // do what you need here
    }
}

更新2:

以下控制器代码可能适合您。

控制器:

public function getDocuments()
{
    // I am suggesting here to change the '$departmentRecipient' to '$departmentlist', as it is more meaningful. Also in your view
    $departmentlist = DB::table('departments')->get();
    return view ('document.create')->with('departmentlist', $departmentlist);
}
public function postDocuments(Request $request)
{
    //same as you have till the $document->save()
    ....
    ....
    //then do this
    $recipients = array();
    $departments = $request->recipient;
    foreach($departments as $department){
        $users = DB::table('department_user')->where('department_id',$department)->pluck('user_id');
        $recipients = array_merge($recipients, $users);
    }
    //now you have a complete list of the users from selected departments
    //Do as you intend to like this
    $user = Auth::user();
    foreach($recipients as $recipientId)
    {
        $document->sentToUsers()->sync([ $recipientId => ['sender_id' => $user->id]],false );
    }
}