简体   繁体   English

从参考表中获取记录雄辩的Laravel 5

[英]get record from the reference table Eloquent Laravel 5

First, In my mysql database, I have 2 tables named "users" and "users_details" and they are in relationship 首先,在我的mysql数据库中,我有两个名为“ users”和“ users_details”的表,它们之间存在关系

users table 用户表

id (PK, AI, INT), username (VARCHAR, UNQ), password (VARCHAR), fullname (VARCHAR) id(PK,AI,INT),用户名(VARCHAR,UNQ),密码(VARCHAR),全名(VARCHAR)

and the users_table 和users_table

id (PK, AI, INT), email (VARCHAR), phone (VARCHAR), address (VARCHAR), has_record (VARCHAR), user_id (int, FK = reference table 'users.id') id(PK,AI,INT),电子邮件(VARCHAR),电话(VARCHAR),地址(VARCHAR),has_record(VARCHAR),user_id(int,FK =参考表'users.id')

and my model bindings. 和我的模型绑定。

'User' model “用户”模型

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    use Authenticatable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['username', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function user_details(){
        return $this->hasOne('App\users_details');
    }

}

and the 'users_details' model 和“ users_details”模型

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class users_details extends Model {

    protected $table = "user_details";


       public function ud()
       {
           return $this->belongsTo('App\User');
       }
}

and I know I can just do like 我知道我可以做到

$data = User::find(1)->with('user_details'); //or
$data = User::find(1)->user_details;

but how about I want to get the fullname record from the 'User' table which is the main reference table for the 'users_table' 'user_id' foreign key? 但是我要如何从“用户”表中获取全名记录呢?“用户”表是“ users_table”“ user_id”外键的主要参考表? how to do it? 怎么做? I tried 我试过了

$data = users_details::where('has_record', '=', 'no')->with('ud')->get(); //get all the records where column 'has_record' of 'users_details' is equal to 'no'
$data = $data->ud->fullname; //get the fullname

but unfortunately and sadly not working, any help, ideas, clues, suggestions, recommendations please? 但是不幸的是,不幸的是,它无法正常工作,请提供任何帮助,想法,线索,建议,建议吗?

UPDATE: 更新:

I achieved it using Query builder 我使用查询构建器实现了它

$test = DB::table('users')
                ->join('user_details', 'users.id', '=', 'user_details.user_id')
                ->where('has_record', '=', 'no')
                ->get();

but is there a way I could do it using the power of Eloquent? 但是有办法我可以使用雄辩的力量吗?

You can just do like that. 你可以那样做。

According to your code 根据你的代码

Find one user detail with user eager load and get name. 查找一个具有用户热切负载的用户详细信息并获取名称。

$user_detail = user_details::with('ud')->find(1);
$user_detail->ud->name;

Get all user detail with user eager load and get user name. 获取用户急切加载的所有用户详细信息并获取用户名。

$user_details = user_details::with('ud')->get();

foreach($user_details as $user_detail){
   echo $user_detail->user->name;
}

I have a suggestion for you if you want. 如果您愿意,我有个建议。 Try to follow the singular/plural format which can make the code more readable. 尝试遵循单数/复数格式,这可以使代码更具可读性。

// User Classs.
public function user_detail(){
    return $this->hasOne('App\UserDetail');
}

// UserDetail Class.
public function user(){
    return $this->belongsTo('App\User');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM