我正在练习laravel,在查询关系时感到困惑,我有两个表(用户和角色)
在我的用户模型中,我有
use SoftDeletes;
public function role(){
return $this->hasOne(Role::class);
}
在我的榜样中,我有
use SoftDeletes;
public function user(){
return $this->belongsTo(User::class);
}
这是我的角色表结构
这是我的用户表格结构
如何根据用户表查询角色
答案 0 :(得分:2)
您应该在两个模型中都更新您的关系:
用户模型
use SoftDeletes;
public function role(){
return $this->belongsTo(Role::class,'role_id');
}
角色模型
use SoftDeletes;
public function user(){
return $this->hasOne(User::class);
}
您的查询如下:
更新后的答案
$user = User::find(1);
dd($user->roles());
答案 1 :(得分:1)
您需要更改两个模型中的关系:
select t1.* from (
SELECT column_names
FROM folders
UNION
SELECT column_names
FROM folders_archive) t1
WHERE t1.column_names = what_you_want;
以及其他模型中的
function howManyHundreds(num) {
return num / 100;
return num % 10;
}
console.log(howManyHundreds(894))
console.log(howManyHundreds(323))
,然后将其称为:
use SoftDeletes;
public function role(){
return $this->hasOne(Role::class,'id','role_id');
}
尝试一下,它将起作用...
答案 2 :(得分:1)
应用/模型/用户
public function role()
{
return $this->belongsTo("App\Model\Role", "role_id", "role_id");
}
app / Model / Role
public function user()
{
return $this->hasOne("App\Model\Users");
}
进一步阅读:https://laravel.com/docs/5.6/eloquent-relationships#one-to-one
答案 3 :(得分:0)
如果Laravel认为您的关系Fatal Exception: java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by java.lang.SecurityException: Caller no longer running, last stopped +2s213ms because: timed out while starting
at android.os.Parcel.readException(Parcel.java:1943)
at android.os.Parcel.readException(Parcel.java:1889)
at android.app.job.IJobCallback$Stub$Proxy.dequeueWork(IJobCallback.java:191)
at android.app.job.JobParameters.dequeueWork(JobParameters.java:196)
at android.support.v4.app.JobIntentService$JobServiceEngineImpl.dequeueWork(SourceFile:314)
at android.support.v4.app.JobIntentService.dequeueWork(SourceFile:639)
at android.support.v4.app.JobIntentService$CommandProcessor.doInBackground$10299ca(SourceFile:2389)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
应该有用。
只需在代码中的任意位置尝试User::find(1)->role
。
答案 4 :(得分:0)
用户模型
use SoftDeletes;
public function role(){
return $this->hasOne(Role::class,'role_id','id);
}
角色模型
use SoftDeletes;
public function user(){
return $this->belongsTo(User::class,'id','role_id');
}
像这样更新您的代码,它就可以工作。