我曾经在codeigniter问过这个问题,现在我想在laravel中询问这个问题.
我想为自定义帮助器,模型和控制器设置一个全局变量,该变量来自数据库结果.
这是一个例子:
不知道把变量放在哪里
$data= DB::table('categories')->where('id', '2')->first();
$this->product_var = $data->product; **//main global variable**
Run Code Online (Sandbox Code Playgroud)
自定义助手
function test() {
if($this->product_var=="product name") {
}
}
Run Code Online (Sandbox Code Playgroud)
我的控制器
function index() {
echo $this->product_var;
}
Run Code Online (Sandbox Code Playgroud)
我的模特
function get_data() {
echo $this->product_var;
}
Run Code Online (Sandbox Code Playgroud)
正如你可以在上面看到我的剧本,$this->product_var几乎用custom helper,my controller和my model.在codeigniter中,我们在libraries文件夹中创建Globals.php,或者只是将变量放在核心控制器中.我应该在哪里设置全局变量?
我想得到最后的余额并从后端更新xxx用户的一些交易..不幸的是,同时,xxx也从前端进行交易,所以当我处理我的查询时,xxx也处理相同的查询,所以它变得相同最后的余额.
这是我的剧本.
假设:xxx最后余额是10000
$transaction = 1000;
$getData = mysqli_fetch_array(mysqli_query($conn,"select balance from tableA where user='xxx'"));
$balance = $getData["balance"] - $transaction; //10000 - 1000 = 9000
mysqli_query($conn,"update tableA set balance='".$balance."' where user='xxx'");
Run Code Online (Sandbox Code Playgroud)
同时用户xxx从前端做交易..
$transaction = 500;
$getData = mysqli_fetch_array(mysqli_query($conn,"select balance from tableA where user='xxx'"));
$balance = $getData["balance"] - $transaction; //10000-500 it should be 9000-500
mysqli_query($conn,"update tableA set balance='".$balance."' where user='xxx'");
Run Code Online (Sandbox Code Playgroud)
我怎样才能先完成查询,然后用户xxx可以处理查询?
我想为 mysql 存储过程创建串联。
在 php 中,我可以使用 (.) 连接变量
example :
$table = "table_".$_GET["number"];
$sql = mysqli_query($conn,select * from `".$table."` where `field`='$field');
Run Code Online (Sandbox Code Playgroud)
在mysql存储过程中,我不知道如何连接变量。
这是我的存储过程脚本。
CREATE DEFINER=`user`@`localhost` PROCEDURE `testprocedure`(v_number int(5))
BEGIN
declare v_table varchar(20);
set v_table = 'table_'.v_number;
update `v_table` set `field`=v_number where `field2`='test';
END $$
DELIMITER ;
Run Code Online (Sandbox Code Playgroud)
那么如何添加到v_number集合v_table然后添加v_table到更新表查询?