Laravel雄辩:BelongTomany系列获得价值

Laravel雄辩:BelongTomany系列获得价值,laravel,eloquent,Laravel,Eloquent,我有以下关系 任务与类别之间存在多对多关系 类别与类型有一对多的关系 目前,我正在任务控制器中使用此功能来获取我所需的所有数据,这些数据几乎可以正常工作: $type = Type::with('categories.tasks') ->where('slug', $type) ->first(); 此返回类型,带有类别集合,每个类别都有一个任务集合,每个任务都有一个类别集合。这个结构在设计上是正确的,问题是任务的category子集合返回所有类别,

我有以下关系

任务与类别之间存在多对多关系 类别与类型有一对多的关系

目前,我正在任务控制器中使用此功能来获取我所需的所有数据,这些数据几乎可以正常工作:

$type = Type::with('categories.tasks')
        ->where('slug', $type)
        ->first();
此返回类型,带有类别集合,每个类别都有一个任务集合,每个任务都有一个类别集合。这个结构在设计上是正确的,问题是任务的category子集合返回所有类别,但我希望它被限制为与父类别集合相同的条件,即
where('slug',$type)
我希望集合返回不是数组,因为如果应用了条件,结果将始终只返回一个类别

我尝试过使用scope,但没有成功。任何帮助都将不胜感激

我的桌子和各自的模型

类型表

CREATE TABLE `types` (
      `id` int(11) NOT NULL,
      `slug` varchar(60) NOT NULL,
      `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
      `updated_at` timestamp NOT NULL DEFAULT current_timestamp()
    ) 
类型模型

 class Type extends Model{
 public function categories()
    {
        return $this->morphMany(Category::class, 'categorize');
    }

    public function project()
    {
        return $this->belongsTo(Project::class);
    }
    }
类别表

CREATE TABLE `categories` (
  `id` int(10) NOT NULL,
  `title` varchar(128) NOT NULL,
  `color` varchar(10) NOT NULL,
  `content` varchar(128) NOT NULL,
  `categorize_id` int(6) NOT NULL,
  `categorize_type` varchar(256) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT current_timestamp()
)
类别模型:

  class Category extends Model
  {
 public function categorize(){
        return $this->morphTo();
    }
    public function tasks(){
        return $this->belongsToMany(Task::class,'task_category');
    }
    public function types(){
        return $this->belongsTo(Type::class);
    }
    }
任务表

CREATE TABLE `tasks` (
  `id` int(10) UNSIGNED NOT NULL,
  `project_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `desc` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `is_completed` tinyint(1) NOT NULL DEFAULT 0,
  `start` timestamp NOT NULL DEFAULT current_timestamp(),
  `end` timestamp NULL DEFAULT NULL,
  `allDay` tinyint(1) NOT NULL DEFAULT 0,
  `created_at` timestamp NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT current_timestamp(),
  `deleted_at` timestamp NULL DEFAULT NULL
) 
透视表

CREATE TABLE `task_category` (
  `task_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL
) 
任务模型

   class Task extends Model
{

 public function category(){
      return $this->belongsToMany(Category::class,'task_category','task_id','category_id');
    }
    }

编辑:我在使用来自

$type=type::with('categories.tasks'))
->其中('slug',$type)
->第一个();
如果您从这样的任务中获得类别

foreach($type->categories as$category){
foreach($category->tasks as$task){
$wantedCategory=$task->categories;//类似这样
}
}
然后是的,它将为您提供与该任务相关的所有类别

您需要的只是使用循环(或索引)中的变量
$category


如果您提供数据库结构以及如何尝试恢复有问题的类别,我们可以为您提供更多帮助。

请添加您的数据库结构这很难实现read@ml59重新表述了我的问题,并添加了关于tables@Flame重新表述我的问题并添加有关表的所有信息information@Breezer你重新措辞之后,我说的似乎是对的。不要从子集合中查找类别,从父集合中获取。是的,这与我想要的最接近,但我希望可以在模型中定义这种关系somehow@Breezer您无法定义该关系,因为它依赖于一个状态,并随状态而变化。