AngularJS and Laravel: Finishing Building a CRM

When creating a single-page app we should use some kind of framework to do some of the job for us so we can focus on the actual functionality. 

AngularJS fits here perfectly, because features like dynamic dependency injection and bi-directional data binding are just great. Sometimes we also require some kind of server. If you've chosen PHP then Laravel may be your best option, as it's easy to work with and pretty powerful.

In this part of the tutorial, we will build the front-end of our application using AngularJS. Angular is a really unique framework. Instead of abstracting the HTML or providing some way of DOM manipulation they extended the HTML to deal with the fact that it was surely not designed to work with dynamic data. 

Because of that Angular may need some more learning than other frameworks, but it's really worth the time spent.


Preparation

Before we start programming our front-end ,we have to modify the Laravel part a bit. Go to app/views, delete the example stuff that is there and create the file named home.php. Now let's create the layout. 

Start with the DOCTYPE and the html tag:

As you can see we are already using some AngularJS stuff - the ng-app directive. This tells Angular to use the module named app for this application (we will define it later). After that, add the head with a title and CSS:

Now you can put the script tags in with Angular, it's routing module and our app:

 This directive tells Angular to put the template that was requested into that element.

After that we only need to add a route to show the template (in app/routes.php). Add this before the routes for controllers:

Now if you start the server (with php artisan serve) you should see our basic layout when you navigate to http://localhost:8000/ in your browser:

The Style

This article will not be focusing on anything related to CSS, but to make the app more pleasing to the eye while you are developing we will add some style to it. Go to the public/ directory of your app (it's next to app/) and create the style.css with this code in it:

Now open the app in your browser and the layout should be centered with a nicer font in the heading:

Basic Application Structure

We will start with a module declaration. Modules in Angular are pretty much the same thing as in any AMD library, but with the addition of dependency injection which is a really useful thing, as you will see. Here is the declaration of our app module:

The syntax is simple - first goes the module's name, and then the array of dependencies - we will only be using ngRoute here to handle the navigation, which will go next.

Routing

The routing is defined in the module's config() method:

Here is when the dependency injection kicks in for the first time - our callback will take $routeProvider as the only argument, and this module will be injected by Angular.

You have to make sure that the argument names are exactly the same as the module names, because Angular uses them to match the appropriate modules.

Now let's actually use the $routeProvider to set up the routes:

As you can see to define a route you have to call the when() method of the provider (note that they can be chained). 

The first parameter is the URI, and the second one is an object with route's options. Here, we attach appropriate controllers and templates to every route. In the second one we also use :id at the end to mark a route parameter that we will use later. The otherwise() method defines what will happen if any other URI is accessed.


Factory

Before we write the controllers, we have to create a thing called factory. factory is a function that returns a service, which is helpful if you want to separate any data getting/setting functions from the controllers (which is of course what you always want to do). We define it using the factory() method of the module:

The first parameter is the service's name, and the second one is a function which returns the service that will be created using this factory.

We will use the $http module to access our server using Ajax. It provides shortcut methods for all HTTP methods and each of them returns a promise (if you don't know what that is take a look here and here).

We have to return the service as an object with all of the methods that will be used in our controllers:

The first one will GET all of the customers so we can show them in a list:

Second one will GET only one customer by his id:

Third will POST the request to add user to the database:

The second argument in $http.post() is the data that will be sent to the server.

Next one will DELETE the customer with the id provided:

Now there will be few similar ones for transactions. One to get all of them:

One to add new one:

And one to delete:

Customers Controller

The controllers in Angular are (like the name suggests) a way to control the application's behaviour. We will have one for every template. First we will make one for the main page. Start by defining it:

The second parameter here is the constructor function for the controller. It's first argument ($scope) is the link between the DOM and the controller. It's the heart of Angular's bidirectional data binding. The second one is the service from the factory that we created earlier.

Getting the List

Now we will get the customers list from the server using our service:

All promises in Angular provide the success() and error() methods which can be used to add appropriate callbacks. Now let's define the function that will parse the incoming data to show it on the page:

Yes, that is all it takes to feed the template with data. No need for any innerHTML/appendChild()-ish code.

Adding New Customers

We also need to provide the ability to add and remove customers. First let's create an object in the scope where we will hold the data of the new customer:

This way we can avoid accessing DOM when the user adds a customer. Now the function that will actually add the customer: 

Since the user full name will be displayed in the table the input for it will be the same, so we have to split it to get first and last name:

Now we call the appropriate function from our factory with the data from $scope:

After that we add success and error listeners to the promise returned:

Let's define the success callback first:

The data argument holds the response's text. We have to clear the $scope.error variable:

Push the newly added customer to $scope.customers:

And set $scope.newCustomer to its initial state to clear the inputs:

The error callback will just set the $scope.error variable to the text received from the server:

Removing Customers

The function to remove the customer will take his id as a parameter:

We will also display a confirmation box so the user has a chance to cancel the action:

If the user is sure he wants to continue, we delete the customer:

The callback here will have to remove the customer from $scope.customers using the id gotten from the server:

The Result

The complete code should look like this:

Customers Template

Now to actually show the data to our users we have to create a template. We defined it in the route to be ./templates/customers.html, so create the public/templates directory and the customers.html file in it.

First add the heading so the user knows where he is:

Next we need a table with a nice heading to show the data:

Now add the tbody element. And here is where Angular's magic comes in again. Using the ng-repeat directive we tell Angular to repeat the element:

The syntax is as in JavaScript's for...in loop. Now we can access the customer variable to get all the data we need. In Angular you insert variables using double curly braces:

There is also a ng-click directive which will act as a onclick event callback, we use it to add the ability to remove customers. Next there is a footer with inputs so the user can add new customers:

We use the ng-model directive to bind appropriate variables from the scope to the inputs, so they are updated every there is a change in the inputs' value.

The last thing to do is to show a error message if there is any. To achieve that we will use ng-show directive which will only show the element if specified expression is true:

That's it! Now you can open the app in your browser and you should see this:

You can add new customer by clicking the plus sign on the right-bottom corner of the table.


Customer Controller

Now let's create a controller for a single-customer view:

Getting the Data

We get the customer's data using $routeParams module which holds all of the route parameters like the :id we specified earlier:

The callback is pretty much the same as in the CustomersController. Now let's get all of the customer's transactions:

The callback is a bit different than the last one because we also want to show the sum of the transactions' amounts. We need to use parseFloat() because Laravel sends floats as strings.

Adding New Transactions

The code will be very similar to the one used to create new customers:

The only difference is that we add the customer's id to the data so the server knows whos transaction it is. The success callback is also a bit modified, because we have to parse the float before adding it to the $scope and we have to add the amount to our sum.

Removing Transactions

The code for removeTransaction() function is almost identical to removeCustomer being different only in the variable names:

The Result

Whole controller should look like this:

Customer Template

The template for single customer has no new Angular's directives, so just make a file named customer.html in public/templates/ and place this code there:

Note that we are using toFixed(2) to round the floats so they have only two decimal fields, because the way Laravel handles floats in JSON.

Now you can open the browser and click on one of the customers you created. You should see the controller and template in action:


Conclusion

Now if you've added some functionality after the first part, including it in the front-end should be a matter of adding few lines of code here and there.

I hope that after you've done reading the article and your app is finished and working you will start thinking how you could create single-page applications without AngularJS and any PHP apps without Laravel. Let me know if you had any problems with any of the frameworks presented here.

Tags:

Comments

Related Articles