Laravel 拉威尔5。错误:我的模型与垃圾和一个关系。

Laravel 拉威尔5。错误:我的模型与垃圾和一个关系。,laravel,Laravel,存在问题,因为已添加transhed,与tablerelation冲突 型号: <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; class Post extends Model { use SoftDeletes; protected $dates = ['deleted_at'];

存在问题,因为已添加transhed,与tablerelation冲突

型号:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Post extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];

    public function category() {
        return $this->hasOne('App\Models\Category', 'id', 'category_id');
    }
}

因为您像惯例一样将外键命名为
category\u id
,所以我认为您应该这样定义雄辩的关系

public function category() {
    return $this->hasOne('App\Models\Category');
}

您应该在
Model.php
文件中看到如何定义一对一关系的hasOne方法。

因为您将外键命名为
category\u id
就像惯例一样,我认为您应该这样定义雄辩的关系

public function category() {
    return $this->hasOne('App\Models\Category');
}

您应该在
Model.php
文件中看到hasOne方法如何定义一对一关系。

请检查您的分类表有哪些字段!
它是否包含“h1”字段,您正在访问“h1”键,但它可能不在表中

请检查您的类别表中有哪些字段! 它是否包含“h1”字段,您正在访问“h1”键,但它可能不在表中

public function category() {
    return $this->hasOne('App\Models\Category');
}