Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPLaravel如何使用两个表进行排序_Php_Mysql_Laravel - Fatal编程技术网

PHPLaravel如何使用两个表进行排序

PHPLaravel如何使用两个表进行排序,php,mysql,laravel,Php,Mysql,Laravel,在我的Laravel-应用程序中有两个MySQL表,一个名为categories,另一个名为employees。类别表的结构为: id category order 而employees表中也有名为: id category order 因此,假设我有类别如:顾问、程序员和管理人员,当我在后端创建员工时,我可以将新员工分配到这些类别中的一个。现在,在我的Laravel-应用程序的前端,我希望按类别显示员工,以及按给定顺序显示类别。假设顾问的顺序是2,程序员的顺序是1,管理的顺序是3 现在,我

在我的
Laravel
-应用程序中有两个MySQL表,一个名为
categories
,另一个名为
employees
类别表的结构为:

id
category
order
employees
表中也有名为:

id
category
order
因此,假设我有
类别
如:顾问、程序员和管理人员,当我在后端创建员工时,我可以将新员工分配到这些类别中的一个。现在,在我的
Laravel
-应用程序的前端,我希望按类别显示员工,以及按给定顺序显示类别。假设顾问的顺序是2,程序员的顺序是1,管理的顺序是3

现在,我的控制器如下所示:

use App\Employee;

class EmployeesController extends Controller
{

    public function index()
    {
       $employees = Employee::all()->groupBy('category');

       return view('app.employee.index', compact('employees'));
    }
}
Programmers (since this category has order of 1)
 // employees with category "programmers" here

Consultants (2)
 // employees with category "consultants" here

Administration (3)
 // employees with category "administration" here
和我的刀片视图文件:

@foreach ($employees as $category => $workers)
  <div class="col text-center mb-6">
    <h2>{{ $category }}</h2>
  </div>
  <div class="row px-4 px-xl-10">
    @foreach($workers->sortBy('order') as $worker)
      // content of the employee
    @endforeach
 </div>
@endforeach

我认为您必须将查询更改为:

$categories = Category::with(['employees'=>function($q){
   $q->orderBy('order');
}])->orderBy('order')->get();

对我来说,您的列定义有点混乱,我可以建议更改您的列吗:

Table 'categories'
------------------
id
name
order

Table 'employees'
-----------------
id
category_id
将外键添加到employees表:

$table->foreign('category_id')->references('id')->on('categories')
然后,您的模型可以通过关系方法相互映射:

class Employee extends Model
{
    public function category()
    { 
        return $this->belongsTo(Category::class);
    }
}

class Category extends Model
{
    public function employees()
    { 
        return $this->hasMany(Employee::class);
    }
}
因此,有了它,我们可以通过以下方式从控制器查询数据库:

use App\Category;

class EmployeesController extends Controller
{

    public function index()
    {
       $categories = Category::orderBy('order')->get();

       return view('app.employee.index', compact('categories'));
    }
}
并在刀片视图中显示结果:

@foreach ($categories as $category)
  <div class="col text-center mb-6">
    <h2>{{ $category->name }}</h2>
  </div>
  <div class="row px-4 px-xl-10">
    @foreach($category->employees as $employee)
      // content of the employee
    @endforeach
 </div>
@endforeach
@foreach($categories作为$category)
{{$category->name}
@foreach($category->员工为$employee)
//员工的内容
@endforeach
@endforeach

我不太清楚。你能举一个输入和预期输出的例子吗?@vivek_23查看更新后的问题:-)那么用于对员工进行排序的
顺序是从员工表或
类别表中获得的?哦,好的,所以当我做对后,我还需要添加一个
类别
模型?是的,当我尝试在('categories')上执行
$table->foreign('category_id')->references('id')->时,使用Laravel在模型中使用内置的雄辩支持来进行数据查询时,它会有很大帮助
我收到一个错误:
完整性约束冲突:1452无法添加或更新子行:外键约束失败……约束“employees\u category\u id\u foreign”外键(“category\u id”)引用“categories”(“id”)
:-/这是因为您试图将外键添加到表中,但尚未创建。更改迁移文件的顺序或在上次迁移文件中创建所有外键