Open budistwn15 opened 6 months ago
File: OrderController reference: https://github.com/anggarisky/belibang/blob/main/app/Http/Controllers/OrderController.php
method index
public function index()
{
$my_orders = Order::with('product', 'buyer')->where('creator_id', Auth::id())->orderBy('id', 'DESC')->get();
return Inertia::render('MyOrders', [
'my_orders' => $my_orders
]);
}
catatan:
rekomendasi:
use Inertia\Response;
public function index(): Response
{
$my_orders = Order::with('product', 'buyer')->where('creator_id', Auth::id())->orderByDesc('id')->get();
return Inertia::render('MyOrders', [
'my_orders' => $my_orders
]);
}
method approve
public function approve(Order $order){
// Update the order status to "paid"
$order->update(['is_paid' => 1]);
Transaction::where('user_id', $order->buyer_id)->where('product_id', $order->product_id)->update([
'is_paid' => 1
]);
// Redirect the user back or to another page with a success message
return redirect()->back()->with('message', 'Order approved successfully.');
}
catatan:
rekomendasi:
use Illuminate\Http\RedirectResponse;
public function approve(Order $order): RedirectResponse
{
// Update the order status to "paid"
$order->update(['is_paid' => 1]);
Transaction::where('user_id', $order->buyer_id)->where('product_id', $order->product_id)->update([
'is_paid' => 1
]);
// Redirect the user back or to another page with a success message
return redirect()->back()->with('message', 'Order approved successfully.');
}
Method yang lain nya jika tidak digunakan lebih baik di hapus saja dari controller, dan mungkin karena pada route dibuat resource
untuk OrderController
jadi pada route bisa disesuaikan hanya method2 yang digunakan saja
File: Product Controller References: https://github.com/anggarisky/belibang/blob/main/app/Http/Controllers/ProductController.php
method index
public function index()
{
//
$my_products = Product::whereUserId(Auth::id())->orderBy('id', 'DESC')->get();
return Inertia::render('MyProducts', [
'my_products' => $my_products
]);
}
catatan:
rekomendasi:
use Inertia\Response;
public function index(): Response
{
//
$my_products = Product::whereUserId(Auth::id())->orderByDesc('id')->get();
return Inertia::render('MyProducts', [
'my_products' => $my_products
]);
}
public function create()
{
//
return Inertia::render('CreateProduct');
}
catatan:
rekomendasi:
use Inertia\Response;
public function create(): Response
{
//
return Inertia::render('CreateProduct');
}
method store
public function store(Request $request)
{
//
$request->validate([
'name' => 'required|string|max:255',
'slug' => 'sometimes|string|unique:products,slug', // Ensure the slug is unique in the 'products' table
'category' => 'required|string|in:Ebook,Course,Template',
'price' => 'required|numeric',
'path_cover' => 'required|file|mimes:png',
'path_file' => 'required|file|mimes:zip',
// other validation rules...
]);
$slug = Str::slug($request->name);
$user_id = Auth::user()->id;
$product = new Product([
'name' => $request->name,
'category' => $request->category,
'price' => $request->price,
'slug' => $slug,
'user_id' => $user_id
]);
if($request->hasFile('path_cover')){
$pathCover = $request->file('path_cover')->store('cover_products', 'public');
$product->path_cover = $pathCover;
}
if($request->hasFile('path_file')){
$pathFile = $request->file('path_file')->store('file_products', 'public');
$product->path_file = $pathFile;
}
$product->save();
session()->flash('message', 'Product saved successfully!');
return Inertia::location(route('products.index'));
}
catatan:
rekomendasi
// ProductRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ProductRequest extends FormRequest
{
public function rules(): array
{
return [
'name' => 'required|string|max:255',
'slug' => 'sometimes|string|unique:products,slug', // Ensure the slug is unique in the 'products' table
'category' => 'required|string|in:Ebook,Course,Template',
'price' => 'required|numeric',
'path_cover' => 'required|file|mimes:png',
'path_file' => 'required|file|mimes:zip',
];
}
public function attributes(): array
{
return [
'name' => 'Name',
'slug' => 'Slug',
'category' => 'Category',
'price' => 'Price',
'path_cover' => 'Path Cover',
'path_file' => 'Path File'
];
}
public function authorize(): bool
{
return true;
}
}
use Illuminate\Http\RedirectResponse;
use App\Http\Requests\ProductRequest;
public function store(ProductRequest $request): RedirectResponse
{
$product = new Product([
'name' => $name = $request->name,
'category' => $request->category,
'price' => $request->price,
'slug' => str()->slug($name),
'user_id' => auth()->user()->id
]);
if($request->hasFile('path_cover')){
$pathCover = $request->file('path_cover')->store('cover_products', 'public');
$product->path_cover = $pathCover;
}
if($request->hasFile('path_file')){
$pathFile = $request->file('path_file')->store('file_products', 'public');
$product->path_file = $pathFile;
}
$product->save();
session()->flash('message', 'Product saved successfully!');
return Inertia::location(route('products.index'));
}
File: TransactionController References: https://github.com/anggarisky/belibang/blob/main/app/Http/Controllers/TransactionController.php
method index
public function index()
{
//
$my_transactions = Transaction::with('product.user')->where('user_id', Auth::id())->orderBy('id', 'DESC')->get();
return Inertia::render('MyTransactions', [
'my_transactions' => $my_transactions
]);
}
catatan:
rekomendasi:
use Inertia\Response;
public function index(): Response
{
//
$my_transactions = Transaction::with('product.user')->where('user_id', Auth::id())->orderByDesc('id')->get();
return Inertia::render('MyTransactions', [
'my_transactions' => $my_transactions
]);
}
File: Order (Model) references: https://github.com/anggarisky/belibang/blob/main/app/Models/Order.php
class Order extends Model
{
use HasFactory, SoftDeletes;
protected $guarded = [
'id'
];
public function product(): BelongsTo {
return $this->belongsTo(Product::class);
}
public function buyer(){
return $this->belongsTo(User::class, 'buyer_id');
}
}
pada method yang ada pada model ini lebih baik di berikan return type yang sesuai dengan method yang dibuat
rekomendasi:
class Order extends Model
{
use HasFactory, SoftDeletes;
protected $guarded = [
'id'
];
public function product(): BelongsTo {
return $this->belongsTo(Product::class);
}
public function buyer(): BelongsTo
{
return $this->belongsTo(User::class, 'buyer_id');
}
}
File: Product (Model) references: https://github.com/anggarisky/belibang/blob/main/app/Models/Product.php
class Product extends Model
{
use HasFactory, SoftDeletes;
protected $guarded = [
'id'
];
public function user(){
return $this->belongsTo(User::class);
}
}
pada method yang ada pada model ini lebih baik di berikan return type yang sesuai dengan method yang dibuat
rekomendasi:
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Product extends Model
{
use HasFactory, SoftDeletes;
protected $guarded = [
'id'
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}
File: Transaction (Model) references: https://github.com/anggarisky/belibang/blob/main/app/Models/Transaction.php
File: Order (Migrations) references: https://github.com/anggarisky/belibang/blob/main/database/migrations/2024_02_22_012021_create_orders_table.php
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->integer('total_price');
$table->foreignId('product_id')->constrained()->onDelete('cascade');
$table->unsignedBigInteger('buyer_id');
$table->boolean('is_paid');
$table->softDeletes();
$table->timestamps();
$table->foreign('buyer_id')->references('id')->on('users')->onDelete('cascade');
});
catatan:
onDelete('cascade')
bisa menggunakan cascadeOnDelete()
rekomendasi:
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->integer('total_price');
$table->foreignId('product_id')->constrained()->cascadeOnDelete(');
$table->foreignId('buyer_id')->constrained('users')->cascadeOnDelete();
$table->boolean('is_paid');
$table->softDeletes();
$table->timestamps();
});
File: Order (Migrations) references: https://github.com/anggarisky/belibang/blob/main/database/migrations/2024_02_22_014549_add_creator_id_to_orders_table.php
public function up(): void
{
Schema::table('orders', function (Blueprint $table) {
//
$table->unsignedBigInteger('creator_id');
$table->foreign('creator_id')->references('id')->on('users')->onDelete('cascade');
});
}
catatan:
rekomendasi:
public function up(): void
{
Schema::table('orders', function (Blueprint $table) {
$table->foreignId('creator_id')->constrained('users')->cascadeOnDelete();
});
}
files: routes (web) references: https://github.com/anggarisky/belibang/blob/main/routes/web.php
Views
import { Head } from '@inertiajs/react';
export default function MetaTags({ image = 'https://buildwithangga.com/themes/front/images/meta/kelas-online-belajar-ui-ux-design-dan-web-development-build-with-angga.png', title = 'BuildWithAngga', description = 'BuildWithAngga menyediakan kelas online bersertifikat dan komunitas untuk belajar skills UI UX design dan coding yang dibimbing oleh mentor expert. Jadilah ahli bersama kami!', url = 'https://buildwithangga.com', }) { return (
);
}
- contoh penggunaan misalnya pada file `Benefits.jsx`
```javascript
import Navigation from '@/Components/Navigation';
import { Head } from '@inertiajs/react';
import MetaTags from '@/Components/MetaTags';
export default function Benefits({}) {
return (
<div>
<Head title="Benefits" />
<MetaTags
title="Discover Products"
description="Explore our wide range of products."
url="http://example.com/page"
/>
<Navigation/>
</div>
);
}
File: FrontController References: https://github.com/anggarisky/belibang/blob/main/app/Http/Controllers/FrontController.php
catatan:
rekomendasi:
catatan:
rekomendasi:
Method discover
catatan:
$products
jika mengurutkan secara descending bisa dipersingkat penulisan nya menjadiorderByDesc('id')
rekomendasi:
catatan:
rekomendasi:
method checkout
catatan:
rekomendasi:
method download
catatan:
rekomendasi: