eliteregistry/app/Http/Controllers/ShipsController.php

87 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2021-04-07 05:03:54 +00:00
<?php
namespace App\Http\Controllers;
2021-04-08 06:55:46 +00:00
use App\Models\EliteShips;
2021-04-07 05:03:54 +00:00
use App\Models\Ships;
2021-04-08 15:39:57 +00:00
use Illuminate\Support\Arr;
2021-04-07 05:03:54 +00:00
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
2021-04-07 05:03:54 +00:00
use Illuminate\Support\Str;
use Illuminate\Http\Request;
class ShipsController extends Controller
{
public function index(){
$ships = \App\Models\Ships::sortable()->paginate(10);
2021-04-08 06:37:02 +00:00
$eliteShips = EliteShips::all();
2021-04-08 15:39:57 +00:00
$sortedEliteShips = Arr::sort($eliteShips, function($eliteShip)
{
// Sort ships by their name.
return $eliteShip->shipName;
});
return view('ships.index', ['ships'=>$ships,'eliteShips'=>$sortedEliteShips]);
2021-04-07 05:03:54 +00:00
}
public function add(Request $request){
2021-04-09 08:35:39 +00:00
$eliteShips = DB::table('elite_ships');
2021-04-07 05:03:54 +00:00
if($request->input('addShip')) {
try {
$ship = new Ships;
$ship->id = Str::uuid()->toString();
$ship->shipId = strtoupper(request('shipId'));
$ship->shipName = request('shipName');
$ship->shipOwner = request('shipOwner');
2021-04-07 08:07:09 +00:00
$ship->shipType = request('shipType');
2021-04-09 08:35:39 +00:00
// $ship->shipClass = $eliteShips->where('shipName', request('shipType'));
$ship->shipClass = $eliteShips->where('shipName', request('shipType'))->value('shipClass');
2021-04-07 05:03:54 +00:00
$ship->created_at = now();
$ship->save();
}catch(\Exception $e){
session()->flash('error', 'Failed to create record.');
2021-04-07 07:06:24 +00:00
Log::error($e);
2021-04-07 05:03:54 +00:00
return redirect('/');
}
session()->flash('success', 'Record created successfully.');
return redirect('/');
}
if($request->input('deleteShip')) {
try {
DB::table('ships')->where('id', $request->get('deleteShip'))->delete();
}catch(\Exception $e){
2021-04-07 07:06:24 +00:00
Log::error($e);
2021-04-07 05:03:54 +00:00
session()->flash('error', 'Failed to delete record.');
return redirect('/');
}
session()->flash('success', 'Record deleted successfully.');
return redirect('/');
}
if($request->input('editShip')) {
try {
DB::table('ships')->where('id', $request->get('editShip'))->update(array(
'shipId' => strtoupper(request('shipId')),
'shipName' => request('shipName'),
2021-04-07 08:07:09 +00:00
'shipOwner' => request('shipOwner'),
2021-04-09 08:35:39 +00:00
'shipType' => request('shipType'),
// 'shipClass' => $eliteShips->where('shipName', request('shipType')),
'shipClass' => $eliteShips->where('shipName', request('shipType'))->value('shipClass'),
2021-04-07 05:03:54 +00:00
));
}catch(\Exception $e){
2021-04-07 07:06:24 +00:00
Log::error($e);
2021-04-07 05:03:54 +00:00
session()->flash('error', 'Failed to update record. Is it possible that the ship ID is not unique?');
return redirect('/');
}
session()->flash('success', 'Record edited successfully.');
return redirect('/');
}
}
}