laravel 选择 where 和 where 条件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20454857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:41:06  来源:igfitidea点击:

laravel select where and where condition

laravellaravel-4eloquent

提问by spacemonkey

I have this basic query i want to perform but an error keeps coming up. Probably due to my newness to laravel.

我有我想要执行的这个基本查询,但错误不断出现。可能是因为我对 Laravel 不熟悉。

here is the code:

这是代码:

$userRecord = $this->where('email', $email)->where('password', $password);
        echo "first name: " . $userRecord->email;

I am trying to get the user record matching the credentials where email AND password are a match. This is throwing an error:

我正在尝试获取与电子邮件和密码匹配的凭据匹配的用户记录。这是抛出错误:

Undefined property: Illuminate\Database\Eloquent\Builder::$email

未定义的属性:Illuminate\Database\Eloquent\Builder::$email

I've checked the email and password being passed to the function, and they are holding values. what is the problem here?

我检查了传递给函数的电子邮件和密码,它们保存着值。这里有什么问题?

Thanks,

谢谢,

回答by mjhinch

$this->where('email', $email)->where('password', $password) 

is returning a Builder object which you could use to append more where filters etc.

正在返回一个 Builder 对象,您可以使用该对象在过滤器等位置附加更多内容。

To get the result you need:

要获得您需要的结果:

$userRecord = $this->where('email', $email)->where('password', $password)->first();

回答by Mgorunuch

$userRecord = Model::where([['email','=',$email],['password','=', $password]])->first();

or

或者

$userRecord = self::where([['email','=',$email],['password','=', $password]])->first();

I` think this condition is better then 2 where. Its where condition array in array of where conditions;

我认为这个条件比 2 好。它的 where 条件数组中的 where 条件数组;

回答by TonyArra

After reading your previous comments, it's clear that you misunderstood the Hash::make function. Hash::make uses bcrypt hashing. By design, this means that every time you run Hash::make('password'), the result will be different (due to random salting). That's why you can't verify the password by simply checking the hashed password against the hashed input.

阅读您之前的评论后,很明显您误解了 Hash::make 函数。Hash::make 使用 bcrypt 散列。按照设计,这意味着每次运行 Hash::make('password') 时,结果都会不同(由于随机加盐)。这就是为什么您不能通过简单地根据散列输入检查散列密码来验证密码。

The proper way to validate a hash is by using:

验证散列的正确方法是使用:

Hash::check($passwordToCheck, $hashedPassword);

So, for example, your login function would be implemented like this:

因此,例如,您的登录功能将如下实现:

public static function login($email, $password) {
    $user = User::whereEmail($email)->first();
    if ( !$user ) return null;  //check if user exists
    if ( Hash::check($password, $user->password) ) {
        return $user;
    } else return null;
}

And then you'd call it like this:

然后你会这样称呼它:

$user = User::login('[email protected]', 'password');
if ( !$user ) echo "Invalid credentials.";
else echo "First name: $user->firstName";

I recommend reviewing the Laravel security documentation, as functions already exist in Laravel to perform this type of authorization.

我建议查看Laravel 安全文档,因为 Laravel 中已经存在执行此类授权的功能。

Furthermore, if your custom-made hashing algorithm generates the same hash every time for a given input, it's a security risk. A good one-way hashing algorithm should use random salting.

此外,如果您的定制散列算法每次都为给定输入生成相同的散列,则存在安全风险。一个好的单向散列算法应该使用随机加盐。

回答by afarazit

You either need to use first()or get()to fetch the results :

您需要使用first()get()获取结果:

$userRecord = $this->where('email', $email)->where('password', $password)->first();

You most likely need to use first()as you want only one result returned.

您很可能需要使用,first()因为您只想返回一个结果。

If the record isn't found nullwill be returned. If you are building this query from inside an Eloquent class you could use self.

如果未找到记录,null将返回。如果您是从 Eloquent 类内部构建此查询,则可以使用self.

for example :

例如 :

$userRecord = self::where('email', $email)->where('password', $password)->first();

More info here

更多信息在这里

回答by Wader

The error is coming from $userRecord->email. You need to use the ->get()or ->first()methods when calling from the database otherwise you're only getting the Eloquent\Builderobject rather than an Eloquent\Collection

错误来自$userRecord->email. 从数据库调用时,您需要使用->get()or->first()方法,否则您只会获取Eloquent\Builder对象而不是Eloquent\Collection

The ->first()method is pretty self-explanatory, it will return the first row found. ->get()returns all the rows found

->first()方法是不言自明的,它将返回找到的第一行。->get()返回找到的所有行

$userRecord = Model::where('email', '=', $email)->where('password', '=', $password)->get();
echo "First name: " . $userRecord->email;

回答by spacemonkey

After rigorous testing, I found out that the source of my problem is Hash::make('password'). Apparently this kept generating a different hash each time. SO I replaced this with my own hashing function (wrote previously in codeigniter) and viola! things worked well.

经过严格的测试,我发现我的问题的根源是Hash::make('password')。显然,这每次都会生成不同的哈希值。所以我用我自己的散列函数(之前在 codeigniter 中写过)和中提琴替换了它!事情进展顺利。

Thanks again for helping out :) Really appreciate it!

再次感谢您的帮助:) 非常感谢!