4 months ago

Unique fields with conditions

Posted 4 months ago by Romentigo

I have the following migration:


public function up()
        Schema::create('pairs', function (Blueprint $table) {
            $table->unique(['week_id', 'day_id', 'classroom_id', 'pair_number_id']);
            $table->unique(['group_id', 'day_id', 'pair_number_id', 'week_id']);
            $table->unique(['lecturer_id', 'day_id', 'pair_number_id', 'week_id']);

Can I use conditions in unique arrays? For example, I have 3 types of a week: every, odd and even. If the classroom is occupied at that time on even weeks, it also can be occupied on odd weeks, even at the same time. But it cannot be occupied on every week. For example:

Week: even - Request: odd - Response: true

Week: every - Request: odd or even - Response: false

Week: even - Request: every - Response: false

So, if it's occupied every week, it cannot be occupied on odd or even week. If it's occupied on even week, it can be occupied also on odd week and vice versa. How can I do this via Laravel migrations? Or should I make it manually in my database?

Please sign in or create an account to participate in this conversation.