如何将变量从foreach传递到视图?

时间:2016-08-17 15:10:11

标签: php laravel eloquent laravel-5.2

我试图将foreach中的变量传递给我的视图。所以我可以在我的选择表单中使用它。因为我在部门用户之间有两个表M:M关系。我需要获得department_name所属的所有user_id。我可以通过department_name发送数据。我在这里做了一下,请看一下。

DB Diagram:

SC

department_user

SC

正如您在此处所见,user_id是用户的ID,document_id是用户所属的位置。

型号:

系:

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

用户:

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

控制器:

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

    return view('document.create')->with('department', $department);
}

我在死后获取所有users_id并转储变量departmentRecipient

SC

查看:

<div class = "form-group">
    <label for = "recipient" class = "control-label">Recipient:</label>     
    <select name = "recipient[]" multiple class = "form-control select2-multi" id = "myUserList">
        @foreach ($department as $list)
            <option value = "{{ $list->user_id }}">{{ $list->department_name }}</option>
        @endforeach
    </select>

</div>

我想将我的控制器中的foreach $department改为我的选择表单。但它总是说。

  

尝试获取非对象的属性(查看:C:\ Users \ JohnFrancis \ LaravelFrancis \ resources \ views \ document \ create.blade.php)

目标:

SC

4 个答案:

答案 0 :(得分:2)

我没有在您的user_id对象的转储值中看到$departmentRecipient属性,这就是您收到错误的原因。但是,$departmentRecipient对象中有一个用户数组,您在foreach循环中创建了该数组。您正在挑选单个部门中的每个user_id并设置users $departmentRecipient对象的users属性,因此您将在public function getDocuments() { $departmentRecipient = DB::table('departments')->get(); $departmentUsers = array(); foreach ($departmentRecipient as $department) { $users = DB::table('department_user') ->where('department_id', '=', $department->id) ->pluck('user_id'); foreach ($users as $userId) { $departmentUsers[$userId] = $department->department_name; } } return view('document.create')->with('department', $department)->with('departmentUsers', $departmentUsers); } 属性中获取一个数组。在这里,我有一个解决方案,

$departmentUsers

并且您的视图内部循环遍历变量@foreach ($departmentUsers as $userId => $departmentName) <option value = "{{ $userId }}">{{ $departmentName }}</option> @endforeach ,就像这样,

select2

这样可行,但由于您的部门包含多个用户,因此您可以在select2下拉列表中多次获取单个部门名称。如果您通过foreach分享更多您的目标,那么我可以帮助您以其他方式解决您的问题。

此外,如果您有兴趣使用Eloquent,那么您可以摆脱大量的foldLeft循环。

答案 1 :(得分:2)

使用以下循环迭代部门用户并将其添加到数据透视表。

foreach($request->department as $departmentId) 
{ 
foreach(Department::find($departmentId->id)->users()->get() as $user1) //find the users belonging to the current department 
{ 
$document->sentToUsers()->sync([ $user1->id => ['sender_id' => $user->id]],false ); 
} 
}

同时从getDocuments()中删除以下代码,因为它是多余的:

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

答案 2 :(得分:1)

在您的情况下,您可以针对每个部门拥有多个用户。所以要使它与你的forearch代码一起正常工作。您需要确保每个离开时都获得一个用户记录。因此,修改控制器中的以下代码行。

$department->users = DB::table('department_user')->where('department_id', '=', $department->id)->pluck('user_id');

但是你想要显示部门的所有用户,那么你必须将foreach循环代码更改为视图。

答案 3 :(得分:1)

试用此代码

应用/系

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

应用/用户

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

控制器

use App/User;

public function getDocuments($userId,User $User)
{
    $getSpecificUser = $User->with('departments')->find($userid);

    return view('document.create')->compact('getSpecificUser');
}

查看

@foreach ($getSpecificUser as $getUser)
    @if(empty($getUser->departments) === false)
        @foreach ($getUser->departments as $departments)
            <option value = "{{ $getUser->id }}">{{ $departments->department_name }}</option>
        @endforeach
    @endif
@endforeach