Laravel如何与全名结合使用名字中间名字缩写姓

时间:2019-05-04 14:53:54

标签: php laravel

使用全名进行连载的正确方法是什么 它在表中的三列resident_fname resident_mi resident_lname

$resident = Resident::whereRAW("Concat(resident_fname,'',resident_mi,'',resident_lname) LIKE '%{$request->input('reservation_name')}%'")
        ->first();

2 个答案:

答案 0 :(得分:0)

我们需要对原始查询使用CONCAT_WS(),但也要了解CONCAT()。那么为什么我们使用CONCAT_WS()?

CONCAT()和CONCAT_WS()函数都用于连接两个或多个字符串,但是它们之间的基本区别在于CONCAT_WS()函数可以与字符串之间的分隔符一起进行串联,而在CONCAT()函数中没有分隔符的概念。

示例:

mysql> Select CONCAT('Akash','is','a','good','developer') AS 'Example of CONCAT()';

+---------------------+
| Example of CONCAT() |
+---------------------+
| Akashisagooddeveloper   |
+---------------------+

1 row in set (0.00 sec)

mysql> Select CONCAT_WS(' ','Akash','is','a','good','developer') AS 'Example of CONCAT_WS()';

+------------------------+
| Example of CONCAT_WS() |
+------------------------+
| Akash is a good developer  |
+------------------------+

1 row in set (0.00 sec)
$resident = Resident::where(DB::raw('CONCAT_WS(" ", resident_fname, resident_mi, resident_lname)'), 'like', $request->input('reservation_name'))->first();

答案 1 :(得分:0)

在像这样的模型中创建函数的更好的主意

?xml version="1.0" encoding="UTF-8"?>
<Error><Code>SignatureDoesNotMatch</Code><Message>The request 
signature we calculated does not match the signature you provided. 
Check your key and signing method.</Message> 
<AWSAccessKeyId>gibberish</AWSAccessKeyId><StringToSign>PUT

,您可以通过

进行访问
class Resident extends Model{
    public function name(){
        return $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;
    }
}