Filter Laravel users to show only uploaded users
P粉373990857
P粉373990857 2023-09-03 00:26:34
0
1
614

I want to ask, how can I upload a file to the network on user 1, but only attach it to user 1, while currently it is also attached to user 2, my coding is wrong.

Just like the example image below: Picture Network

Take a look at the picture. If it has been uploaded, change its status to Submitted in the "Bahasa Indonesia" SUBJECT. However, for user 2, it has not been uploaded yet, but it has also become Submitted. User 2's status must be Waiting because it has not been uploaded yet. Upload.这里附上我在用户1上上传的mysql: 图像MySqli

AssignmentStudentController

public function DataAssignment(){ $userAssignments = Assignment::join('subjects', 'assignments.id_subject', '=', 'subjects.id_sub') ->join('class_infos', 'subjects.id_class', '=', 'class_infos.id') ->join('class_details', 'class_infos.id', '=', 'class_details.id_class') ->where('class_details.id_user', '=', Auth::user()->id) ->get(); return view('student.assignment.data_assignment', compact('userAssignments')); }

AssignmentStudentController.php

 @forelse($userAssignments as $data)  @empty  @endforelse 
Subject Title Due Date Submission Date Status Score Action
{{$data->subjects->name_subject}} {{$data->title}} {{ date('d M Y - H:m', strtotime($data->due_date)) }} WIB {{ (!empty($data->assignments->id_student)) ? date('d M Y - H:m' ,strtotime($data->assignments->updated_at)):'Not uploaded yet' }} {{ (!empty($data->assignments->id_student)) ? 'Submitted':'Waiting' }} {{ (!empty($data->assignments->id_student)) ? ($data->assignments->score) ? $data->assignments->score :'Process':'0' }} @if(!empty( $data->assignments->id_student)) @else @endif
No Data

P粉373990857
P粉373990857

reply all (1)
P粉797855790

In addition to the assignment method in the model, you can also check by defining a new method:

class Assignment extends Model { public function assignments() { return $this->hasMany(::class, 'id_assignment', 'id_id'); } // new method public function checkStudentAssignmentIsNull() { return $this->hasMany(::class, 'id_assignment', 'id_id') ->where('.id_student', Auth::user()->id) ->first() === null; } }

When using, you can use!$data->checkStudentAssignmentIsNull()instead of!empty($data->assignments->id_student).

I can see your table more clearly now:

// new method public function checkStudentAssignmentIsNull() { return $this->hasMany(AssignmentDetail::class, 'id_assignment', 'id_id') ->where('assignment_details.id_student', Auth::user()->id) ->first() === null; }
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!