[英]Problem with Laravel Eloquent - relation not working
I'am beginner in Laravel.我是 Laravel 的初学者。 I have project in Laravel 5.8.
我在 Laravel 5.8 中有项目。
I have User
model:我有
User
model:
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
use psCMS\Presenters\UserPresenter;
use scopeActiveTrait;
public static $roles = [];
public $dates = ['last_activity'];
// ...
public function scopeHistory()
{
return $this->hasMany('App\UserLoginHistory');
}
// ...
}
and UserLoginHistory
:和
UserLoginHistory
:
class UserLoginHistory extends Model
{
protected $quarded = ['id'];
public $timestamps = false;
protected $fillable = ['user_id', 'date_time', 'ip'];
public function user()
{
return $this->belongsTo('App\User');
}
}
I want show user login history by this code:我想通过这段代码显示用户登录历史:
User::history()->where('id', $idAdmin)->orderBy('id', 'desc')->paginate(25);
but it's not working.但它不起作用。
This function not working - I haven't got results.这个 function 不工作 - 我没有得到结果。
How can I fixed it?我该如何解决?
First of all, you are defining your relationship as a scope (prefixing the relationship with the scope
keyword).首先,您将您的关系定义为 scope(使用
scope
关键字作为关系前缀)。 Try updating your model relationship to this:尝试将您的 model 关系更新为:
public function history()
{
return $this->hasMany('App\UserLoginHistory');
}
Then, given your query, it seems that you want to get all the UserLoginHistory
records for a given User
.然后,根据您的查询,您似乎想要获取给定
User
的所有UserLoginHistory
记录。 You could accomplish this in two ways (at least).您可以通过两种方式(至少)完成此操作。
From the UserLoginHistory
model itself, constraining the query by the foreign key value:从
UserLoginHistory
model 本身,通过外键值约束查询:
$userId = auth()->id(); // get the user ID here.
$results = UserLoginHistory::where('user_id', $userId)->paginate(15);
// ^^^^^^^ your FK column name
From the User
model using your defined relationship:从
User
model 使用您定义的关系:
$userId = auth()->id(); // get the user ID here.
$results = User::find($userId)->history;
The downside of the second approach is that you'll need to paginate the results manually.第二种方法的缺点是您需要手动对结果进行分页。
in your User model you should define your relation by this way:在您的用户 model 中,您应该通过这种方式定义您的关系:
public function history()
{
return $this->hasMany('App\UserLoginHistory');
}
then if you would like to select with history model you can do that with WhereHas() method:那么如果你想 select 和历史 model 你可以用WhereHas()方法做到这一点:
User::whereHas(['history'=>function($q) use ($idAdmin) {
$q->where('id',$idAdmin)
}])->orderBy('id', 'desc')->paginate(25);
You must be do this changes你必须做这个改变
public function history()
{
return $this->hasMany('App\UserLoginHistory');
}
usage用法
$user = User::find($idAdmin);
$userHistories = $user->history()->latest()->paginate(25);
or get user with all history或获取所有历史记录的用户
User::with('history')->find($idAdmin);
// Post model
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
}
// Category model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function posts()
{
return $this->belongsToMany('App\Post')->withTimestamps();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.