diff --git a/app/Http/Controllers/ProductsController.php b/app/Http/Controllers/ProductsController.php new file mode 100644 index 0000000..df075fc --- /dev/null +++ b/app/Http/Controllers/ProductsController.php @@ -0,0 +1,73 @@ +get('q'); + $products = Product::where(function($query) use ($q) { + if ($q) { + $query->where('name', 'like', '%' . $q . '%'); + } + }) + ->orderBy('name')->paginate(25); + + if (in_array($request->get('action'), ['edit','delete']) && $request->has('id')) + $editableProduct = Product::find($request->get('id')); + + return view('products.index', compact('products','editableProduct')); + } + + public function store(Request $request) + { + $this->validate($request, [ + 'name' => 'required|max:20', + 'cash_price' => 'required|numeric', + 'credit_price' => 'nullable|numeric', + ]); + + Product::create($request->only('name','cash_price','credit_price')); + + flash(trans('product.created'), 'success'); + + return redirect()->route('products.index'); + } + + public function update(Request $request, $productId) + { + $this->validate($request, [ + 'name' => 'required|max:20', + 'cash_price' => 'required|numeric', + 'credit_price' => 'nullable|numeric', + ]); + + $routeParam = $request->only('q'); + + $product = Product::findOrFail($productId)->update($request->only('name','cash_price','credit_price')); + + flash(trans('product.updated'), 'success'); + + return redirect()->route('products.index', $routeParam); + } + + public function destroy(Request $request, $productId) + { + $this->validate($request, [ + 'product_id' => 'required|exists:products,id', + ]); + + if ($request->get('product_id') == $productId && Product::findOrFail($productId)->delete()) { + flash(trans('product.deleted'), 'success'); + return redirect()->route('products.index'); + } + + flash(trans('product.undeleted'), 'error'); + return back(); + } +} diff --git a/app/Product.php b/app/Product.php index e588cf3..27a9ef1 100644 --- a/app/Product.php +++ b/app/Product.php @@ -6,10 +6,11 @@ use Illuminate\Database\Eloquent\Model; class Product extends Model { - protected $fillable = ['cash_price', 'credit_price']; + protected $fillable = ['name','cash_price','credit_price']; public function getPrice($type = 'cash') { + // TODO: if there is no credit_price then return cash_price if ($type == 'credit') { return $this->credit_price; } diff --git a/database/migrations/2017_04_09_013901_create_products_table.php b/database/migrations/2017_04_09_013901_create_products_table.php index 360433f..a10ac6d 100644 --- a/database/migrations/2017_04_09_013901_create_products_table.php +++ b/database/migrations/2017_04_09_013901_create_products_table.php @@ -17,7 +17,7 @@ class CreateProductsTable extends Migration $table->increments('id'); $table->string('name'); $table->unsignedInteger('cash_price'); - $table->unsignedInteger('credit_price'); + $table->unsignedInteger('credit_price')->nullable(); $table->timestamps(); }); } diff --git a/resources/lang/id/master.php b/resources/lang/id/master.php new file mode 100644 index 0000000..1a924e9 --- /dev/null +++ b/resources/lang/id/master.php @@ -0,0 +1,28 @@ + 'Master', + 'list' => 'Daftar Master', + 'search' => 'Cari Master', + 'not_found' => 'Master tidak ditemukan', + 'empty' => 'Belum ada Master', + 'back_to_index' => 'Kembali ke daftar Master', + + // Actions + 'create' => 'Input Master Baru', + 'created' => 'Input Master baru telah berhasil.', + 'show' => 'Detail Master', + 'edit' => 'Edit Master', + 'update' => 'Update Master', + 'updated' => 'Update data Master telah berhasil.', + 'delete' => 'Hapus Master', + 'delete_confirm' => 'Anda yakin akan menghapus Master ini?', + 'deleted' => 'Hapus data Master telah berhasil.', + 'undeleted' => 'Data Master gagal dihapus.', + 'undeleteable' => 'Data Master tidak dapat dihapus.', + + // Attributes + 'name' => 'Nama Master', + 'description' => 'Deskripsi Master', +]; \ No newline at end of file diff --git a/resources/lang/id/product.php b/resources/lang/id/product.php new file mode 100644 index 0000000..9fd4eca --- /dev/null +++ b/resources/lang/id/product.php @@ -0,0 +1,30 @@ + 'Produk', + 'list' => 'Daftar Produk', + 'search' => 'Cari Produk', + 'not_found' => 'Produk tidak ditemukan', + 'empty' => 'Belum ada Produk', + 'price' => 'Harga', + 'back_to_index' => 'Kembali ke daftar Produk', + + // Actions + 'create' => 'Input Produk Baru', + 'created' => 'Input Produk baru telah berhasil.', + 'show' => 'Detail Produk', + 'edit' => 'Edit Produk', + 'update' => 'Update Produk', + 'updated' => 'Update data Produk telah berhasil.', + 'delete' => 'Hapus Produk', + 'delete_confirm' => 'Anda yakin akan menghapus Produk ini?', + 'deleted' => 'Hapus data Produk telah berhasil.', + 'undeleted' => 'Data Produk gagal dihapus.', + 'undeleteable' => 'Data Produk tidak dapat dihapus.', + + // Attributes + 'name' => 'Nama Produk', + 'cash_price' => 'Harga Tunai', + 'credit_price' => 'Harga Kredit', +]; \ No newline at end of file diff --git a/resources/views/layouts/partials/top-nav.blade.php b/resources/views/layouts/partials/top-nav.blade.php index 7fee250..019adf6 100644 --- a/resources/views/layouts/partials/top-nav.blade.php +++ b/resources/views/layouts/partials/top-nav.blade.php @@ -22,12 +22,15 @@ @if (Auth::check())
  • -
    + {{ csrf_field() }}
  • +
  • + {{ link_to_route('products.index', trans('product.list')) }} +