在Laravel刀片枢轴表中收集相同ID

rqdpfwrv  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(80)

我有一个带有两个外键的透视表,我想根据一个外键列出项目。

id | tool_id | user_id
    ______________________
    1    2         12
    2    5         12
    3    3         12
    4    4          7

我有表之间的关系,我想按用户列出工具,如下所示:

No: User  Tools
    --- ----  -----
      1 John  2,3,5
      2 Sara  4

我循环了记录,但您可以猜到它的列表为:

No: User  Tools
--- ----  -----
1   John  2
2   John  3
3   John  5
4   Sara  4

----or----

No: User  Tools
--- ----  -----
1   John  2,3,5
2   John  2,3,5
3   John  2,3,5
4   Sara  4

我怎样才能把它们列出来呢?

No: User  Tools
 --- ----  -----
   1 John  2,3,5
   2 Sara  4

这是迄今为止尝试过的:
$assign从透视表中获取所有数据,架构位于post的顶部。

<table class="table mb-0">
            <thead>
            <tr>
                <th scope="col">Tool</th>
                <th scope="col">Employee</th>
                <th scope="col">Department</th>
            </tr>
            </thead>
            <tbody>
            @foreach($assigns as $assign)
                <tr>
                    <td>{{$assign->employee->last_name." ".$assign->employee->first_name}}</td>
                    <td>{{$assign->employee->department->name}}</td>
                    <td>
                        @foreach($assigns->where('employee_id', $assign->employee_id) as $tool)
                            {{$tool->tool->tool_code}}
                        @endforeach
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
    • 控制器:**
public function assignLetter(){
        $assigns = ToolAssign::all();
        $employees = Employee::where('status', 1)->where('is_inspector', 1)->orderBy('last_name')->get();
        return view('tool.assign', compact('assigns', 'employees'));
    }
    • 透视表的模型**
public function employee()

    {
        return $this->belongsTo(Employee::class, "employee_id", "id");
    }
    
    public function tool()
    {
        return $this->belongsTo(Tool::class, "tool_id", "id");
    }
0md85ypi

0md85ypi1#

我认为你应该把你的关系类型从belongsTo改为belongsToMany,就像这样:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class User extends Model
{
    // ...

    /**
     * Get the tools that the user belongs to.
     *
     * @return BelongsToMany
     */
    public function tools(): BelongsToMany
    {
        return $this->belongsToMany(Tool::class);
    }
}

这样,您可以执行以下操作:(未测试)

foreach (User::all() as $user) {
    echo $user->id . ' - ' . $user->name . ' - ' . $user->tool->pluck('id')->join(', ');
}

编辑:
这里是一个例子,让你的代码达到你想要的,但它真的很混乱。我建议你尝试我的代码以上修复它。

<table class="table mb-0">
        <thead>
        <tr>
            <th scope="col">Tool</th>
            <th scope="col">Employee</th>
            <th scope="col">Department</th>
        </tr>
        </thead>
        <tbody>
        @foreach($assigns as $assign)
            <tr>
                <td>{{$assign->employee->last_name." ".$assign->employee->first_name}}</td>
                <td>{{$assign->employee->department->name}}</td>
                <td>
                    {{ $assigns->where('employee_id', $assign->employee_id)->get()->map(function($tool) { return $tool->tool->tool_code; })->join(', '); }}
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
sdnqo3pr

sdnqo3pr2#

我解决了困难的方式。它不愚蠢,如果它的工作:)

<table>
    <thead>
        <tr>
            <th scope="col">Tool</th>
            <th scope="col">Employee</th>
            <th scope="col">Department</th>
        </tr>
    </thead>
    <tbody>
    @php($index = 1)
    @foreach($employees as $collect)
        @if(\App\Models\ToolAssign::where('employee_id', $collect->id)->count() > 0)
            <tr>
                <th scope="row">{{$index}}</th>
                <td>{{$collect->last_name." ".$collect->first_name}}</td>
                <td>{{$collect->department->name}}</td>
                <td>
                    @foreach(\App\Models\ToolAssign::where('employee_id', $collect->id)->get() as $codes)
                        @if($loop->last)
                            {{$codes->tool->tool_code}}
                        @else
                            {{$codes->tool->tool_code}},
                        @endif
                    @endforeach
                </td>
            </tr>
            @php($index ++)
        @endif
    @endforeach
    </tbody>
</table>

相关问题