Home » Laravel

Controller in Laravel

Laravel Controllers: In this tutorial, we are going to discuss how to work with Laravel: Here, we will see how to make controller in Laravel?
Submitted by Bharti Parmar, on November 18, 2019

For further process we need to know something about it,

  • You can say that the Controller is the entry point of any project or any web-based application because you can assume it's meaning by the name itself.
  • We make a controller to control our project. It receives HTTP requests, communicates with models and manages the views of our project.

Controller in Laravel

ADD the postcontroller with --resource parameters on CMD,

    $ php artisan make:controller PostController --resource

Path: "app/http/controller/PostController.php"

In postcontroller, we use our model (post) model

  • use "App\post" (on top of the file)
  • public function index{}
Laravel Controller

Example: PostController.php code for index method.

use App\Post;
class PostController extends Controller
{
	// Display a listing of the resource.
	public function index()
	{
		$posts = Post::latest()->paginate(5);
		return view('posts.index',compact('posts'))->with('i',(request()->input('page',1) -1) *5);
	}
}
  • 'index' method will show all data from the database to our index view.
  • 'paginate' method to sort data in the view.
  • The controller has created, store, show, update and destroy method with index method. We will also be using these methods in the upcoming article with an example or with creating a form.
  • After creating a route declaration in route.php in the controller directory.
  • In the controller directory, the auth folder exists which is pre-defined in Laravel for reset password, Login, and Register.

Conclusion:

In this article, we have learnt about the Controller in Laravel. I hope you understood the concept, we will be knowing more about it in the upcoming articles. Have a great day! Happy Learning!







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.