简体   繁体   English

Laravel-奇怪的查询行为

[英]Laravel - Weird behaviour with query

So, I'm using a simple method to display an edit form, using information I've seen from various Laravel CRUD tutorials. 因此,我使用一种简单的方法来显示编辑表单,并使用了从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

}

As you can see, this method couldn't be any simpler. 如您所见,此方法再简单不过了。

The route for this method is as follows: 此方法的路由如下:

/user/{user}/edit

Where {user} is the Primary Key of the model bound to the route, in my case this happens to be $username . 其中{user}是绑定到路由的模型的主键,在我的情况下,它恰好是$username

So, If I were to enter /user/stevew/edit I'd expect the query to be the following: 因此,如果我要输入/user/stevew/edit ,则希望查询如下:

select * from users where username = ? and select * from用户where username = ? and用户where username = ? and select * from where username = ? and users . where username = ? and用户. deleted_at is null limit 1 delete_at is null limit 1

  • ? = stevew =史蒂夫

The thing is: the query isn't returning the user I expect, instead of returning the details for stevew, I get the details for another user: edwardl. 问题是:查询没有返回我期望的用户,而是返回了另一个用户edwardl的详细信息,而不是返回stevew的详细信息。

This is the User table migration 这是用户表迁移

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();
});

This is a dump of the queries executed 这是执行的查询的转储

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"
}

I get that the first query is the logged in user, which makes sense, the second query is the query I expect to run, it even has the correct username. 我知道第一个查询是已登录的用户,这很有意义,第二个查询是我希望运行的查询,它甚至具有正确的用户名。

I just can't see where it is getting a different username value. 我只是看不到它在哪里获得不同的用户名值。

Since its going to be a bit messy in the comments, here is what I meant by 由于评论中的内容会有些混乱,因此我的意思是

When you typehint User, laravels di container will try to resolve this dependency and pass a new instance of your user model to the controller 当您键入用户提示时,laravels di容器将尝试解决此依赖性并将新的用户模型实例传递给控制器

public function edit(User $user)
{
    print_r($user);
    // output is a new instance of an eloquent model
}

But what you most likely want is the user name from the URL 但是您最可能想要的是URL中的用户名

public function edit(string $user)
{
    print_r($user);
    // output is the string from your route e.g. /user/Bob/edit -> output Bob
}

Just give both a try and you will notice the difference =) 尝试一下,您会发现其中的区别=)

btw. 顺便说一句。 as mentioned, you'll still need to change the query to eg 如前所述,您仍然需要将查询更改为例如

User::where('username', $user)->first();

btw. 顺便说一句。 personally I sometimes like to be more explicit with parameters to avoid confusion. 就我个人而言,有时我希望对参数更加明确,以免造成混淆。 eg /user/{username}/edit and the controller method edit($username) . 例如/user/{username}/edit和控制器方法edit($username) Then you'll be more like ... "oh yeah .. its the username, not the entire user object" =) 然后,您将更像是……“哦,是的。它的用户名,而不是整个用户对象” =)

//Get user with specified username
$user = User::findOrFail($user)->get();

In laravel find / findOrFail refers to the primary id of the table 在laravel中,find / findOrFail引用表的主要ID

what you should use if its other than the id 如果不是ID,应该使用什么

$user = User::where('username', $user)->first();

This way you'll be getting the right user 这样,您将找到合适的用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM