Home > Software engineering >  How to see only uploaded users in laravel?
How to see only uploaded users in laravel?

Time:12-06

I want to ask, how do I upload a file to the web on user 1 but only attach it to user 1, whereas currently it is also attached to user 2, my coding is wrong.
like the example image below: Images Web
If you look at the picture, if it has been uploaded, change it to Submitted status in the "Bahasa Indonesia" SUBJECT, but for user 2 it hasn't been uploaded, but it has also become Submitted, user 2's status must be Waiting because it hasn't been uploaded yet. And here is attached my mysql uploaded on user 1: Images 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

<table >
    <thead >
    <tr>
        <th scope="col" >
            Subject
        </th>
        <th scope="col" >
            Title
        </th>
        <th scope="col" >
            Due Date
        </th>
        <th scope="col" >
            Submission Date
        </th>
        <th scope="col" >
            Status
        </th>
        <th scope="col" >
            Score
        </th>
        <th scope="col" >
            Action
        </th>
    </tr>
    </thead>
    <tbody>
    @forelse($userAssignments as $data)
        <tr >

            <th scope="row" >
                {{$data->subjects->name_subject}}
            </th>

            <td >
                {{$data->title}}
            </td>
            <td >
                {{ date('d M Y - H:m', strtotime($data->due_date)) }} WIB
            </td>
            <td >
                {{ (!empty($data->assignments->id_student)) ? date('d M Y - H:m' ,strtotime($data->assignments->updated_at)):'Not uploaded yet' }}
            </td>
            <td >
                {{ (!empty($data->assignments->id_student)) ? 'Submitted':'Waiting' }}
            </td>
            <td >
                {{ (!empty($data->assignments->id_student)) ? ($data->assignments->score) ? $data->assignments->score :'Process':'0' }}
            </td>
            <td >
                @if(!empty( $data->assignments->id_student))
                    <a href="{{ (!empty($data->assignments->file_assignment))? url('upload/assignment/students/'.$data->assignments->file_assignment):''}}" download>
                        <svg xmlns="http://www.w3.org/2000/svg"  viewBox="0 0 20 20" fill="currentColor">
                            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm1-12a1 1 0 10-2 0v4a1 1 0 00.293.707l2.828 2.829a1 1 0 101.415-1.415L11 9.586V6z" clip-rule="evenodd"></path>
                        </svg>
                    </a>
                @else

                @endif

                <a type="button" data-modal-toggle="{{route('input.assignment', $data->id_id)}}">
                    <svg xmlns="http://www.w3.org/2000/svg"  viewBox="0 0 20 20" fill="currentColor">
                        <path fill-rule="evenodd" d="M6 2a2 2 0 00-2 2v12a2 2 0 002 2h8a2 2 0 002-2V7.414A2 2 0 0015.414 6L12 2.586A2 2 0 0010.586 2H6zm5 6a1 1 0 10-2 0v2H7a1 1 0 100 2h2v2a1 1 0 102 0v-2h2a1 1 0 100-2h-2V8z" clip-rule="evenodd"></path>
                    </svg>
                </a>

                <a href="{{ (!empty($data->file_asg))? url('upload/assignment/question/'.$data->file_asg):url('images/no_image.jpg') }}" download>
                    <svg xmlns="http://www.w3.org/2000/svg"  viewBox="0 0 20 20" fill="currentColor">
                        <path fill-rule="evenodd" d="M6 2a2 2 0 00-2 2v12a2 2 0 002 2h8a2 2 0 002-2V7.414A2 2 0 0015.414 6L12 2.586A2 2 0 0010.586 2H6zm5 6a1 1 0 10-2 0v3.586l-1.293-1.293a1 1 0 10-1.414 1.414l3 3a1 1 0 001.414 0l3-3a1 1 0 00-1.414-1.414L11 11.586V8z" clip-rule="evenodd"></path>
                    </svg>
                </a>
            </td>
        </tr>
    @empty
        <tr colspan = "7" >
            <td >
                No Data
            </td>
        </tr>
    @endforelse
    </tbody>
</table>

CodePudding user response:

You can check by defining a new method in addition to the assignments method in your model:

class Assignment extends Model
{
    public function assignments()
    {
        return $this->hasMany(<YOUR_ASSIGNMENTS_TABLE_NAME>::class, 'id_assignment', 'id_id');
    }

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

And when using, you can do !$data->checkStudentAssignmentIsNull() instead of !empty($data->assignments->id_student).

I see your tables 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;
}
  • Related