File: /var/www/html/owlcrm/app/Http/Controllers/PlanFeatureController.php
<?php
namespace App\Http\Controllers;
use App\Models\Plan;
use App\Models\PlanFeature;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Yajra\DataTables\Facades\DataTables;
use Validator;
class PlanFeatureController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
if ($request->ajax()) {
$search_keyword = '';
$request_data = $request->all();
if ($request->has('search_keyword')) {
$search_keyword = $request->search_keyword;
}
// avoid zero column as it's checkbox so we can't sort by it
if ($request->has('order') && $request->order[0]['column'] != 0) {
$sort_column_number = $request->order[0]['column'];
$sort_column_dir = $request->order[0]['dir'];
$sort_column_key = $request->columns[$sort_column_number]['data'];
}
$main_query = PlanFeature::query();
$query = $main_query;
if (!empty($search_keyword)) {
$query = $query->where('title', 'LIKE', '%' . $search_keyword . '%');
}
$data = $query->get();
$count_total = $main_query->count();
$count_filter = $count_total;
return DataTables::of($data)
->addColumn('plan_id', function ($row) {
return $row->plan->name;
})
->addColumn('title', function ($row) {
return $row->title;
})
->addColumn('description', function ($row) {
return $row->description;
})
->addColumn('action', function ($row) {
if ($row->is_admin !== 1) {
return view('users.plan_feature.partially.delete', compact('row'));
}
})
->rawColumns(['action'])
->with([
"recordsTotal" => $count_total,
"recordsFiltered" => $count_filter,
])
->make(true);
}
return view('users.plan_feature.index');
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
$plans = Plan::all();
return view('users.plan_feature.create', compact('plans'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'plan_id' => 'required|exists:plans,id',
'title' => [
'required', 'string',
Rule::unique('plan_features')->where(function ($query) use ($request) {
return $query->where('plan_id', $request->input('plan_id'));
})
],
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$plan_feature = new PlanFeature();
if (isset($plan_feature)) {
$plan_feature->plan_id = $request->plan_id;
$plan_feature->title = $request->title;
$plan_feature->description = $request->description;
$plan_feature->save();
return redirect()->route('planfeature.index')->with('success', 'Plan Feature Add Successfully');
} else {
return back()->with('error', 'Plan Feature does not exist');
}
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
$plan_feature = PlanFeature::findorFail($id);
return view('users.plan_feature.show', compact('plan_feature'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(string $id)
{
$plan_feature = PlanFeature::findorFail($id);
$plans = Plan::all();
return view('users.plan_feature.edit', compact('plan_feature', 'plans'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, string $id)
{
$validator = Validator::make($request->all(), [
'title' => [
'required', 'string',
Rule::unique('plan_features')->where(function ($query) use ($request) {
return $query->where('plan_id', $request->input('plan_id'));
})
],
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
}
$plan_feature = PlanFeature::find($id);
if (isset($plan_feature)) {
$plan_feature->plan_id = $request->plan_id;
$plan_feature->title = $request->title;
$plan_feature->description = $request->description;
$plan_feature->save();
return redirect()->route('planfeature.index')->with('success', ' Plan Feature has been updated successfully');
} else {
return back()->with('error', 'Plan Feature does not exist');
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(string $id)
{
$plan_feature = PlanFeature::find($id);
if (isset($plan_feature)) {
$plan_feature->delete();
return redirect()->back()->with('success', 'Plan Feature has been deleted successfully.');
} else {
return back()->with('error', 'Plan Feature does not exist');
}
}
}