Laravel 5.4在另一个laravel模型函数返回的实例上调用模型函数

时间:2017-05-16 14:43:19

标签: php laravel laravel-5 laravel-5.3

我有3个表和相应的模型,如下所示连接:

Telephones.php:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Telephones extends Model {
    protected $table = 'telephones';
    protected $primaryKey = 'telephone_id';
    public function UserTelephone() {
        return $this->hasOne('App\Models\Users','user_id','user_id');
    }
}

Addresses.php:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Addresses extends Model {
    protected $table = 'addresses';
    protected $primaryKey = 'address_id';
    public function UserAddress() {
        return $this ->hasOne('App\Models\Users','address_id','address_id');
    }
}

Users.php:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Users extends Model {
    protected $table = 'users';
    protected $primaryKey = 'user_id';

 }

在我看来,我想将用户的电话与用户的地址联系起来,以便我执行以下操作:

<?php  $user_telephone = $telephone->UserTelephone()->first();
       $address = $user_telephone->UserAddress()->first();
?>

但它失败并出现错误:

BadMethodCallException in Builder.php line xxxx:
Call to undefined method 
Illuminate\Database\Query\Builder::UserAddress()

我有什么想法可以解决它?

4 个答案:

答案 0 :(得分:0)

尝试调用您的模型函数:

    $user_telephone = $telephone->UserTelephone; 
    $address = $user_telephone->UserAddress;

只有在打电话给电话型号

时才会没问题

编辑

您的错误出现在您声明模型关系的位置 改变这个:

 return $this->hasOne('App\Models\Users','user_id','user_id');

到这个

 return $this->hasOne(Users::class,'user_id','user_id');

与您的其他功能相同

*确保您的型号名称是什么..大部分时间都是User

答案 1 :(得分:0)

在您的电话和地址模型上,我会说使用Belongs而不是Has One。

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\Users;

class Addresses extends Model {
    protected $table = 'addresses';
    protected $primaryKey = 'address_id';
    public function UserAddress() {
        return $this->belongsTo(Users::class);
    }
}

class Telephones extends Model {
    protected $table = 'telephones';
    protected $primaryKey = 'telephone_id';
    public function UserTelephone() {
        return $this->belongsTo(Users::class);
    }
}

来自https://laravel.com/docs/5.4/eloquent-relationships#one-to-one 定义关系的反向

您的案例不能处理用户地址的原因是您的用户模型上可能没有address_id字段。

return $this->hasOne('App\Models\Users','id','user_id');

您正在创建一种关系,因此您需要让它知道他们是如何联系的。我建议您阅读更多有关人际关系的信息:https://laravel.com/docs/5.4/eloquent-relationships

答案 2 :(得分:0)

电话型号:

   public function UserTelephone() {
        return $this->belongsTo('App\Models\Users','user_id','user_id');
    }

地址模型:

    public function UserAddress() {
        return $this->belongsTo('App\Models\Users','address_id','address_id');
    }

控制器:

 $telephone =Telephone::find(1);  //get first telephone
 $user_telephone = $telephone->UserTelephone; // get User 
 $address = $user_telephoneObject->UserAddress; //get Address Of the user

答案 3 :(得分:0)

最后我设法解决了它,问题是我试图在错误的对象上调用模型的函数。 我的目标是在用户知道时获取地址。更具体地说,我有以下关系:telephone-&gt; user-&gt; address。正如您可以看到最初发布的代码,我声明了2个函数,UserTelephoneUserAddress。然后我在UserTelephonetelephone objectUserAddress拨打了recipient object。这就是我的代码失败的原因。不得不在Users模型上创建一个新功能,以便在知道用户时获取地址。工作代码发布如下:

<强> Telephones.php:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Telephones extends Model {
    protected $table = 'telephones';
    protected $primaryKey = 'telephone_id';
    public function UserTelephone() {
        return $this->belongsTo('App\Models\Users','user_id','user_id');
    }
}

<强> Addresses.php:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Addresses extends Model {
    protected $table = 'addresses';
    protected $primaryKey = 'address_id';
    public function UserAddress() {
        return $this ->belongsTo('App\Models\Users','address_id','address_id');
    }
}

<强> Users.php:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Users extends Model {
    protected $table = 'users';
    protected $primaryKey = 'user_id';
    public function Address() {
        return $this->hasOne('App\Models\Addresses','address_id','address_id');
    }
}

在我看来:

<?php  $address = $telephone->UserTelephone->Address()->first();?>