Home » Php » php – Laravel 5.8 check record in the database if exists not working

php – Laravel 5.8 check record in the database if exists not working

Posted by: admin February 25, 2020 Leave a comment

Questions:

I got a schedules table and i want it to have any duplicates. If the user puts same datas in the form that has already in the database it will not go to dd('exists') does anyone know what seems to be the problem?

Here is my update

$schedule = Schedule::findOrFail($id);

$validate = Schedule::where('subject_code_id',$request->subject)
                    ->where('teacher_id',$request->teacher)
                    ->where('room_id',$request->room)
                    ->where('start_time',$request->start_time)
                    ->where('end_time',$request->end_time)
                    ->where('school_year',$request->schoolyr)
                    ->where('day',$request->days)
                    ->where('term',$request->term)
                    ->where('semester',$request->sem)
                    ->count();

if($validate == 1){
          dd('exist');
} else{
            $schedule->update($request->all());
}

tried
dd($request->all());
I got

array:11 [
  "id" => 9
  "term" => "2"
  "semester" => "2"
  "start_time" => "10:30 PM"
  "end_time" => "12:30 AM"
  "scid" => 10
  "teacher_id" => 5
  "room_id" => 6
  "subject_code_id" => 9
  "day" => "SAT"
  "school_year" => "2020"
]
How to&Answers:

Instead of using the count method to determine if any records exist that match your query’s constraints, you may use the exists and doesntExist methods:

$schedule = Schedule::findOrFail($id);

$validate = Schedule::where('subject_code_id',$request->subject)
                ->where('teacher_id',$request->teacher)
                ->where('room_id',$request->room)
                ->where('start_time',$request->start_time)
                ->where('end_time',$request->end_time)
                ->where('school_year',$request->schoolyr)
                ->where('day',$request->days)
                ->where('term',$request->term)
                ->where('semester',$request->sem)
                ->exists();

if($validate){
      dd('exist');
} else{
        $schedule->update($request->all());
}

Please visit to see details – aggregates

Answer:

you can easily do that with laravel unique validation by using Rule class as below

$this->validate($request, [
    'subject_code_id' => [
        Rule::unique('schedules')->where(function ($query) use($request) {
            return $query->where('teacher_id', $request->teacher); //you can add all conditions here
        }),
    ],
]);

for more see unique validation rules here

Answer:

use first() method instead of count()

$schedule = Schedule::findOrFail($id);

$validate = Schedule::where('subject_code_id',$request->subject)
                    ->where('teacher_id',$request->teacher)
                    ->where('room_id',$request->room)
                    ->where('start_time',$request->start_time)
                    ->where('end_time',$request->end_time)
                    ->where('school_year',$request->schoolyr)
                    ->where('day',$request->days)
                    ->where('term',$request->term)
                    ->where('semester',$request->sem)
                    ->first();

if($validate){
          dd('exist');
} else{
            $schedule->update($request->all());
}

Answer:

use exists and print msg

$schedule = Schedule::findOrFail($id);

$validate = Schedule::where('subject_code_id',$request->subject)
                    ->where('teacher_id',$request->teacher)
                    ->where('room_id',$request->room)
                    ->where('start_time',$request->start_time)
                    ->where('end_time',$request->end_time)
                    ->where('school_year',$request->schoolyr)
                    ->where('day',$request->days)
                    ->where('term',$request->term)
                    ->where('semester',$request->sem)
                    ->exists();

//check $validate

if($validate){
       echo "data is already in databse";
} else{
       $schedule->update($request->all());
}