A useful resource controller is utilized in Laravel to carry out CRUD operations simply. When you’ll create a useful resource controller utilizing artisan command from the terminal then it would create all crucial strategies contained in the controller associated to CRUD operations. It handles all HTTP requests for the applying and requires a single line of code for CRUD routes. How one can create a useful resource controller and carry out CRUD operations in Laravel are proven on this tutorial.

Prerequisite:

You’ll require a desk within the database the place the Laravel venture is related. You possibly can create a desk by migrate command or manually. I’ve used the customers desk on this tutorial to do the CRUD operation utilizing the useful resource controller. The desk is empty now. The construction of the desk is proven under.

Controllers of Laravel Resource-Linux Hint

Create a Useful resource Controller:

Run the next command from the terminal to create the useful resource controller named UserController.

$ php artisan make:controller UserController –resource

Controllers of Laravel Resource-Linux Hint

Should you open the controller from any editor you will notice the next codes are already written within the controller. Seven strategies are created contained in the controller routinely for doing the CRUD operations.

{
/**
* Show a list of the useful resource.
*
* @return IlluminateHttpResponse
*/
public operate index()
{
//
}

/**
* Present the shape for creating a brand new useful resource.
*
* @return IlluminateHttpResponse
*/
public operate create()
{
//
}

/**
* Retailer a newly created useful resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public operate retailer(Request $request)
{
//
}

/**
* Show the desired useful resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public operate present($id)
{
//
}

/**
* Present the shape for modifying the desired useful resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public operate edit($id)
{
//
}

/**
* Replace the desired useful resource in storage.
*
* @param IlluminateHttpRequest $request
* @param int $id
* @return IlluminateHttpResponse
*/
public operate replace(Request $request, $id)
{
//
}

/**
* Take away the desired useful resource from storage.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public operate destroy($id)
{
//
}
}

Useful resource Routes:

Add the next route for the UserController to create useful resource routes within the internet.php file.

Route::useful resource(‘customers’, ‘UserController’);

Now, run the next command from the terminal to examine the present route record from the online.php file.

The next route info is generated for utilizing the Route::useful resource() methodology. seven routes are created for calling seven strategies. The makes use of of those routes are defined later within the subsequent a part of this tutorial.

Controllers of Laravel Resource-Linux Hint

Add the next traces at the start of the UserController.php file to import the Person mannequin, Hash service for hashing the password, and DB service for database operations.

use AppUser;
use Hash;
use DB

Insert Report:

Modify the create() methodology of UserController with the next code. Right here, an object of the Person mannequin is created to insert the brand new data. After assigning the obligatory values, the save() operate is named to insert the brand new report into the scholars desk.

public operate create()
{
$person = new Person;

$user->title = ‘fahmida’;
$user->e mail = ‘[email protected]’;
$user->password = Hash::make(‘12345’);
strive
$user->save();
echo “Report is inserted”;

catch (IlluminateDatabaseQueryException $e) {
echo “Duplicate entry”;
}
}

The path to name the create() methodology of UserController is ‘customers/create’. Run the next URL from the browser. If the report is inserted correctly then the next output will seem.

http://localhost/laravelpro/public/customers/create

Controllers of Laravel Resource-Linux Hint

Now, if the examine the desk from the database then you’ll get the next output.

Controllers of Laravel Resource-Linux Hint

View All Data:

Modify the index() methodology of UserController with the next code to retrieve all data from the customers desk and show the values of title and e mail.

public operate index()
{
$customers = DB::choose(‘choose * from customers’);
foreach($customers as $person)

echo “Identify: $user->title”;
echo ”
Electronic mail:$user->e mail”;

}

The path to name the index() methodology of UserController is ‘customers’. Run the next URL from the browser.

http://localhost/laravelpro/public/customers

The next output will seem.

Controllers of Laravel Resource-Linux Hint

Choose Particular Report:

Modify the present() methodology of UserController with the next code to retrieve a report from the customers desk that accommodates 1 within the id discipline and show the worth of the title.

public operate present($id)

The path to name the present() methodology of UserController is ‘customers/{id}’. Run the next URL from the browser.

http://localhost/laravelpro/public/customers/1

The next output will seem.

Controllers of Laravel Resource-Linux Hint

Replace Report:

Two strategies are primarily outlined within the useful resource controller to replace a database report. These are edit() and replace() the place the info of the report updates utilizing any edited kind. However no edit kind is used right here. So, solely the edit() methodology is used to replace a specific report of the scholars desk. The present worth of a specific report is printed earlier than and after executing the replace question.

public operate edit($id)

$person = DB::choose(‘choose * from customers the place id=’.$id);
echo “The present e mail of the person is “. $person[0]->e mail.”
“;

$e mail = ‘[email protected]’;
$person = DB::choose(“Replace customers set e mail=’$e mail’the place id=”.$id);

$person = DB::choose(‘choose * from customers the place id=’.$id);
echo “The e-mail of the person after replace is “. $person[0]->e mail;

The path to name the edit() methodology of UserController is ‘customers/{id}/edit’. Run the next URL from the browser.

http://localhost/laravelpro/public/customers/1/edit

The next output will seem.

Controllers of Laravel Resource-Linux Hint

Delete Report:

destroy() methodology is outlined to delete any report from the desk. However the route for deleting the report passes from one other methodology. Right here, I’ve used the present() methodology to create a hyperlink for deleting the report that’s added on the finish of the tactic.

public operate present($id)

Controllers of Laravel Resource-Linux Hint

Add the next code within the destroy() methodology to delete a specific report.

public operate destroy($id)

$person = DB::choose(‘Delete from customers the place id=’.$id);
echo “The report is deleted”;

Add the route in internet.php file for calling the destroy() methodology.

Route::get(‘/customers/delete/{id}’, ‘[email protected]’);

After clicking the delete hyperlink the next output will seem.

Controllers of Laravel Resource-Linux Hint

Conclusion:

The usage of the useful resource controller and the idea of the route useful resource are defined intimately on this tutorial by utilizing a CRUD operation. I hope, it would assist the brand new Laravel customers to implement CRUD operation simply of their venture.

php artisan make:controller,laravel __construct,laravel delete controller,use __construct in laravel,create model in laravel,command "make:controller" is not defined.,delete controller laravel,php artisan make:controller in folder,laravel post tutorial,how to create new view in laravel,make:controller in laravel 7,how to call controller in laravel,where in laravel controller,laravel store,laravel resource path,resource routing in laravel,laravel controller namespace,laravel edit