Files
Kent-logistics-Laravel/app/Http/Controllers/MarkListController.php
2025-11-07 17:34:56 +05:30

96 lines
2.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\MarkList;
use App\Models\User;
use Carbon\Carbon;
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;
class MarkListController extends Controller
{
/**
* Add new Mark No (entered by user)
*/
public function addmarkno(Request $request)
{
try {
// Validate input
$request->validate([
'mark_no' => 'required|string|max:255|unique:mark_list,mark_no',
'origin' => 'required|string|max:255',
'destination' => 'required|string|max:255',
]);
} catch (\Illuminate\Validation\ValidationException $e) {
// Check if the mark_no uniqueness failed
if (isset($e->validator->failed()['mark_no']['Unique'])) {
return response()->json([
'success' => false,
'message' => 'Mark No already taken by another customer.'
], 400);
}
// Default validation error
return response()->json([
'success' => false,
'message' => $e->getMessage()
], 400);
}
// Authenticate user via JWT
$user = JWTAuth::parseToken()->authenticate();
if (!$user) {
return response()->json([
'success' => false,
'message' => 'Unauthorized'
], 401);
}
// Create mark record
$mark = MarkList::create([
'mark_no' => $request->mark_no,
'origin' => $request->origin,
'destination' => $request->destination,
'customer_id' => $user->customer_id,
'customer_name' => $user->customer_name,
'company_name' => $user->company_name,
'mobile_no' => $user->mobile_no,
'date' => Carbon::now()->toDateString(),
'status' => 'active',
]);
return response()->json([
'success' => true,
'message' => 'Mark No added successfully.',
'data' => $mark
], 200);
}
/**
* Show all marks for the logged-in user
*/
public function showmarklist()
{
$user = JWTAuth::parseToken()->authenticate();
if (!$user) {
return response()->json([
'success' => false,
'message' => 'Unauthorized'
], 401);
}
$marks = MarkList::where('customer_id', $user->customer_id)
->orderBy('id', 'desc')
->get();
return response()->json([
'success' => true,
'data' => $marks
]);
}
}