【发布时间】:2019-09-16 11:05:17
【问题描述】:
那里。
所以,我正在为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');
}
}
【问题讨论】:
-
获取当前登录用户:
Auth::user(). -
谢谢你的回答,但是我怎样才能只得到
user_idAuth::user()的记录呢? -
向我们展示您的
report模型 -
@Sohel0415 ,现在请检查我的问题。
-
把这个
$reports = \App\Reports::get();改成$reports = \App\Reports::where('user_id', Sentinel::getUser()->id)->get();