我有一個錯誤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
}
}
均返回「試圖獲得非對象的屬性」 – emeliku
@emeliku你需要改變的關係名稱改爲'user' –
改變什麼? – emeliku