v1.5.24bb Added comments

This commit is contained in:
2021-05-21 12:28:38 +02:00
parent 20fe7dbcee
commit 3b0b2ff759
7 changed files with 99 additions and 8 deletions
@@ -12,9 +12,13 @@ class ContactController extends Controller
{
public function __construct()
{
//Check authentication and languages settings
$this->middleware([ "auth" ]);
$this->middleware([ "lang" ]);
//The middleware is being run just before the pages is getting loaded.
//We use this middleware to chek if a user has the right permission/authentication to view the pages
$this->middleware([ "check.auth:contact.show" ])->only("show", "index");
$this->middleware([ "check.auth:contact.create" ])->only("create", "store");
$this->middleware([ "check.auth:contact.edit" ])->only("edit", "update");
@@ -26,10 +30,19 @@ class ContactController extends Controller
*
* @return \Illuminate\Http\Response
*/
//Controller for the contact index pages, all backend work for the contact index pages is done here.
public function index(Request $request)
{
//We use the Contact model to query all information from the contact form in the database
$contact = Contact::query()->get();
/*
Here we return/pass the contact variable containing all the database information to the
contacts.index pages as a parameter. The "contacts" in green text is the name you have to use
on the index pages to get the data from the database, you can name it what ever you want, but its best
best practice to give it a describing name.
*/
return Response::detect("contacts.index", [ "contacts" => $contact]);
}
@@ -38,10 +51,11 @@ class ContactController extends Controller
*
* @return \Illuminate\Http\Response
*/
/*The create controller, all it has to do is redirect you to the contacts create pages*/
public function create()
{
return Response::detect("contacts.create");
}
/**
@@ -50,8 +64,11 @@ class ContactController extends Controller
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
//The store controller is used to store user data.
public function store(Request $request)
{
//Validate that the user gave a contactname, title, email, phone and make sure its required and only contains max 255.
$requestContact = $request->validate([
"contactname" => "required|max:255",
"title" => "required|max:255",
@@ -59,13 +76,20 @@ class ContactController extends Controller
"phone" => "max:255",
]);
//Make a new instance of the contact modal and pass $requestContact to be saved in the database
$contact = new Contact($requestContact);
//Set phonetimes to what the user entered, we dont validate thats why it has its on "save" function .
$contact->phonetimes = $request->phonetimes;
//Call the save function.
$saved = $contact->save();
//If it couldn't save redirect to the contacts.store view
if(!$saved){
return redirect()->route("contacts.store");
}else{
/*If it did get saved query all information from the database and redirect to contacts.index with the parameter
contacts
*/
$contact = Contact::query()->get();
return redirect()->route("contacts.index", ['contacts' => $contact]);
}
@@ -78,6 +102,8 @@ class ContactController extends Controller
* @param int $id
* @return \Illuminate\Http\Response
*/
//The show controller is used to show a single entity from the database based on the id its get
public function show($id)
{
return Response::detect("contacts.show", [ "contacts" => $id]);
@@ -90,6 +116,10 @@ class ContactController extends Controller
* @param int $id
* @return \Illuminate\Http\Response
*/
/*Edit controller finds all the data on the specific id passed to it, finds that entity from the database and
that match with the id and redirect you to the edit pages.
*/
public function edit($id)
{
$contact = Contact::find($id);
@@ -103,17 +133,26 @@ class ContactController extends Controller
* @param int $id
* @return \Illuminate\Http\Response
*/
//Update controller
public function update(Request $request, $id)
{
//request all the data from the database
$data = $request->all();
//Find the entity in the database that match the id
$contact = Contact::find($id);
//Call the update function
$contact->update($data);
//Call the save function
$saved = $contact->save();
//If it couldn't save redirect to the contacts.update view
if(!$saved){
return redirect()->route("contacts.update", [ "contacts" => $contact ]);
}else{
/*If it did get saved query all information from the database and redirect to contacts.index with the parameter
contacts
*/
$contact = Contact::query()->get();
return redirect()->route("contacts.index", ['contacts' => $contact]);
}
@@ -127,6 +166,7 @@ class ContactController extends Controller
* @return \Illuminate\Http\RedirectResponse
* @throws \Exception
*/
//The destroy controller finds the id and calls a delete function, after that it redirects to contact index.
public function destroy($id)
{
$contact = Contact::find($id);