当前位置:网站首页>Example of parameter passing from laravel query constructor to closure method
Example of parameter passing from laravel query constructor to closure method
2022-06-26 12:22:00 【Special sword】
Here is the custom directory title
Laravel The query constructor closure method accepts parameters
scene :
Set external parameters $status Pass on to where Use inside the closure function .
Code demonstration :
/** * Examples of use */
public function test5(Request $request){
$status = 1;// External parameters ( The status value 1: Through auditing ,2: Failed to pass the audit )
// Get approved products
$data= Goods::where(function ($query) use($status) {
$query->where('status', $status);
})
->select('id','name','status')->get();
//dd($data->toArray());//laravel Directly use the built-in... Within the framework dd() Function to print . toArray() You can convert the result set from the query into an array , This makes the data clearer .
pring_r($data);die;// Print the results
}
/** * Code interpretation */
public function demoExplain(Request $request){
$status = 1;// You need to where Parameters passed in closure methods
//$query It's fixed writing , Don't move .
// use ($ Need to think wherr The variable name passed by the closure method )
// Multiple parameters in use() There is , Division of no. Such as : use($ Parameters A,$ Parameters B,$ Parameters C)
$data= Goods::where(function ($query) use($status) {
//$status The name of the variable passed in , Here you can use dd($status) Print the results
//dd($status); The result of printing is 1 Same as the variable value defined above , That means it's coming in .
$query->where('status', $status);
})
->select('id','name','status')->get();
//dd($data->toArray());//laravel Directly use the built-in... Within the framework dd() Function to print . toArray() You can convert the result set from the query into an array , This makes the data clearer .
pring_r($data);die;// Print the results
}
边栏推荐
- Introduction to the four major FPGA manufacturers abroad
- 女性科学家的流失
- 【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
- Random numbers in leetcode 710 blacklist [random numbers] the leetcode path of heroding
- 国际美妆业巨头押注中国
- 2、 MySQL Foundation
- 1、 MySQL introduction
- Common problems and Thoughts on member operation management
- 我想知道,十大劵商如何开户?在线开户安全么?
- Pratique de l'attaque et de la défense du réseau HUST | 6 Expérience de sécurité du microprogramme de l'équipement IOT | expérience 2 technologie d'atténuation des attaques de l'équipement IOT basée s
猜你喜欢

利用 Repository 中的方法解决实际问题

International beauty industry giants bet on China

Cet article présente la moyenne mobile quadratique linéaire et le fonctionnement simple d'Excel pour réaliser la prédiction des séries chronologiques dans la modélisation.

Basic principle of MOS tube and important knowledge points of single chip microcomputer

Ad - update the modified PCB package to the current PCB
![[graduation season · advanced technology Er] I remember the year after graduation](/img/e7/8e1dafa561217b77a3e3992977a8ec.png)
[graduation season · advanced technology Er] I remember the year after graduation

Statistical genetics: Chapter 2, the concept of statistical analysis

Apiccloud implements the document download and preview functions

Spark-day03-core programming RDD operator

Pre knowledge of hash table -- binary search tree
随机推荐
leetcode 715. Range 模块 (hard)
JMeter response time and TPS listener tutorial
I want to know whether flush is a stock market? Is online account opening safe?
Is it safe to open a securities account in general
Microservice governance (nocas)
UDP协议详解[通俗易懂]
Mqtt disconnect and reconnect
SQL injection in Pikachu shooting range
ctfshow web入门 命令执行web75-77
Refined operation, extending the full life cycle value LTV
菜鸟实战UML——活动图
Scala problem solving the problem of slow SBT Download
Investment forecast and development strategy analysis report of China's rural sewage treatment industry in 2022
I want to know how the top ten securities firms open accounts? Is online account opening safe?
Scala-day06- pattern matching - Generic
Spark-day03-core programming RDD operator
CG bone animation
Analysis report on China's photovoltaic inverter market prospect forecast and investment strategy recommendations in 2022
女性科学家的流失
What are the top ten securities companies? Is it safe to open a mobile account?