我有一个名为“items”的表和一个名为“ref_code”的 where 条件的输入。
$items = DB::table('items')
->where('ref_code','=', $request->ref_code)
->get(['id', 'ref_code', 'name','price']);
Run Code Online (Sandbox Code Playgroud)
但我似乎无法获取每一列的值。
我使用以下方法检查了查询构建器是否工作:
return $items;
Run Code Online (Sandbox Code Playgroud)
幸运的是,没有问题。
但是返回或获取单个值不适用于:
return $items->id
Run Code Online (Sandbox Code Playgroud)
我的语法错了吗?所有这些都在我的控制器中。
编辑:我试过了
dd($items);
Run Code Online (Sandbox Code Playgroud)
在返回之前,它向我展示了这个:
Collection {#325 ?
#items: array:1 [?
0 => {#322 ?}
]
}
Run Code Online (Sandbox Code Playgroud) 有没有人经历过这个?
Bootstrap Javascript(For modals,accordion和其他动画)对我有冲突Chart.js.
这是cdn链接,我使用了缩小版本.(Chart.min.js)
如果这有帮助,我将展示我script的图表:
<script src="{{ asset('js/Chart.min.js') }}"></script>
<script>
let myChart = document.getElementById('myChart').getContext('2d');
let massPopChart = new Chart(myChart, {
responsive: true,
type:'line',
data:{
labels:['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
datasets:[{
label:'Sales',
data:[
500,
304,
601,
670,
912,
612,
500
],
backgroundColor: 'rgba(129, 207, 238, 1)',
borderWidth: 1,
borderColor: '#000',
hoverBorderWidth: 3,
hoverBorderColor: '#000'
}]
},
options:{
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
}
}]
}, …Run Code Online (Sandbox Code Playgroud) 我在从两个表中获取某个字段的总和时遇到问题。
首先,这是我的查询。
//Getting of Cost of Goods Sold (Menus)
$totalMenuCost = DB::raw('(SUM(orders.qty * orders.cost)) as cost');
$yearMenuSold = DB::raw('YEAR(orders.created_at) as year');
$menuscost = DB::table('orders')
->where('status', 'served')
->select($totalMenuCost, $yearMenuSold);
//Getting of Cost of Goods Sold (Items)
$totalItemCost = DB::raw('(SUM(purchases.qty * purchases.cost)) as cost');
$yearItemSold = DB::raw('YEAR(purchases.created_at) as year');
$itemcost = DB::table('purchases')
->where('status', 'served')
->union($menuscost)
->select($totalItemCost, $yearItemSold)
->get();
Run Code Online (Sandbox Code Playgroud)
当我尝试这样做时return $itemcost。它返回两行:
[
{
cost: "792.00",
year: 2017
},
{
cost: "1700.00",
year: 2017
}
]
Run Code Online (Sandbox Code Playgroud)
我试图让它返回一行但添加它,如下所示:
[
{
cost: "2492.00", // …Run Code Online (Sandbox Code Playgroud) Laravel有一个timestamp生成列created_at和的列updated_at。
我想问一下设置应用程序时区的最佳方法是什么?我想将我的应用程序的时区设置为GMT + 8。
所以我有两个系列,sales和costs.
现在我需要将它们合并为一个集合以满足我的foreach条件(我不确定我是否可以在一个集合中使用两个集合foreach)
RAW查询:
//Raw MySQL Queries for Sales
$total_sales = DB::raw('SUM(receipts.total) as totalSales');
$year_receipt_created = DB::raw('YEAR(receipts.created_at) as year');
//Raw MyQSL Queries for Cost of Goods Sold
$total_cost = DB::raw('(SUM(qty * cost)) as totalCost');
$year_sold = DB::raw('YEAR(created_at) as year');
Run Code Online (Sandbox Code Playgroud)
这是我query的两个collections:
$sales = DB::table('receipts')
->where('status', 'served')
->where('mode', 'restaurant')
->select($total_sales, $year_receipt_created)
->groupBy('year')
->get();
$costs = DB::table('orders')
->where('status', 'served')
->select($total_cost, $year_sold)
->groupBy('year')
->get();
Run Code Online (Sandbox Code Playgroud)
我尝试过测试的东西:将集合转换为数组并尝试合并它们但我似乎遇到了问题.
我还原它是因为我不知道这是不是最好的方式.请让我知道最好的方法是什么.
更新:这是这两个查询的输出,希望它有所帮助:
销售
{
totalSales: …Run Code Online (Sandbox Code Playgroud) 我一直在使用的语法Carbon::now()->startOfWeek()和Carbon::now()->endOfWeek()一段时间了.
它返回一周的第一天,即一周的日期Monday和一周的最后一天Sunday.(我不知道为什么不是周日和周六)
但现在,我想每一个day的current week.那么剩下的就是日期Tuesday,Wednesday,Thursday,Friday,和Saturday.
这是我获取Monday和确切的语法Sunday.
$monday = Carbon::now()->startOfWeek();
$sunday = Carbon::now()->endOfWeek();
Run Code Online (Sandbox Code Playgroud) 为了开始,我做了一个定制的Artisan Command MySqlRestore.
它只是使用转储sql文件来恢复数据库.
这是我的MySqlRestore代码:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class MySqlRestore extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'db:restore';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Restores database using info from .env';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the …Run Code Online (Sandbox Code Playgroud) 当我回声Carbon::now()。它只显示日期时间。“2018-07-05 09:21:21”
但是当我在 MongoDB 上插入它时,它变成了一个对象;
{
"date" : "2018-07-05 09:21:21.020981",
"timezone_type" : 3,
"timezone" : "UTC"
}
Run Code Online (Sandbox Code Playgroud)
我只想要date. 为什么会这样?
编辑:
我试过这样做
$currentDateTime = strtotime(Carbon::now());
$request->merge([
'dateSample' => date("Y-m-d H:i:s", $currentDateTime)
]);
Run Code Online (Sandbox Code Playgroud)
但它保存为string类型。我需要它是Date类型。