我無法在資料庫的組態檔頁面中顯示用戶組態檔資訊,這給了我屬性 [名稱] 在此集合實體上不存在。(查看:C:\xampp\htdocs\project_one\resources\views\profile.blade.php)
我在下面附上了我的代碼
路線
Route::get('/profile', function() {
return view('profile', [
"title" => "Profile",
"profile" => Profile::all(),
"user" => User::all()
]);
});
組態檔表
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->id();
$table->string('name')->nullable();
$table->string('email')->unique();
$table->string('address')->nullable();
$table->string('phone')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->timestamps();
});
}
輪廓模型
class Profile extends Model
{
use HasFactory;
public function user(){
return $this->belongTo(User::class);
}
}
用戶模型中的功能
public function profile(){
return $this->hasOne(Profile::class);
}
刀片模板
<div class="form-group mb-3">
<label for="floatingName">Name</label>
<p>{{ $user->name }}</p>
</div>
uj5u.com熱心網友回復:
您的用戶查詢“user” => User::all() 您傳遞到個人資料頁面是您資料庫中的全部用戶,這是您無法獲得特定用戶名的許多用戶的集合.... ..如果你想要當前登錄用戶的名字,你必須使用
<div class="form-group mb-3">
<label for="floatingName">Name</label>
<p>{{ auth()->user()->name }}</p>
</div>
無需從您的路線傳遞用戶集合。
或者您也可以通過您的路線傳遞用戶登錄用戶
Route::get('/profile', function() {
return view('profile', [
"title" => "Profile",
"profile" => Profile::all(),
"user" => auth()->user(),
]);
});
在你看來
<div class="form-group mb-3">
<label for="floatingName">Name</label>
<p>{{ $user->name }}</p>
</div>
uj5u.com熱心網友回復:
Route::get('/profile', function() {
return view('profile', [
"title" => "Profile",
"profile" => Profile::all(),
"user" => User::all() // HERE IS THE PROBLEM
]);
});
User:all()回傳資料庫中所有用戶的集合,對于您在會話中獲取特定用戶的情況,請使用auth()->user()
您的路線應如下所示
Route::get('/profile', function() {
return view('profile', [
"title" => "Profile",
"profile" => Profile::all(),
"user" => auth()->user(), // HERE IS THE SOLUTION
]);
});
然后您可以在刀片中使用變數$user并獲取特定用戶的名稱
<div class="form-group mb-3">
<label for="floatingName">Name</label>
<p>{{ $user->name }}</p>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/408110.html
標籤:
下一篇:Laravel中的模塊是什么
