简体   繁体   English

Laravel One-to-One关系无效

[英]Laravel One-to-One relationship not working

I have 2 database tables => mobile_phones , mobile_users 我有2个数据库表=> mobile_phonesmobile_users

Schema for mobile_phones mobile_phones架构

  • phone_id (primary key , auto_increment) phone_id(主键,auto_increment)

  • phone_name (varchar(150)) phone_name(varchar(150))

  • phone_model (int (11)) phone_model(int(11))

Schema for mobile_users mobile_users架构

  • user_id (primary key , auto_increment) user_id(主键,auto_increment)

  • username (varchar(150)) 用户名(varchar(150))

  • mobile_phone_id (foreign key referencing mobile_phones(phone_id)) mobile_phone_id(引用mobile_phones(phone_id)的外键)

Model class for mobile_phones mobile_phones模型类

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class MobilePhone extends Model
{
    protected $primaryKey = "phone_id";
    public $timestamps = false;
    protected $table = "mobile_phones";
    protected $fillable = array("phone_name","phone_model");

    public function mobileUser()
    {
        return $this->hasOne("MobileUser","mobile_phone_id");
    }

}

Model class for mobile_users mobile_users模型类

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class MobileUser extends Model
{
    protected $primaryKey = "user_id";
    public $timestamps = false;
    protected $table = "mobile_users";
    protected $fillable = array("username","mobile_phone_id");

    public function mobilePhone()
    {
        return $this->belongsTo("MobilePhone","phone_id");
    }


}

I am trying to establish One-to-One relationship between MobileUser and MobilePhone models but it isn't happening. 我正在尝试在MobileUserMobilePhone模型之间建立一对一的关系,但是这种情况没有发生。 Below is my code in Controller's action - 以下是我在Controller的行动中的代码 -

public function query()
    {
       $getUsername = MobilePhone::find(1)->username;        
       echo $getUsername;
    }

The above code gives me NULL when I do a var_dump() I did check similar questions on SO and they used with() (but this isn't necessary). 上面的代码在我执行var_dump()时给了我NULL ,我确实在SO上检查了类似的问题,并将它们with()一起使用(但这不是必需的)。 I am referring Laravel 5.2 docs and they state that we can access relate record in another relation using Eloquent's dynamic properties. 我指的是Laravel 5.2文档,他们说我们可以使用Eloquent的动态属性访问另一个关系中的关系记录。 Link to that docs is here 链接到该文档在这里

Please Help !! 请帮忙 !!

Something like this : 像这样的东西:

public function query()
    {
       $getUsername = MobilePhone::find(1)->mobileUser;        
       echo $getUsername->username;
    }

When doing this: 执行此操作时:

public function query()
    {
       $getUsername = MobilePhone::find(1)->username;        
       echo $getUsername;
    }

You try to access a username property out of a MobilePhone number, which is incorrect. 您尝试从MobilePhone号码访问username属性,这是不正确的。

Try this: 尝试这个:

public function query()
    {
       $getUsername = MobilePhone::find(1)->mobileUser->username;        
       echo $getUsername;
    }

Also I encourage you to use the with statement as it will preload all dependencies needed (means 1 single query) instead of eager loading it (many queries) 另外,我鼓励你使用with语句,因为它会预加载所需的所有依赖项(意味着单个查询),而不是急于加载它(许多查询)

You need to call the function of your model so do the following 您需要调用模型的功能,因此请执行以下操作

public function query()
{
       $getUsername = MobilePhone::find(1)->mobileusers;        
       echo $getUsername->username;
}

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

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