[英]Laravel - Weird behaviour with query
因此,我使用一種簡單的方法來顯示編輯表單,並使用了從Laravel CRUD各種教程中看到的信息。
/**
* Show the form for editing the specified resource.
*
* @param \App\User $users
* @return \Illuminate\Http\Response
*/
public function edit(User $user)
{
//Get user with specified username
$user = User::findOrFail($user)->get();
return view('users.edit', compact('user')); //pass user and roles data to view
}
如您所見,此方法再簡單不過了。
此方法的路由如下:
/user/{user}/edit
其中{user}
是綁定到路由的模型的主鍵,在我的情況下,它恰好是$username
。
因此,如果我要輸入/user/stevew/edit
,則希望查詢如下:
select * from
用戶where username = ? and
用戶where username = ? and
select * from
where username = ? and
用戶.
delete_at is null limit 1
問題是:查詢沒有返回我期望的用戶,而是返回了另一個用戶edwardl的詳細信息,而不是返回stevew的詳細信息。
這是用戶表遷移
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username');
$table->string('displayName');
$table->string('email')->unique();
$table->string('role')->nullable();
$table->string('department')->nullable();
$table->string('location')->nullable();
$table->string('directDialIn')->nullable();
$table->string('mobileNumber')->nullable();
$table->string('managedByUsername')->nullable();
$table->string('managedByDisplayName')->nullable();
$table->timestamps();
$table->softDeletes();
});
這是執行的查詢的轉儲
QueryExecuted {#736 ▼
+sql: "select * from `users` where `username` = ? and `users`.`deleted_at` is null limit 1"
+bindings: array:1 [▼
0 => "jesseo"
]
+time: 6.25
+connection: MySqlConnection {#306 ▶}
+connectionName: "mysql"
}
QueryExecuted {#7458 ▼
+sql: "select * from `users` where `username` = ? and `users`.`deleted_at` is null limit 1"
+bindings: array:1 [▼
0 => "stevew"
]
+time: 0.62
+connection: MySqlConnection {#306 ▶}
+connectionName: "mysql"
}
QueryExecuted {#14214 ▼
+sql: "select `roles`.*, `model_has_roles`.`model_id` as `pivot_model_id`, `model_has_roles`.`role_id` as `pivot_role_id`, `model_has_roles`.`model_type` as `pivot_model_type` from `roles` inner join `model_has_roles` on `roles`.`id` = `model_has_roles`.`role_id` where `model_has_roles`.`model_id` = ? and `model_has_roles`.`model_type` = ? ◀"
+bindings: array:2 [▼
0 => "jesseo"
1 => "App\User"
]
+time: 0.84
+connection: MySqlConnection {#306 ▶}
+connectionName: "mysql"
}
QueryExecuted {#21003 ▼
+sql: "select * from `users` where `users`.`username` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and `users`.`deleted_at` is null"
+bindings: array:14 [▼
0 => 102
1 => "stevew"
2 => "Steve Williams"
3 => "Steve.Williams@newable.co.uk"
4 => "Web Manager, Digital"
5 => "Digital"
6 => "Head Office"
7 => "+44 (0)20 7940 1598 "
8 => ""
9 => "elouttit"
10 => "Edward Louttit"
11 => null
12 => null
13 => null
]
+time: 0.76
+connection: MySqlConnection {#306 ▶}
+connectionName: "mysql"
}
我知道第一個查詢是已登錄的用戶,這很有意義,第二個查詢是我希望運行的查詢,它甚至具有正確的用戶名。
我只是看不到它在哪里獲得不同的用戶名值。
由於評論中的內容會有些混亂,因此我的意思是
當您鍵入用戶提示時,laravels di容器將嘗試解決此依賴性並將新的用戶模型實例傳遞給控制器
public function edit(User $user)
{
print_r($user);
// output is a new instance of an eloquent model
}
但是您最可能想要的是URL中的用戶名
public function edit(string $user)
{
print_r($user);
// output is the string from your route e.g. /user/Bob/edit -> output Bob
}
嘗試一下,您會發現其中的區別=)
順便說一句。 如前所述,您仍然需要將查詢更改為例如
User::where('username', $user)->first();
順便說一句。 就我個人而言,有時我希望對參數更加明確,以免造成混淆。 例如/user/{username}/edit
和控制器方法edit($username)
。 然后,您將更像是……“哦,是的。它的用戶名,而不是整個用戶對象” =)
//Get user with specified username
$user = User::findOrFail($user)->get();
在laravel中,find / findOrFail引用表的主要ID
如果不是ID,應該使用什么
$user = User::where('username', $user)->first();
這樣,您將找到合適的用戶
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.