【问题标题】:I want to solve this without changing a column name in the database table. Is there any way to solve this?我想在不更改数据库表中的列名的情况下解决这个问题。有没有办法解决这个问题?
【发布时间】:2021-02-17 19:10:54
【问题描述】:

SQLSTATE[42S22]: 找不到列: 1054 未知列 'notices.notice_category_id' in 'where 子句' (SQL: select * from notices where notices.notice_category_id = 1 and notices.@ 987654325@ 不为空且notices.deleted_at 为空)

This is notices table migration
 public function up()
    {
        Schema::create('notices', function (Blueprint $table) {
            $table->id();          
            $table->text('subject');
            $table->integer('category_id');           
            $table->date('date');
            $table->timestamps();
        });
    }
notice category table

 public function up()
    {
        Schema::create('notice_categories', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }
model for noitceCategory
  public function notices()
    {
        return $this->hasMany(Notice::class);
    }
notice model
 public function noticeCategory()
    {
        return $this->belongsTo(NoticeCategory::class);
    }

我正在尝试使用以下代码进行计数

$noticecategory->notices->count();

``
tell me if any other information are required

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    NoticeCategory模型中定义通知表的外键

    public function notices()
    {
        return $this->hasMany(Notice::class,'category_id');
    }
    

    如果您不提供外键参数,则 laravel 假定 model_name_id 将是该关系的外键

    【讨论】:

    • 非常感谢您的支持,您救了我,我需要进一步了解laravel。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-21
    • 2010-10-15
    • 2022-09-27
    • 2023-02-25
    • 2020-02-09
    • 1970-01-01
    • 2011-12-04
    相关资源
    最近更新 更多