过滤 Laravel 用户以仅显示上传的用户
P粉373990857
P粉373990857 2023-09-03 00:26:34
[MySQL讨论组]
<p>我想问,如何在用户1上将文件上传到网络,但仅将其附加到用户1,而目前它也附加到用户2,我的编码错误。 </p><p> 就像下面的示例图片一样: 图片网络</p><p> 看一下图片,如果已经上传了,在“Bahasa Indonesia”SUBJECT 中将其状态改为 Submitted,但是对于用户 2 来说还没有上传,但是也变成了 Submitted,用户 2 的状态肯定是 Waiting因为还没有上传。这里附上我在用户1上上传的mysql: 图像MySqli</p> <p><strong>AssignmentStudentController</strong></p> <pre class="brush:php;toolbar:false;">public function DataAssignment(){ $userAssignments = Assignment::join('subjects', 'assignments.id_subject', '=', 'subjects.id_sub') -&gt;join('class_infos', 'subjects.id_class', '=', 'class_infos.id') -&gt;join('class_details', 'class_infos.id', '=', 'class_details.id_class') -&gt;where('class_details.id_user', '=', Auth::user()-&gt;id) -&gt;get(); return view('student.assignment.data_assignment', compact('userAssignments')); }</pre> <p><strong>AssignmentStudentController.php</strong></p> <pre class="brush:php;toolbar:false;">&lt;table class=&quot;w-full text-sm text-left text-gray-500 dark:text-gray-400&quot;&gt; &lt;thead class=&quot;text-xs text-white uppercase bg-[#464867] dark:bg-[#464867]&quot;&gt; &lt;tr&gt; &lt;th scope=&quot;col&quot; class=&quot;py-3 px-6&quot;&gt; Subject &lt;/th&gt; &lt;th scope=&quot;col&quot; class=&quot;py-3 px-6&quot;&gt; Title &lt;/th&gt; &lt;th scope=&quot;col&quot; class=&quot;py-3 px-6&quot;&gt; Due Date &lt;/th&gt; &lt;th scope=&quot;col&quot; class=&quot;py-3 px-6&quot;&gt; Submission Date &lt;/th&gt; &lt;th scope=&quot;col&quot; class=&quot;py-3 px-6&quot;&gt; Status &lt;/th&gt; &lt;th scope=&quot;col&quot; class=&quot;py-3 px-6&quot;&gt; Score &lt;/th&gt; &lt;th scope=&quot;col&quot; class=&quot;py-3 px-6&quot;&gt; Action &lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; @forelse($userAssignments as $data) &lt;tr class=&quot;bg-white border-b dark:bg-gray-900 dark:border-gray-700&quot;&gt; &lt;th scope=&quot;row&quot; class=&quot;py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white&quot;&gt; {{$data-&gt;subjects-&gt;name_subject}} &lt;/th&gt; &lt;td class=&quot;w-24 px-6 py-4 text-sm font-medium text-gray-900 whitespace-nowrap dark:text-white&quot;&gt; {{$data-&gt;title}} &lt;/td&gt; &lt;td class=&quot;py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white&quot;&gt; {{ date('d M Y - H:m', strtotime($data-&gt;due_date)) }} WIB &lt;/td&gt; &lt;td class=&quot;py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white&quot;&gt; {{ (!empty($data-&gt;assignments-&gt;id_student)) ? date('d M Y - H:m' ,strtotime($data-&gt;assignments-&gt;updated_at)):'Not uploaded yet' }} &lt;/td&gt; &lt;td class=&quot;py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white&quot;&gt; {{ (!empty($data-&gt;assignments-&gt;id_student)) ? 'Submitted':'Waiting' }} &lt;/td&gt; &lt;td class=&quot;py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white&quot;&gt; {{ (!empty($data-&gt;assignments-&gt;id_student)) ? ($data-&gt;assignments-&gt;score) ? $data-&gt;assignments-&gt;score :'Process':'0' }} &lt;/td&gt; &lt;td class=&quot;py-4 px-6 flex font-medium text-gray-900 whitespace-nowrap dark:text-white&quot;&gt; @if(!empty( $data-&gt;assignments-&gt;id_student)) &lt;a href=&quot;{{ (!empty($data-&gt;assignments-&gt;file_assignment))? url('upload/assignment/students/'.$data-&gt;assignments-&gt;file_assignment):''}}&quot; download&gt; &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; class=&quot;h-10 w-10&quot; viewBox=&quot;0 0 20 20&quot; fill=&quot;currentColor&quot;&gt; &lt;path fill-rule=&quot;evenodd&quot; d=&quot;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&quot; clip-rule=&quot;evenodd&quot;&gt;&lt;/path&gt; &lt;/svg&gt; &lt;/a&gt; @else @endif &lt;a type=&quot;button&quot; data-modal-toggle=&quot;{{route('input.assignment', $data-&gt;id_id)}}&quot;&gt; &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; class=&quot;h-10 w-10&quot; viewBox=&quot;0 0 20 20&quot; fill=&quot;currentColor&quot;&gt; &lt;path fill-rule=&quot;evenodd&quot; d=&quot;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&quot; clip-rule=&quot;evenodd&quot;&gt;&lt;/path&gt; &lt;/svg&gt; &lt;/a&gt; &lt;a href=&quot;{{ (!empty($data-&gt;file_asg))? url('upload/assignment/question/'.$data-&gt;file_asg):url('images/no_image.jpg') }}&quot; download&gt; &lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; class=&quot;h-10 w-10&quot; viewBox=&quot;0 0 20 20&quot; fill=&quot;currentColor&quot;&gt; &lt;path fill-rule=&quot;evenodd&quot; d=&quot;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&quot; clip-rule=&quot;evenodd&quot;&gt;&lt;/path&gt; &lt;/svg&gt; &lt;/a&gt; &lt;/td&gt; &lt;/tr&gt; @empty &lt;tr colspan = &quot;7&quot; class=&quot;bg-white border-b dark:bg-gray-900 dark:border-gray-700&quot;&gt; &lt;td class=&quot;py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white&quot;&gt; No Data &lt;/td&gt; &lt;/tr&gt; @endforelse &lt;/tbody&gt; &lt;/table&gt;</pre></p>
P粉373990857
P粉373990857

全部回复(1)
P粉797855790

除了模型中的分配方法之外,您还可以通过定义新方法来进行检查:

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;
    }
}

使用时,可以使用 !$data->checkStudentAssignmentIsNull() 而不是 !empty($data->assignments->id_student)

我现在可以更清楚地看到您的表格:

// new method
public function checkStudentAssignmentIsNull()
{
    return $this->hasMany(AssignmentDetail::class, 'id_assignment', 'id_id')
        ->where('assignment_details.id_student', Auth::user()->id)
        ->first() === null;
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号