【问题标题】:Laravel 5.3 Eloquent & RelationshipLaravel 5.3 雄辩与关系
【发布时间】:2017-05-19 10:15:58
【问题描述】:

我有一个记录模型和状态模型。每条记录的状态可以是“已批准”、“未批准”或“处理中”,它们存储在通过记录模型中的status_id连接的状态模型中。 这是我的模特和他们的关系。

Records(id, user_id, status_id, note, date, timestamp)

function status()
{
return $this->hasOne('App\Status');
}

Statuses(id, name)

function record()
{
return $this->belongsToMany('App\Record');

}

我想在我的视图 $records->status->name 上这样回调。但它给出了错误

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Statuses.record_id' in 'where clause' (SQL: select * from `statuses` where `statuses`.`record_id` = 3 and `statuses`.`record_id` is not null limit 1) 

我尝试播放关系和回调,但它不起作用。有可能这样做吗?或者更好的是,我只是将状态直接保存在记录表上,而不使用状态记录。谢谢。

【问题讨论】:

  • 你能告诉我们你的相关表名是什么吗?您是否还在迁移中设置了外键?
  • 你能把你的迁移代码也放上去吗!

标签: php laravel eloquent relationship laravel-5.3


【解决方案1】:

你的关系颠倒了。您可以尝试以下方法吗:

class Record extends Model
{
    public function status()
    {
        return $this->belongsTo('App\Status');
    }
}

class Status extends Model
{
    public function records()
    {
        return $this->hasMany('App\Record');
    }
}

【讨论】:

    【解决方案2】:

    我认为您要建立“一对多”的关系,而不是“多对多”的关系。

    请尝试以下方法:

    Records(id, user_id, status_id, note, date, timestamp)
    
    function status()
    {
    return $this->belongsTo('App\Status');
    }
    
    Statuses(id, name)
    
    function record()
    {
    return $this->hasMany('App\Record');
    }
    

    【讨论】:

      【解决方案3】:

      Records 上定义hasOne 关系

      Records(id, user_id, status_id, note, date, timestamp)
      
      function status(){
         return $this->hasOne('App\Status','id','status_id');
      }
      

      和 hasMany 的状态关系

      Statuses(id, name)
      
      function record(){
         return $this->hasMany('App\Record','status_id','id');
      }
      

      【讨论】:

        【解决方案4】:
         SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Statuses.record_id' in 'where clause' (SQL: select * from `statuses` where `statuses`.`record_id` = 3 and `statuses`.`record_id` is not null limit 1)
        

        根据错误!它说您的状态表中没有 records_id。所以添加它。

        为此,您应该在状态迁移中有一个名为 record_id 的无符号整数。

         $table->unsignedInteger('record_id')->references('id')->on('records');    //This will work as the foreign key to find the respective Status.
        

        编辑迁移文件后,您必须回滚迁移。 在项目目录的终端中,

        php artisan migrate:rollback
        

        如果你也可以添加迁移,它会更容易指出。

        【讨论】:

          【解决方案5】:
          model:Record    
          Records(id,user_id,status_id,note)
          function status(){
          return $this->belongsTo('App\Status','id);
          }
          
          model:Status
          Status(id,name)
          function record(){
          return $this->hasmany('App\Record','id','status_id');
          }
          
           Try this relation it will work
          

          【讨论】:

          • ErrorException in 0abded5d613c35e174212ce480a87b7a6a95419a.php line 200: Trying to get property of non-object (View: /Applications/MAMP/htdocs/test/resources/views/records/timeline.blade.php) 感谢您的回复。上面显示错误。但是当我用谷歌搜索更多我需要这样做时它会起作用 {{@$records->status->name}} 或 {{ $yourVar['value'] }} 你知道为什么吗?谢谢。
          猜你喜欢
          • 1970-01-01
          • 2017-10-12
          • 1970-01-01
          • 2017-04-14
          • 1970-01-01
          • 2015-03-14
          • 2018-11-28
          • 2015-01-11
          • 2021-06-03
          相关资源
          最近更新 更多