我正在尝试使用Yajra数据表从数据透视表中检索数据。
模型Project.php
class Project extends Model
{
protected $fillable = ['id','client_id', 'name','description', 'projectType','service_id','files','startDate','endDate','progress','status'];
public function employee()
{
return $this->belongsToMany('App\Employee');
}
}
模型Employee.php
class Employee extends Model
{
protected $fillable = ['id', 'name', 'surname','address','tel', 'email','profession'];
public function project()
{
return $this->belongsToMany('App\Project');
}
}
模型EmployeeProject.php
class EmployeeProject extends Pivot
{
protected $fillable = ['id', 'employee_id', 'project_id'];
}
ProjectsController.php
$projects = Project::with('employee');
return Datatables::of($projects)
->make(true);
projects.blade.php
$('#datatable').DataTable({
"processing": true,
"serverSide": true,
"paging": false,
"ajax": "{{ route('projects.getdata') }}",
"order": [[1,'desc']],
"columns": [
{"data": "id"},
{"data": "client_id"},
{"data": "name"},
{"data": "description"},
{"data": "projectType"},
{"data": "service_id"},
{"data": "employees.name"}, - **I need to retrieve the names of the employees who are involved in the project**
{"data": "files"},
{"data": "startDate"},
{"data": "endDate"},
{"data": "progress", orderable:false, searchable: false},
{"data": "status"},
让您知道我正在使用数据透视表employee_project链接表(员工和项目)。创建功能运行良好,因为记录已正确添加到数据透视表中。 问题是我需要将所有参与该项目的员工的姓名涂上去,例如:
id| name | employeeName |...|
-----------------------------
1 | test | John, Mark,...|...|
谢谢。
答案 0 :(得分:3)
尝试这样的事情:
在projects.blade.php
{
data: function (row) {
let employeeNames= [];
$(row.employee).each(function (i, e) {
employeeNames.push(e.name);
});
return employeeNames.join(", ")
}, name: 'employee.name'
},