时间戳转换器

未找到 Laravel Eloquent 关系列

日期:2023-05-09     浏览:161    
【中文标题】未找到 Laravel Eloquent 关系列【英文标题】:Laravel Eloquent Relationship column not found 【发布时间】:2014-05-27 11:08:56 【问题描述】:

你能帮我解决这个问题吗?

答案模型

class Answer extends Eloquent 
    protected $primaryKey = 'ID';
    protected $table = 'answers';
    protected $fillable = array('customerID', 'agentID', 'status', 'date', 'urn_code', 'urn_id');

    public function customer()
        return $this->hasOne('Customer');
    

客户模型

class Customer extends Eloquent 
    protected $connection = 'mysql';
    protected $table = 'leads';
    protected $primaryKey = 'cID';

    protected $fillable =  array('cID','title', 'first_name','last_name','address1', 'address2', 'post_code','city','phone_number');

    public function answers() 
        return $this->hasMany('Answer');
    

路线

Route::get('sales', function()
    $sales = Customer::with('answers')->get()->paginate(15);

    foreach($sales as $sale)
    echo $sale->last_name . '<br />'; 

);   

这是我的错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'answers.customer_id'

【问题讨论】:

你能展示你的模型的迁移吗? 【参考方案1】:

这正是错误所说的。在您的 answers 表中,Laravel 会自动查找 customer_id 列,但在这种情况下它不存在。

如果您的客户 ID 列使用不同的名称,您可以将其指定为 hasMany() 方法中的第二个参数:

public function answers() 
    return $this->hasMany('Answer', 'my_column');

此外,正如@razor 所指出的,您可能应该在这里使用belongsTo 关系。

【讨论】:

另外,在Answer 模型中,在customer() 关系中应该是belongsTo()(而不是hasOne())【参考方案2】:

由于您使用的是自定义主键,因此您需要指定本地键和外键。

public function answers() 
    return $this->hasMany('Answer', 'foreign_key', 'local_key');

您可能还必须更新您的 Answer 模型(请检查您是否真的需要 hasOne 或 belongsTo 关系):

public function customer()
    return $this->belongsTo('Customer', 'foreign_key', 'local_key');

您可以找到更多信息here。

【讨论】:

相关文章

Laravel 5.8:未找到列:1054 多对多关系中的未知列错误

{】Laravel5.8:未找到列:1054多对多关系中的未知列错误【英文标题】:Laravel5.8:Columnnotfound:1054UnknowncolumnerrorinManyToManyrelationship【发布时间】:2021-09-2302:24:48【问题描述】:我在用户模型和钱包模型之间存在多对多关系:Wallet.php:pu...}

SQLSTATE [42S22]:未找到列:1054 未知列一对多(反向)关系 laravel

{...SQLSTATE[42S22]:未找到列:1054未知列一对多(反向)关系laravel【英文标题】:SQLSTATE[42S22]:Columnnotfound:1054Unknowncolumnonetomany(inverse)relationlaravel【发布时间】:2017-11-0704:05:00【问题描述】:我有两个型号Tour.php和TourCategory.php:Tour.phppr...}

未找到列:Laravel 5 多对多关系查询中“on 子句”中的 1054 未知列“managers.id”

{】未找到列:Laravel5多对多关系查询中“on子句”中的1054未知列“managers.id”【英文标题】:Columnnotfound:1054Unknowncolumn\'managers.id\'in\'onclause\'inLaravel5Many-to-Manyrelationshipquery【发布时间】:2016-01-1102:43:57【问题描述】:我是laravel的新...}

在 Laravel Eloquent 中使用关系表中的特定列获取表中的特定列

{】在LaravelEloquent中使用关系表中的特定列获取表中的特定列【英文标题】:FetchSpecificColumnsintablewithspecificcolumninrelationshiptableinLaravelEloquent【发布时间】:2021-12-3000:10:23【问题描述】:这里我有两个模型。用户和公司。内部用户模...}

多个 Laravel Eloquent 关系未显示来自所有关系的数据

{】多个LaravelEloquent关系未显示来自所有关系的数据【英文标题】:MultipleLaravelEloquentRelationshipnotshowingdatafromallrelationships【发布时间】:2021-12-0511:49:27【问题描述】:我想从Course表中获取id和courseName,从user表中获取id,name和mob以及...}

Laravel Eloquent - 查询数据透视表

{】LaravelEloquent-查询数据透视表【英文标题】:LaravelEloquent-queryingpivottable【发布时间】:2018-11-1117:33:15【问题描述】:在我的Laravel应用程序中,我有三个数据库表,分别称为用户、项目和角色。它们之间存在m:n关系,所以我还有...}

Laravel Eloquent 关系有很多错误:在 where 上使用时调用未定义的方法

{】LaravelEloquent关系有很多错误:在where上使用时调用未定义的方法【英文标题】:LaravelEloquentrelationshipshasmanyerror:Calltoundefinedmethodwhenusingonwhere【发布时间】:2021-08-2214:24:58【问题描述】:我对laraveleloquent中的hasmany关系有疑问。为...}

Laravel Eloquent 关系二模型

{】LaravelEloquent关系二模型【英文标题】:LaravelEloquentRelationTwoModel【发布时间】:2021-10-0108:13:05【问题描述】:我有两个模型。用户,图像。如何获取所有拥有图像且图像日期介于开始日期和结束日期之间的用户。日期列名称“...}

Laravel 6 Eloquent - 三个表和枢轴之间的关系

{】Laravel6Eloquent-三个表和枢轴之间的关系【英文标题】:Laravel6Eloquent-Relationshipbetweenthreetablesandpivots【发布时间】:2020-05-0609:37:42【问题描述】:我有三个表,我想关联这三个表,但我不知道如何。我有一个用户表和两个帖子和...}

不显示来自 Laravel Eloquent 关系的数据

{】不显示来自LaravelEloquent关系的数据【英文标题】:NotDisplayingdatafromLaravelEloquentRelationship【发布时间】:2021-12-1310:21:01【问题描述】:我想从Course表中选择列id、courseName和userId,从Files表中选择id,FilePath,从user表中选择id,name&a...}

Copyright ©2021 时间戳转换器 小常识 114pp | 陕ICP备18005036号