当前位置:网站首页>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
}
边栏推荐
- Prospering customs through science and technology, Ronglian and Tianjin Customs jointly build a genomic database and analysis platform
- Build Pikachu shooting range and introduction
- 2022 edition of China's cotton chemical fiber printing and dyeing Market Status Investigation and Prospect Forecast Analysis Report
- Re recognized! Know that Chuangyu has been selected as one of the first member units of the "business security promotion plan"
- Analysis report on dynamic research and investment planning suggestions of China's laser medical market in 2022
- Jmeter响应时间和tps监听器使用教程
- Lintcode 130 · stacking
- 利用 Repository 中的方法解决实际问题
- 十大券商有哪些?手机开户安全么?
- "Pinduoduo and short video speed version", how can I roast!
猜你喜欢
leetcode 715. Range 模块 (hard)
International beauty industry giants bet on China
Mqtt disconnect and reconnect
Excel operation of manual moving average method and exponential smoothing method for time series prediction
Re recognized! Know that Chuangyu has been selected as one of the first member units of the "business security promotion plan"
HUST网络攻防实践|6_物联网设备固件安全实验|实验二 基于 MPU 的物联网设备攻击缓解技术
11、 Box styles and user interface
Introduction to the four major FPGA manufacturers abroad
Jmeter响应时间和tps监听器使用教程
Prospering customs through science and technology, Ronglian and Tianjin Customs jointly build a genomic database and analysis platform
随机推荐
Refined operation, extending the full life cycle value LTV
MS17_ 010 utilization summary
我想知道,股票开户有哪些优惠活动?网上开户是否安全么?
1、 MySQL introduction
What should I do from member labels to portraits?
18: Chapter 3: development of pass service: 1: SMS login & registration process, introduction; (SMS verification code is used here)
PolarisMesh系列文章——概念系列(一)
dried food! Yiwen will show you SD card, TF card and SIM card!
我想知道,十大劵商如何开户?在线开户安全么?
What determines the rent
China Medical Grade hydrogel market supply and demand research and prospect analysis report 2022 Edition
VMware虚拟机 桥接模式 无法上网 校园网「建议收藏」
Five strategies and suggestions of member marketing in consumer goods industry
Apiccloud implements the document download and preview functions
Measures to support the development of cultural and creative industries in Futian District, Shenzhen
JMeter response time and TPS listener tutorial
Five trends of member marketing of consumer goods enterprises in the future
A most practical arbitrage wizard EA [2022 modified version]
Omnichannel membership - tmall membership 1: opening tutorial
量化初级 -- akshare获得股票代码,最简策略