Skip to content

Instantly share code, notes, and snippets.

@danielmalone
Created February 22, 2016 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielmalone/0482f09dbd85a7aaa7db to your computer and use it in GitHub Desktop.
Save danielmalone/0482f09dbd85a7aaa7db to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Product;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PagesController extends Controller
{
public function index() {
return view('home');
}
public function pasted(Request $request) {
if ($request->mode === 'replace') {
Product::truncate();
}
$lines = explode("\n", $request->sku_quantity);
foreach ($lines as $line) {
$line = trim($line);
$parts = explode(',', $line);
$sku = trim($parts[0]);
$quantity = trim($parts[1]);
if ($request->mode === 'add' && $new = Product::where('sku', $sku)->first()) {
$currentQuantity = $new->quantity;
Product::where('sku', $sku)->update(['quantity' => $quantity + $currentQuantity]);
}
if ($quantity > 0 && ! isset($new)) {
Product::firstOrCreate(['sku' => $sku, 'quantity' => $quantity]);
}
}
return view('success');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment