0

I want to output user activity history.

Here's observer that i want to output message of user activity to my front end if($drop_student){ Logger::createLog("Student Schedule Removed for ". $student->first_name . " " . $student->last_name . " '" . $schedule->name . "'");

UPDATED

public function created(StudentSchedule $student_schedule)
    {
        $student = $student_schedule->student()->get()->first();
        $schedule = $student_schedule->schedule()->get()->first()->subject->subject;
        Logger::createLog("Student Schedule Added for " . $student->first_name . " " . $student->last_name . " '" . $schedule->name . "'");
    }

    /**
     * Handle the student schedule "updated" event.
     *
     * @param  \App\Models\Student\Schedule  $student_schedule
     * @return void
     */
    public function updated(StudentSchedule $student_schedule)
    {
        $student = $student_schedule->student()->get()->first();
        $schedule = $student_schedule->schedule()->get()->first()->subject->subject;
        $drop_students = $student_schedule->schedule()->where('id')->get();
        foreach ($drop_students as $drop_student) {
            $drop_student = $student_schedule->schedule();
        }
        $drop_student->save(['final_grade' => 'DRP']);
        
        if ($drop_student) {
            Logger::createLog("Student Schedule Removed for " . $student->first_name . " " . $student->last_name . " '" . $schedule->name . "'");
        } else {
            Logger::createLog("Student Schedule Updated for " . $student->first_name . " " . $student->last_name . " '" . $schedule->name . "'");
        }
    }

Here my relationship models student & schedule hasMany student schedule

student schedule model:

class Schedule extends Model
{
    use HasFactory;
    
    protected $table = 'student.schedules';
    protected $fillable = [
        'student_id',
        'schedule_id',
        'removal_grade',
        'final_grade',
        'is_published',
    ];

    public $timestamps = false;

    public function student()
    {
        return $this->belongsTo('App\Models\Student', 'student_id');
    }

    public function schedule()
    {
        return $this->belongsTo('App\Models\Schedule', 'schedule_id');
    }
}

student model:

class Student extends Model
{
    use HasFactory;
    
    const MALE = 'male';
    const FEMALE = 'female';
    protected $table = 'students';
    protected $fillable = [
        'first_name',
        'last_name',
        'middle_name',
        'suffix',
        'email',
        'student_number',
        'sex',
        'birthdate',
        'lrn',
        "profile_picture"
    ];
    protected $appends = ['allow_delete'];

    public function schedules()
    {
        return $this->hasMany('App\Models\Student\Schedule', 'student_id', 'id');
    }

}

schedule model:

class Schedule extends Model
{
    use HasFactory;
    
    const MONDAY = 'M';
    const TUESDAY = 'T';
    const WEDNESDAY = 'W';
    const THURSDAY = 'TH';
    const FRIDAY = 'F';
    const SATURDAY = 'S';
    const SUNDAY = 'SUN';
    protected $table = 'schedules';
    protected $fillable = [
        'start_time',
        'end_time',
        'room_id',
        'subject_id',
        'session_id',
        'user_id',
        'day',
        'section_id',
        'price_per_unit_id',
    ];
    protected $casts = ['day' => 'array'];
    protected $appends = ['allow_delete', 'enrollments_count'];
    
    public function students()
    {
        return $this->hasManyThrough(
            'App\Models\Student',
            'App\Models\Student\Schedule',
            'schedule_id',
            'id',
            'id',
            'student_id',
        );
    }

    public function studentSchedules()
    {
        return $this->hasMany('App\Models\Student\Schedule', 'schedule_id', 'id');
    }

}

Here's api that i use.

Route::post('/drop-schedules', [EnrollmentController::class, 'dropSchedules']);

Here's dropSchedules method in controller.

public function dropSchedules(Request $request)
    {
        StudentSchedule::whereIn('id', $request->student_schedules)->update(['final_grade' => 'DRP']);
        return response()->json([
            'message' => 'Student schedules successfully dropped.'
        ], 200);
    }

but when i fire the drop schedule button. This logic it doesn't run. Only the created method Student Schedule Added for

enter image description here

Kael
  • 161
  • 2
  • 13
  • Calling `update` directly on the builder won't be firing any model events as it is a direct update on the database, not a model instance ... on a side note `->get()->first()` isn't needed, you only need `first()`; if you call `get()->first()` you are getting all of the records for that query then only taking the first one, instead of only querying for the first record (calling `first()` on the builder) – lagbox Aug 08 '22 at 02:33
  • thanks for taking time to answer. I see that's why only ```created method``` on observer was firing not ```update```. Im quite at lost here, – Kael Aug 08 '22 at 02:46
  • you would have to get a collection of the models then spin through that and call update on each one ... also `$drop_student` in your `updated` observer method will always evaluate to `true` as a Collection is an object and objects are truthy `true` – lagbox Aug 08 '22 at 03:01
  • I got basic understanding now. Is this correct approach? ```$drop_student = $student_schedule->schedule()->where('id')->update(['final_grade' => 'DRP']);``` – Kael Aug 08 '22 at 03:26
  • No you can't use `where(..)->update` because that does a mass update. You need to do `where(..)->get()` and then do a `foreach` loop on each result and run the `save([ 'final_grade' => 'DRP' ])` – apokryfos Aug 08 '22 at 04:46
  • Sorry for not getting back quickly. My senior assigned me to different task. I have modified code as suggested. But i feel im missing some pieces or is this the right approach to the problem? – Kael Aug 12 '22 at 03:40

0 Answers0