我想知道像这样大的功能是否可以。如果没有,还有没有其他有效的方法可以使它变得更容易,更快,并在不降低速度的情况下高效地执行?我是否还应将某些动作分成不同的功能? 如果是这样,我还必须由于id而重复$ output?
public function select(Request $request)
{
if ($request->ajax()) {
if (Auth::user()) {
$output="";
$id = auth()->user()->id;
if ($request->action) {
if ($request->action === "destroy_sales") {
Sales::where('id',$request->item_id)->where('spreadsheet_id', $request->spreadsheet_id)->delete();
}else if ($request->action === "edit_sales") {
$post = new Sales;
$post = Sales::find($request->item_id);
$post->spreadsheet_id = $request->spreadsheet_id;
$post->sold_price = $request->name;
$post->sold_price = $request->sold_price;
$post->save();
}
if (Auth::user()->subscribed('main')) {
$sales = DB::table('Sales')->where('user_id', '=', $id)
->where('spreadsheet_id', '=', $request->spreadsheet_id)
->whereBetween('created_at',[$request->start_date, $request->end_date])->get();
$summary = DB::table('Sales')->where('user_id', '=', $id)
->where('spreadsheet_id', '=', $request->spreadsheet_id)
->whereBetween('created_at',[$request->start_date, $request->end_date]);
if ($request->action == "summary") {
$sales = DB::table('Sales')->where('user_id', '=', $id)
->whereBetween('created_at',[$request->start_date, $request->end_date])->get();
$summary = DB::table('Sales')->where('user_id', '=', $id)
->whereBetween('created_at',[$request->start_date, $request->end_date]);
}
}else{
$sales = DB::table('Sales')->take(3)->where('user_id', '=', $id)
->where('spreadsheet_id', '=', $request->spreadsheet_id)
->whereBetween('created_at',[$request->start_date, $request->end_date])->get();
$summary = DB::table('Sales')->take(3)->where('user_id', '=', $id)
->where('spreadsheet_id', '=', $request->spreadsheet_id)
->whereBetween('created_at',[$request->start_date, $request->end_date]);
if ($request->action == "summary") {
$sales = DB::table('Sales')->take(3)->where('user_id', '=', $id)
->whereBetween('created_at',[$request->start_date, $request->end_date])->get();
$summary = DB::table('Sales')->take(3)->where('user_id', '=', $id)
->whereBetween('created_at',[$request->start_date, $request->end_date]);
}
}
if ($sales->count()>0) {
$output.="
<table>
<tr>
<th>Name</th>
<th>Sold Price</th>
<th>Item Cost</th>
<th>Shipping Charge</th>
<th></th>
<th></th>
</tr>
";
foreach ($sales as $key => $sales) {
$output.=
'<tr>'.
'<th><input type="text" name="name" value="'.$sales->name.'"></th>'.
'<th><input type="number" name="sold_price" value="'.$sales->sold_price.'"></th>'.
'<th><input type="number" name="item_cost" value="'.$sales->item_cost.'"></th>'.
'<th><button class="sales_edit" id="'.$sales->id.'">Edit</button><button class="sales_delete" id="'.$sales->id.'">Delete</button>
<input id="'.$sales->id.'sheet" type="hidden" value="'.$sales->spreadsheet_id.'"></input>
</th>'.
'</tr>';
}
$output.=
"</table>";
return response()->json([
'spreadsheet_grid' =>$output,
'spreadsheet_sales'=>array(
"sold_price" => $summary->sum('sold_price')+$summary->sum('shipping_charge'),
"item_cost" => $summary->sum('item_cost')+$summary->sum('shipping_cost'),
"fees" =>$summary->sum('fees')+$summary->sum('other_fees')+$summary->sum('processing_fees'),
"profit" =>$summary->sum('profit'),
"item_id" =>$sales->spreadsheet_id
)
]);
}else{
$output.="<div>Empty</div>";
return response()->json([
'spreadsheet_grid' =>$output,
'spreadsheet_sales'=>array(
"sold_price" =>0,
"item_cost" =>0,
"fees" =>0,
"profit" =>0
)
]);
}
}
}
}
}
答案 0 :(得分:1)
我认为您没有使用laravel,而是在laravel框架中编写了肮脏的php代码。
如果您希望此方法仅对经过身份验证的用户有效,请将此路由添加到auth
中间件。
如果您希望此功能仅适用于ajax,则有两种选择:
if(!$request->ajax()) abort(404);
// and rest of your method
$request->validate([
"action" => [
"required",
Rule::in(["destroy_sales", "edit_sales"])
],
"item_id" => [
"required",
function($attribute, $value, $fail){
try {
Sales::findOrFail($value);
} catch (ModelNotFoundException $exception){
$fail($exception->getMessage());
}
}
]
]);
创建两个新的受保护方法来删除和编辑您的销售模型,或创建两个新路线,例如Route::resource("sales", "SaleController");
使用laravel的批量更新功能更新您的模型。将具有fillable
的受保护属性添加到具有以下属性的销售模型中:(“ spreadsheet_id”,“ name”,“ sold_price”)并使用:
Sale::find($request->item_id)->update($request->all());
spreadshit_id
这时您可以将方法的大小减小70%。