first initial
This commit is contained in:
178
app/Http/Controllers/DealerController.php
Normal file
178
app/Http/Controllers/DealerController.php
Normal file
@@ -0,0 +1,178 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Dealer;
|
||||
use App\Models\Menu;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
// use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Yajra\DataTables\DataTables;
|
||||
|
||||
class DealerController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$menu = Menu::where('link', 'dealer.index')->first();
|
||||
abort_if(Gate::denies('view', $menu), 403, 'Unauthorized User');
|
||||
|
||||
if ($request->ajax()) {
|
||||
$data = Dealer::leftJoin('users as u', 'u.id', '=', 'pic')->select('u.name as pic_name', 'dealers.*');
|
||||
return Datatables::of($data)->addIndexColumn()
|
||||
->addColumn('action', function($row) use ($menu) {
|
||||
$btn = '';
|
||||
if($row->pic != null) {
|
||||
|
||||
if(Auth::user()->can('delete', $menu)) {
|
||||
$btn .= '<button class="btn btn-danger btn-sm btn-bold" data-action="'. route('dealer.destroy', $row->id) .'" id="destroyDealer'. $row->id .'" onclick="destroyDealer('. $row->id .')"> Hapus </button>';
|
||||
}
|
||||
|
||||
if(Auth::user()->can('update', $menu)) {
|
||||
$btn .= '<button class="btn btn-warning btn-sm btn-bold" id="editDealer'. $row->id .'" data-url="'. route('dealer.edit', $row->id) .'" data-action="'. route('dealer.update', $row->id) .'" onclick="editDealer('. $row->id .')"> Edit </button>';
|
||||
}
|
||||
}else{
|
||||
if(Auth::user()->can('delete', $menu)) {
|
||||
$btn .= '<button class="btn btn-danger btn-sm btn-bold" data-action="'. route('dealer.destroy', $row->id) .'" id="destroyDealer'. $row->id .'" onclick="destroyDealer('. $row->id .')"> Hapus </button>';
|
||||
}
|
||||
|
||||
if(Auth::user()->can('update', $menu)) {
|
||||
$btn .= '<button class="btn btn-warning btn-sm btn-bold" id="editDealer'. $row->id .'" data-url="'. route('dealer.edit', $row->id) .'" data-action="'. route('dealer.update', $row->id) .'" onclick="editDealer('. $row->id .')"> Edit </button>
|
||||
<button class="btn btn-success btn-sm btn-bold" data-action="'. route('dealer.picstore', $row->id) .'" id="addPic'. $row->id .'" data-url="'. route('dealer.edit', $row->id) .'" onclick="addPic('. $row->id .')"> Tambahkan PIC </button>';
|
||||
}
|
||||
}
|
||||
|
||||
return $btn;
|
||||
})
|
||||
->rawColumns(['action'])
|
||||
->make(true);
|
||||
}
|
||||
|
||||
$mechanics = User::where('role_id', 3)->get();
|
||||
return view('back.master.dealer', compact('mechanics'));
|
||||
}
|
||||
|
||||
public function pic_store(Request $request, $id)
|
||||
{
|
||||
$menu = Menu::where('link', 'dealer.index')->first();
|
||||
abort_if(Gate::denies('update', $menu), 403, 'Unauthorized User');
|
||||
|
||||
Dealer::find($id)->update([
|
||||
'pic' => $request->pic
|
||||
]);
|
||||
|
||||
$response = [
|
||||
"status" => 200,
|
||||
"message" => "Data updated successfully"
|
||||
];
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$menu = Menu::where('link', 'dealer.index')->first();
|
||||
abort_if(Gate::denies('create', $menu), 403, 'Unauthorized User');
|
||||
Dealer::create($request->all());
|
||||
|
||||
$response = [
|
||||
"status" => 200,
|
||||
"message" => "Data created successfully"
|
||||
];
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param \App\Models\Dealer $dealer
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show(Dealer $dealer)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*
|
||||
* @param \App\Models\Dealer $dealer
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function edit(Dealer $dealer)
|
||||
{
|
||||
$menu = Menu::where('link', 'dealer.index')->first();
|
||||
abort_if(Gate::denies('view', $menu), 403, 'Unauthorized User');
|
||||
$dealer = Dealer::find($dealer->id);
|
||||
$response = [
|
||||
'data' => $dealer,
|
||||
'status' => 200,
|
||||
'message' => 'get data successfully'
|
||||
];
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \App\Models\Dealer $dealer
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, Dealer $dealer)
|
||||
{
|
||||
$menu = Menu::where('link', 'dealer.index')->first();
|
||||
abort_if(Gate::denies('update', $menu), 403, 'Unauthorized User');
|
||||
Dealer::find($dealer->id)->update($request->all());
|
||||
|
||||
$response = [
|
||||
"status" => 200,
|
||||
"message" => "Data updated successfully"
|
||||
];
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param \App\Models\Dealer $dealer
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy(Dealer $dealer)
|
||||
{
|
||||
$menu = Menu::where('link', 'dealer.index')->first();
|
||||
abort_if(Gate::denies('delete', $menu), 403, 'Unauthorized User');
|
||||
Dealer::destroy($dealer->id);
|
||||
|
||||
$response = [
|
||||
"status" => 200,
|
||||
"message" => "Data deleted successfully"
|
||||
];
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user