-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from hanisirfan/visitor_add
Visitor add
- Loading branch information
Showing
19 changed files
with
1,052 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Visitor; | ||
use Illuminate\Support\Str; | ||
use Illuminate\Http\Request; | ||
use chillerlan\QRCode\QRCode; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class VisitorController extends Controller | ||
{ | ||
/** | ||
* Create a new controller instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->middleware('auth'); | ||
} | ||
|
||
/** | ||
* Show the application dashboard. | ||
* | ||
* @return \Illuminate\Contracts\Support\Renderable | ||
*/ | ||
public function index(Request $request, $uuid) | ||
{ | ||
if ($request->isMethod('get')) { | ||
// Check if visitor exists. | ||
if (Visitor::where('uuid', $uuid)->first()) { | ||
$visitor = Visitor::where('uuid', $uuid)->first(); | ||
|
||
return view('visitors.view')->with(['visitor' => $visitor]); | ||
} else { | ||
return abort(404); | ||
} | ||
} | ||
} | ||
|
||
public function create(Request $request) | ||
{ | ||
|
||
if ($request->isMethod('post')) { | ||
|
||
$validated = $request->validate([ | ||
'visitor-name' => ['required'], | ||
'visitor-ic-number' => ['required'], | ||
'visitor-vehicle-plate-number' => ['required'], | ||
'visitor-datetime' => ['required'], | ||
]); | ||
|
||
$uuid = (string) Str::uuid(); | ||
$name = $request->input('visitor-name'); | ||
$icNumber = $request->input('visitor-ic-number'); | ||
$plateNumber = $request->input('visitor-vehicle-plate-number'); | ||
$accessDateTime = $request->input('visitor-datetime'); | ||
$userName = Auth::user()->name; | ||
|
||
$visitor = Visitor::create([ | ||
'uuid' => $uuid, | ||
'name' => $name, | ||
'ic_number' => $icNumber, | ||
'vehicle_plate_number' => $plateNumber, | ||
'visit_datetime' => $accessDateTime, | ||
'added_by' => $userName, | ||
]); | ||
|
||
$request->session()->flash('addVisitorSuccess', 'Visitor successfully added!'); | ||
|
||
// Reference: https://startutorial.com/view/php-qr-code-generator-with-source-code | ||
$qrCode = (new QRCode())->render('visitorqr: ' . $uuid); | ||
$request->session()->flash('qrCode', $qrCode); | ||
$request->session()->flash('uuid', $uuid); | ||
|
||
return back(); | ||
} | ||
|
||
if ($request->isMethod('get')) { | ||
return view('visitors.add'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use DateTime; | ||
use chillerlan\QRCode\QRCode; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Concerns\HasUuids; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class Visitor extends Model | ||
{ | ||
use HasFactory; | ||
use HasUuids; // Use UUID instead of auto incremented IDs. | ||
|
||
protected $primaryKey = 'uuid'; | ||
|
||
protected $appends = ['qr', 'date_time']; | ||
|
||
/** | ||
* Generate QR based on UUIDs | ||
* | ||
*/ | ||
public function getQRAttribute() { | ||
return (new QRCode())->render('visitorqr: ' . $this->uuid); | ||
} | ||
|
||
/** | ||
* Generate QR based on UUIDs | ||
* | ||
*/ | ||
public function getDateTimeAttribute() { | ||
return new DateTime($this->visit_datetime); | ||
} | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array<int, string> | ||
*/ | ||
protected $fillable = [ | ||
'uuid', | ||
'name', | ||
'ic_number', | ||
'vehicle_plate_number', | ||
'visit_datetime', | ||
'added_by', | ||
]; | ||
|
||
/** | ||
* The attributes that should be hidden for serialization. | ||
* | ||
* @var array<int, string> | ||
*/ | ||
protected $hidden = []; | ||
|
||
/** | ||
* The attributes that should be cast. | ||
* | ||
* @var array<string, string> | ||
*/ | ||
protected $casts = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.