如何仅按用户ID获取报告?

时间:2019-09-16 11:05:17

标签: php database laravel

有。

因此,我正在为My reports做一个部分,我需要根据登录用户的ID从数据库中获取报告。我该如何在控制器中做到这一点?我正在使用\App\Reports::get(),但是我需要根据登录用户的ID来获取它,该ID保存在user_id的数据库中。

 public function myReports($page = null)
    {
        if ($user = Sentinel::check())
        {
            // return $user;
            $data = $this->data;
            $id = $user->id;
            $data['title'] = "My Reports";
            $reports =  \App\Reports::get();
            $data['leftside_profile'] = 'my-reports';


            $find_contact[] = $id;
            // DB::enableQueryLog();

            $page = Input::get('page');
            $perPage = 10;
            $offset = ($page * $perPage) - $perPage;

                                                {
                                                    $query->whereIn('user_id',$find_contact)
                                                          ->where('type', '=', 'profile_updates');
                                                });

            return view('profile.my_reports',$data)
            ->with(compact('reports'));
        }
        else{
            return redirect('home');
        }
    }

报告模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Reports extends Model
{
    protected $table = 'reports';

    // public $timestamps = false;

    protected $fillable = [
        'user_id', 'username', 'user_id_posted', 'username_posted', 'news_id','opinion_id','event_id','career_solution_id', 'subject', 'why_reporting','why_reporting_message','additional_message','private'
    ];


    public function career_solutionReport()
    {
        return $this->belongsTo('App\CareerSolution','career_solution_id','id');
    }
     public function eventReport()
    {
        return $this->belongsTo('App\Event','event_id','id');
    }
     public function newsReport()
    {
        return $this->belongsTo('App\News','news_id','id');
    }
     public function opinionReport()
    {
        return $this->belongsTo('App\Opinion','opinion_id','id');
    }

    public function user()
    {
        return $this->belongsTo('App\User','user_id','id');
    }

}

0 个答案:

没有答案
相关问题