Hello to all, welcome to therichpost.com. Today, In this post, I will tell you, Angular 8 with laravel 6 backend working example.
Both Angular 8 and Laravel 6 are the best on their platforms. Today, In this post, I will take Angular 8 as frontend and Laravel 6 as backend.
If you are new then you can check my old posts related to Laravel 6 and Angular 8.
Here is the images showing of Angular 8 frontend in which I am getting data from laravel 6.
Here are the complete code snippet for Angular 8 with laravel 6 backend working example and please use this carefully:
1. Let start, here are the basics commands to set Angular 8 into your pc:
$ npm install -g @angular/cli //Setup Angular8 atmosphere $ ng new angularlatest8 //Install New Angular App /**You need to update your Nodejs also for this verison**/ $ cd angularlatest8 //Go inside the Angular 8 Project
2. After all above setup, here is code, you need to add into your app.module.ts file:
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { HttpClientModule } from '@angular/common/http'; import { AppComponent } from './app.component'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, HttpClientModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
3. Here is the code, you need to add app.component.ts file:
This is laravel project path(http://localhost/blog/public/api/sample-restful-apis)where I have set the api from where I am getting the data.
import { Component } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { title = 'angular8restapi'; data = []; constructor(private http: HttpClient) { this.http.get('http://localhost/blog/public/api/sample-restful-apis').subscribe(data => { this.data.push(data); console.log(this.data); }, error => console.error(error)); } }
4. Here is the code you need to add into app.component.html file:
<table> <tr> <th>Name</th> <th>Domain</th> </tr> <tr *ngFor="let mydata of data[0]"> <td>{{mydata.name}}</td> <td>{{mydata.domain}}</td> </tr> </table>
5. Here is the code, you need add into app.component.css file:
table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%; } td, th { border: 1px solid #dddddd; text-align: left; padding: 8px; } tr:nth-child(even) { background-color: #dddddd; }
6. Here is the final code, you need to into your laravel 6 api.php file, where we will have the data to show in angular 8 application:
... Route::get('sample-restful-apis', function() { return response()->json([ [ 'name' => 'Google', 'domain' => 'Google.com' ],[ 'name' => 'Google', 'domain' => 'Google.com' ],[ 'name' => 'Google', 'domain' => 'Google.com' ],[ 'name' => 'Google', 'domain' => 'Google.com' ],[ 'name' => 'Google', 'domain' => 'Google.com' ] ]); }); ...
In the end don’t forget to run ng serve command to enjoy the final output.
If you have any query then please let me know or comment below.
Jassa Jatt
Thank you
Awesome post. The way you present the whole concept is incredible and easy to understand.
Thank you Ashwani.
dont u have angular and laravel complete blog site tutorials
Yes and I am making video tutorial on it and we share it soon.
please is there any tutorial of crud user ??
Hi, I am making with angular and php.
https://therichpost.com/angular-11-crud-add-edit-delete-tutorial/
hello sir my name is ganesh i am working on angular laravel developer in one of the mnc company i wanted to do freelancing website development whats should i do for that please guid me
Yes sure, I will guide you. You can see Angular templates on my site and implement it with Laravel backend.