Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Tomasz_Kisiel's avatar

Strange relationship - One To Four ?

I have two tables and two models. User and Task. My user can have three normal tasks and one special task. Tables look like this:

users : | id | task_1 | task_2 | task_3 | special_task |

tasks: | id | desc |

I want to create relation.. or scope.. or anything that let my do something like this.

$user->normalTasks();

And I get array which includes task_1, task_2 and task_3. It is also possible that the array will be empty because the user may not have any tasks at present or only have one or two.

I'm trying this:

public function task1() {
  return $this->belongsTo(Task::class, 'id');
}

public function task2() {
  return $this->belongsTo(Task::class, 'id');
}

public function task3() {
  return $this->belongsTo(Task::class, 'id');
}

public function normalTasks() {
    $a = $this->task1;
    $b = $this->task2;
    $c = $this->task3;

    return $a;
        ->merge($b)
        ->merge($c);
}

But I get only :

Call to undefined method App\Task::merge()

I thinking from few days, but I can't create any similar thing. Anybody helps ?

0 likes
5 replies
Romain's avatar

Hey, interesting question.

If you can, I would suggest that you re-think your architecture a little. To me it would be simpler to have a table for users and a table for tasks (like you so far) but, the tasks would have a column "special" that is false by default.

In your migration for tasks table: $table->boolean('special', false);

and then for your relationships: In User:

public function tasks() {
  return $this->hasMany(Task::class);
}

public function normalTasks() {
  return $this->tasks->where('special', '=', false);
}

public function spacialTasks() {
  return $this->tasks->where('special', '=', true);
}

Now the trick is then to check how many normal/special task you have on a user when creating a new task. Then deciding wether to create a new one or not. To know how many tasks you have: $user->normalTasks->count() :D

Sorry if the where conditions are off, I did it on top of my head but I'm sure you get the idea

Tomasz_Kisiel's avatar

Tasks are not divided into special and non-special ones. It depends on the user. The task may be special for the first user, but not for the second.

Romain's avatar

but then the user decides if it's special. I imagine that you have a submission form, there the user can decide on wether a task is special or not. That is fine, and my suggested structure will still work. All you need is your database to know if a task is special

kundefine's avatar
// you can try this. i don't know it will work or not
// if you get the task1, task2, task3 data then it will work
public function normalTasks() {
    $a = collect($this->task1); 
    $b = collect($this->task2);
    $c = collect($this->task3);

    return $a;
        ->merge($b)
        ->merge($c);
}
Tomasz_Kisiel's avatar

Finally I decided to make normal many to many relationship and control task programaticly but thanks for all sugestion.

Please or to participate in this conversation.