#Part4 laravel 5 Blog Tutorial : Model Eloquent & Displaying Data from Database

Create Blog Apps in Laravel 5.3 part 4 - To continue previews lessons about CRUD / create blog apps in laravel 5.3 project, this lesson will discuss about how to create and working with Model Eloquent  and how to Displaying Data from Database.

At the previews lessons, we has create database and connect it form laravel project, than we have configuration Restful Controllers and Routing System in our project, if you still confused, just read #Part2 laravel 5 Blog Tutorial : Database & Migration and part3 Restful Controllers and Routing System.

Model Eloquent & Displaying Data from Database

First, we need to create a Model in our project, Model will make a communication with database, create model using 'Artisan command' >>

php artisan make:model Blog

your model will be stored on App\Blog.php



namespace App;

use Illuminate\Database\Eloquent\Model;

class Blog extends Model
    protected $table ='blog_post';

"blog_post" is our table in database.

Next, in you Controller (BlogController) add Blog name spaces to declare our model. if you don't have BlogController.php, following the Part3 tutorial.

use App\Blog;

Next, add index function in our controller, we will fetch all data from database

Model Eloquent & Displaying Data from Database


public function index()
        // we will create index function
        // we need to show all data from "blog" table
        $blogs = Blog::all();
        // show data to our view
        return view('blog.index',['blogs' => $blogs]);        

Next, create new folder (i create a 'blog' folder) and index file in our view. that index file will stored on resources\views\blog\index.blade.php


<h1>My First Blog</h1>
@foreach ($blogs as $blog)
  <h2>{{ $blog->title }}</h2>
  <p>{{ $blog->description }}</p>

Now, try to access your project stored on : http://localhost:8080/blog , manual insert any data into your database before.

Video tutorial Part4 laravel 5 Blog

See you Next Lessons......

Recomended Tutorials


0 Response to "#Part4 laravel 5 Blog Tutorial : Model Eloquent & Displaying Data from Database"

Post a Comment