IMMREX7
<?php
namespace App\Http\Controllers\School\Library;
use Illuminate\Http\Request;
use App\Http\Controllers\School\SchoolController;
use DB;
use Auth;
class LocationController extends SchoolController
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$locations = \App\BookLocation::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idBookLocation', 'desc')->get();
return view('schools.library.locations', compact('locations'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
foreach ($request->locations as $key => $val) {
$rules['locations.' . $key . '.locName'] = 'unique:book_location,locName,NULL,idBookLocation,idSchool,' . Auth::guard('school')->user()->idSchool;
}
$message = [
'locations.*locName.unique' => 'Location Already Exist.',
];
$this->Validate($request, $rules, $message);
foreach ($request->locations as $var) {
$loc = new \App\BookLocation();
$loc->fill($request->all());
$loc->idSchool = Auth::guard('school')->user()->idSchool;
$loc->locName = $var['locName'];
$loc->save();
}
flash('Locations has been added successfully!!');
if ($request->ajax()) {
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
return redirect('school/booklocations');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
$locations = \App\BookLocation::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idBookLocation', 'desc')->get();
$loc = \App\BookLocation::where('idBookLocation', '=', $id)->first();
return view('schools.library.locations', compact('locations', 'loc'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
$loc = \App\BookLocation::where('idBookLocation', '=', $id)->first();
$rules = [
'locName' => 'required|unique:book_location,locName,' . $id . ',idBookLocation,idSchool,' . Auth::guard('school')->user()->idSchool,
];
$this->validate($request, $rules);
$loc->fill($request->all());
$loc->update();
return redirect('school/booklocations');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$loc = \App\BookLocation::where('idBookLocation', '=', $id)->first();
$loc->delete();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
}
Copyright © 2021 -