Posts

Laravel 5.2 Eloquent Insert Row or Add Record

Introduction to Eloquent Most of applications code is based on Object Oriented Design but the database contains tables and relations and return set of rows instead list of objects. So there is inhereit mismatch between application code and database artifacts. Different Lanaugage provide object relational mapping (ORM) technologies that fill this mismatch gap. Eloquent is ORM that is used by Laravel. Eloquent is used to interact with the database to perform select, insert, update and other database operations. We create model calsses corresponding to every table. Each Model class help in performaing database related operations with associate table. You must know what are migrations and how setup database for Laravel applications to proceed further. Create Model Class By default the model classes are located in 'App' folder. If you see you Laravel application's App folder you will see a User.php file, its already created model class by Laravel. Migration for users table are a...

How to Get Request Data in Laravel 5.2

Image
This post explain how to get request data in routes.php file and in Laravel 5.2 controllers. The request data includes the parameters submitted via URL query string e.g. /search?category=furniture, here category  is parameter name and furniture is parameter value. If a form is submitted via GET or POST, the data that is sent to server is also called request data. In short, request data is data that a request carries. When Laravel framework receive an HTTP request, before transferring request processing to controllers, the framework extract data from request and instantiate an object of type Request , which is Laravel provided class. The Request object contain all the data that HTTP request carries. The Laravel framework passes the object using dependency injection into controller methods. It is passed only if we have expressed our interest by declaring an argument of type Request in controller methods. After receiving the Request object as argument, we can read the parameter ...

How to Get Route Parameters in Controller of Laravel 5.2

Image
Requests in Laravel can contain data in two different ways i.e. as URL route parameters or as request parameters. This article explains how to get URL route parameters inside controller. The Route Parameters are those which are submitted as part of the URL Pattern. Assume users access the url /users/search/lahore.   Here we can treat "lahore" as route parameter for city. Inside Controller we must get city name in a variable so that we can query database for users living in Lahore. To get route parameters, we define appropriate URL pattern in routes.php file. Lets see the mapping for above URL in routes.php file and how we would receive the variable inside controller. Route::get('/users/search/{city}', 'UsersController@search'); We can receive the value of city placeholder inside controller by declaring just an argument in search method, as below: namespace App\Http\Controllers; class UsersController extends Controller { public function search($c...

How to Return View from Controller in Laravel 5.2

Image
In previous tutorial , I explained the different operations that a controller performs in Laravel. We also learned how to make hello world controller that return some text to user. In this post, I would explain how to make controllers that return view template. I would also explain how we can pass data to view templates from the controller. In HelloWorldController, we looked at how controller can return short string to browser. In real applications, controller render response using views which are defined outside the controllers. These views are located at /resources/views folder of your Laravel application. After a controller has done its working e.g. interaction with model etc. Controller select a view and send its contents to browser. Remember, views are just PHP files that contains PHP, HTML, CSS etc. To demonstrate above concepts, below I create UsersController. C:\wamp\www\laraveltutorials>php artisan make:controller UsersController Controller created successfully. H...

Laravel 5.2 Hello World Controller Example

Image
Lets first see what a controller is and what task it performs, then I would make a simple controller that prints hello world message to browser. Controller is core component in MVC architecture. A controller in Laravel perform three major tasks: Controller interpret the request i.e. extracting request and URL route parameters Controller interact with application persistence state, called Model. Model is not limited to database; interacting with caches, indexing servers, directory servers, file store, or even third party APIs is all part of persistence state related operations. Controller do not perform these operations itself, it calls other software module or components e.g. services or data access objects to communicate with persistence state. Based on URL, request & route parameters and results returned by Model, controller renders response to user. Controller render the response using one of these approaches: Controller passes data retrieved from model to view template...

Laravel Routing / Routes.php Tutorial with Examples

Image
All MVC Frameworks provide some mechanism to define rules that are used to map different requests to corresponding controllers. In Laravel MVC Framework, we define these rules in routes.php (located in http\Http folder) file to map different URL patterns to controllers. I assume you have created your first laravel project. Here I explain how to define mapping rules in routes.php file. Simplest Routing Rule Route::get('/hello-world', function () { return "Hello World"; }); Laravel provide a class with different static methods e.g. get, post etc. The method name tells about the type of HTTP request (get, post, ...) for which the rule is defined. The first parameter is URL Pattern, means what a user enter in the browser address bar after the context path or domain name. Here "/hello-world" pattern shows this rule would be used to process or delegate requests coming from domain-name/hello-world pattern. The 2nd parameter can be a string ...

Lucene Highlighter Tutorial with Example

Image
The post explains how to implement search terms Highlighter using Apache Lucene 5.1 along with example code. When users search, they want to search in minimum time. So the techniques that facilitate users to search fast are important for better user search experience, highlighter is one of those techniques. HighLighter performs two functions: It makes the terms bold in search result which were part of user query, so that user can identiy and quickly review the result. If your document text is long, Highlighter also select best fragment of text that contains the search keywords, so that user could read 2-3 lines of document to decide whether exploring the link further would help. Using Google, you must have noticed, Google highlight the query keywords making them bold and also select a particular fragment of text from the description that is stored in Google about that articles. As show below: Notice, there are three query terms: java, inheritance and bitspedia. In re...