鍍金池/ 問答/PHP/ laravel 關(guān)聯(lián)模型報(bào)錯(cuò)!failed to open stream

laravel 關(guān)聯(lián)模型報(bào)錯(cuò)!failed to open stream

數(shù)據(jù)庫


用戶表:

  • id
  • name
  • password
  • email

客訴表:

  • id
  • title
  • content
  • uid *關(guān)聯(lián)user表id*

客訴日志表:

  • id
  • text
  • cc_id *關(guān)聯(lián)客訴id*

Rma 模型

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Rma extends Model
{
    /**
     * Rename table name.
     * @var string
     */
    protected $table = 'oacustomercomplaint';

    /**
     * The attributes that are mass assignable.
     * @var array
     */
    protected $fillable = [];

    /**
     * 關(guān)聯(lián)rma日志記錄
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function logs()
    {
        return $this->hasMany('App\Models\Rmalog', 'cc_id');
    }

    /**
     * 關(guān)聯(lián)user表
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user()
    {
        return $this->belongsTo('App\User', 'uid');
    }
}

Rma 控制器

public function getRmaList(){
    $rmas = Rma::with('user')->orderBy('cc_time', 'desc')->get();
    return $rmas;
}

User model我是獨(dú)立放在app目錄下面的
其它的模型都在models目錄下

clipboard.png

User 模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Rename table name.
     * @var string
     */
    protected $table = 'user';

    /**
     * All fields can be written.
     * @var array
     */
    protected $guarded = [];

    /**
     * Hidden fields.
     * @var array
     */
    protected $hidden = [
        'password'
    ];
}

當(dāng)我試圖關(guān)聯(lián)user模型獲取用戶數(shù)據(jù)時(shí)出現(xiàn)了以下錯(cuò)誤:

(1/1) ErrorException
include(/Library/WebServer/Documents/LaravelOA): failed to open stream: Undefined error: 0

clipboard.png

我以為是權(quán)限問題,我試圖將項(xiàng)目目錄的權(quán)限改為777,似乎也沒用。
可是我關(guān)聯(lián)logs日志表的時(shí)候獲取的數(shù)據(jù)又是正常的,求大佬解答

回答
編輯回答
避風(fēng)港

你的user表的命名空間寫錯(cuò)了吧你寫的是

'App\User'

我想應(yīng)該是

'App\Models\User'
2018年3月7日 22:24
編輯回答
冷眸

可以這樣寫:

return $this->belongsTo(User::class, 'uid');

User model 通過命名空間去加載

2018年1月10日 02:38
編輯回答
愛是癌

樓上正解,這句代碼有問題,找不到UserModel。
return $this->belongsTo('App\Model\User', 'uid');

2017年8月18日 02:47
編輯回答
浪蕩不羈
php artisan clear-compiled
php artisan optimize

試試

2018年1月10日 20:56