2017-04-05 80 views
1

我有一個錯誤whith關係,我不明白爲什麼; 你能幫我解決這個問題嗎?由於LARAVEL - 錯誤關係(查看)

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$name (View: C:\wamp64\www\xxxxxx\resources\views\front\sujet\show.blade.php) 

代碼視圖:

<div>{{$poste->users()->name}}</div> 

代碼示範:

namespace App\Models; 

use Illuminate\Database\Eloquent\Model; 

class Poste extends Model 
{ 
    // 
    public function sujets() 
    { 
     return $this->belongsTo('App\Models\Sujet'); 
    } 

    public function users() 
    { 
     return $this->belongsTo('App\Models\User'); 
    } 
} 

控制器:

$unSujet=Sujet::find($id); 
     $lesPostes = Poste::orderBy('id')->where('sujet_id', $id)->paginate(15); 
     return view('front/sujet/show', compact ('unSujet', 'lesPostes')); 

和DD($ poste->用戶( ))return:(直接在$ fore的foreach視圖中進行測試t)

BelongsTo {#268 ▼ 
    #foreignKey: "users_id" 
    #otherKey: "id" 
    #relation: "users" 
    #query: Builder {#267 ▶} 
    #parent: Poste {#261 ▼ 
    #connection: null 
    #table: null 
    #primaryKey: "id" 
    #keyType: "int" 
    #perPage: 15 
    +incrementing: true 
    +timestamps: true 
    #attributes: array:7 [▼ 
     "id" => 1 
     "description" => """ 
     Salut a TOUS Cest m on 1er sujet et mon 1er poste. \r\n 
     \r\n 
     mais ya un gros bug, c'est que ca écrit en majuscule tout seul ou alors c'est seulement pendant l'edition de ce message, mais c'est étrange et j'aimerais pouvoir écrire normalement :) !\r\n 
     \r\n 
     Merci,\r\n 
     coardialement le bg. 
     """ 
     "created_at" => "2017-03-31 11:14:48" 
     "updated_at" => "2017-03-31 11:14:48" 
     "signale" => 0 
     "sujet_id" => 9 
     "user_id" => 2 
    ] 
    #original: array:7 [▶] 
    #relations: [] 
    #hidden: [] 
    #visible: [] 
    #appends: [] 
    #fillable: [] 
    #guarded: array:1 [▶] 
    #dates: [] 
    #dateFormat: null 
    #casts: [] 
    #touches: [] 
    #observables: [] 
    #with: [] 
    +exists: true 
    +wasRecentlyCreated: false 
    } 
    #related: User {#266 ▼ 
    #connection: null 
    #table: null 
    #primaryKey: "id" 
    #keyType: "int" 
    #perPage: 15 
    +incrementing: true 
    +timestamps: true 
    #attributes: [] 
    #original: [] 
    #relations: [] 
    #hidden: [] 
    #visible: [] 
    #appends: [] 
    #fillable: [] 
    #guarded: array:1 [▶] 
    #dates: [] 
    #dateFormat: null 
    #casts: [] 
    #touches: [] 
    #observables: [] 
    #with: [] 
    +exists: false 
    +wasRecentlyCreated: false 
    } 
} 

回答

0

更改關係名稱爲user。 Laravel使用關係名稱來生成外鍵,通過它可以搜索相關數據。

然後你就可以通過添加first()方法獲取的對象:

{{ $poste->user()->first()->name }} 

或者,你可以這樣做:

{{ $poste->user->name }} 
+0

均返回「試圖獲得非對象的屬性」 – emeliku

+0

@emeliku你需要改變的關係名稱改爲'user' –

+0

改變什麼? – emeliku

0

,你能否告訴一下輸出由{{ $ poste->用戶}}

從你的輸出

#related: User {#266 ▼ 
#connection: null 
#table: null 
#primaryKey: "id" 
#keyType: "int" 
#perPage: 15 
+incrementing: true 
+timestamps: true 
#attributes: [] 

它顯示關係找不到相關的用戶對象。這

{{ $poste->user()->first() }} 

打印結果。如果這讓比你需要將確認要麼郵政不屬於有效USER_ID空數組作爲輸出

+0

我編輯了第一篇文章 – emeliku

+0

好的,所以我們可以看到在輸出中沒有相關的用戶對象被關係找到 – mchampaneri

0

你的模型的外觀,是不相關的用戶 在型號帖子:

public function user() 
{ 
    return $this->belongsTo('App\Models\User', 'user_id'); 
} 

在模型用戶

public function postes() 
{ 
    return $this->hasMany('App\Models\Poste', 'user_id'); 
} 

您可以訪問{{$ poste->用戶>名稱}}

好運