File: /var/www/vhost/disk-apps/dev-beg.teky.com.co/app/Http/Controllers/LocationsController.php
<?php
namespace App\Http\Controllers;
use App\Zone;
use App\Storage;
use App\Location;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class LocationsController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($zone_id,$storage_id)
{
abort_unless(\Gate::allows('location_access'), 403);
$zone = Zone::where('id', $zone_id)->first();
$storage = Storage::where('id', $storage_id)->first();
$locations = Location::where('storage_id', $storage_id)->with('storage')->orderBy('code', 'asc')->get();
return view('locations.list', compact('locations', 'storage_id','zone_id', 'zone', 'storage'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create($zone_id,$storage_id)
{
abort_unless(\Gate::allows('location_create'), 403);
$zone = Zone::where('id', $zone_id)->first();
$storage = Storage::where('id', $storage_id)->first();
return view('locations.create', compact('storage_id','zone_id', 'zone', 'storage'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
abort_unless(\Gate::allows('location_create'), 403);
try {
if (!Location::where('code', $request->input('code'))->first()) {
Location::create(array_slice($request->all(), 1));
return response(array("status" => true, "type" => "success", "title" => "", "message" => __('created_successfully'), "data" => null));
} else {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('already_exists', ['name' => $request->input('code')]), "data" => null));
}
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_creating'), "data" => null));
}
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
abort_unless(\Gate::allows('location_show'), 403);
return redirect()->back();
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($zone_id,$storage_id,$location_id)
{
abort_unless(\Gate::allows('location_edit'), 403);
$location = Location::findOrFail($location_id);
$storage = Storage::where('id', $storage_id)->first();
$zone = Zone::where('id', $zone_id)->first();
return view('locations.edit', compact('storage_id', 'location', 'zone_id', 'zone', 'storage'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request,$zone_id,$storage_id,$location_id)
{
abort_unless(\Gate::allows('location_edit'), 403);
try {
if (!Location::where([['id', '!=', $location_id], ['code', $request->input('code')]])->first()) {
Location::where('id', $location_id)->update(array_slice($request->all(), 2));
return response(array("status" => true, "type" => "success", "title" => "", "message" => __('updated_successfully'), "data" => null));
} else {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('already_exists', ['name' => $request->input('code')]), "data" => null));
}
} catch (\Throwable $th) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('error_updating'), "data" => null));
}
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($zone_id,$storage_id,$location_id)
{
abort_unless(\Gate::allows('location_destroy'), 403);
try {
Location::where('id', $location_id)->delete();
return response(array("status" => true, "type" => "success", "title" => "", "message" => __('deleted_successfully'), "data" => null));
} catch (\Illuminate\Database\QueryException $e) {
return response(array("status" => false, "type" => "error", "title" => "Oops...", "message" => __('delete_relation_data'), "data" => null));
}
}
}