iT邦幫忙

0

laravel class not found 的問題

各位大神好:
小弟有問題想要請教

laravel 版本為 5.8.33

我的model長這樣

Accounts.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Accounts extends Model
{
    public $timestamps = false;
    protected $table ="accounts";

    public function auth(){
        return $this->hasOne('App/AccountAuth','accountsn','sn');
    }
}

AccountAuth.php

namespace App;

use Illuminate\Database\Eloquent\Model;

class AccountAuth extends Model
{
    public $timestamps = false;
    protected $table ="accountauth";

    public function account(){
        return $this->belongsTo('App/Accounts','accountsn','sn');
    }
}

執行到 auth() 這個method的時候會噴 Class 'App/AccountAuth' not found的Error
也有執行過 composer dump-auto -o
不過還是一樣的結果 想請大神幫幫忙 這要怎麼解

firecold iT邦新手 1 級 ‧ 2019-08-22 10:21:23 檢舉
你看很久那代表沒發現回答不一樣的話,代表不夠細心
你寫App/Accounts
我寫App\Accounts
真的耶... 我是找錯字找很久都找不到的那種...
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
firecold
iT邦新手 1 級 ‧ 2019-08-21 18:36:08
最佳解答

try it
路徑這樣寫

public function auth(){
        return $this->hasOne('App\AccountAuth','accountsn','sn');
}
看更多先前的回應...收起先前的回應...

大大這段好像跟我原本寫的一樣耶... 我看好久XD

大大 我試過你的方法把路徑改成斜線出現一樣的Error....
但是再改回反斜線就可以運作了 這是什麼魔術 = ="

firecold iT邦新手 1 級 ‧ 2019-08-22 12:01:53 檢舉

反斜應該是\喔
你檢查看看檔案是不是存檔之類的
正常來說用\才是正確的
或者你可以試試看phpstorm
會自動幫你檢查有沒有引用到檔案

小弟依照大大的提示改成反斜線之前有這樣改過

use App\AccountAuth;

return $this->hasOne(AccountAuth::class,'accountsn','sn');
 

雖然這樣不會噴error 但是拿到的實例卻沒有欄位的值...
後來改成大大的方式不成功之後再改回原本的就可以了 不知道這樣有沒有影響??

firecold iT邦新手 1 級 ‧ 2019-08-22 14:09:47 檢舉

其實你這樣也可以
會突然可以應該是上方編輯器幫你增加use了
沒有拿到值你要檢查一下外鍵跟local_key的關係
以及select的時候要把相關的column都抓出來

好的 大大我再研究一下 感恩

firecold iT邦新手 1 級 ‧ 2019-08-22 14:14:54 檢舉

你可以把你select的code拿出來討論看看

0
小魚
iT邦大師 1 級 ‧ 2019-08-21 17:44:00

你的位置放對嗎?
而且一般不會直接放在App底下.

看更多先前的回應...收起先前的回應...

應該是對的 因為model是 用 make:model 生出來的沒有動過位置

namespace跟use也是model生出來的時候自動寫上去的

小魚 iT邦大師 1 級 ‧ 2019-08-21 20:13:46 檢舉

那試試這樣
1.跟下面大大寫的一樣,反過來試試看
2.Model中加入相關的欄位, 看起來目前沒有欄位.

小魚大大 欄位確定是有的..QQ
Accounts
https://ithelp.ithome.com.tw/upload/images/20190822/20111526QeARYJxqRs.png
AccountAuth
https://ithelp.ithome.com.tw/upload/images/20190822/20111526cP29fo3212.png

我要發表回答

立即登入回答