In this tutorial I will interact with the database in laravel. by using eloquent, interaction with our database will be more beautiful in terms of programming because the code is much clearer, and its methods have been abstracted, so we use them more comfortable and shorter. Let's just get started, how beautiful is eloquent.
What is Eloquent?
Eloquent is one of the laravel features to come in contact with the database, although we can also use query builder as well. Eloquent can be used not only in laravel. By using eloquent, our codes will be much easier to understand, and shorter. Not only that, eloquent can also manage relationships between our tables. with its interesting features, eloquent is widely used, but it takes time to learn it.
Make Database Example
In this tutorial I will have 1 Database test with table categories.
I have a structure table and columns. id, name, desc, created_at, updated_at
I have some fake data as an example. So ,later we will take data from database with eloquent.
Make Models
We have createad a table from our database, then we will create a model. What is model?,
Model is the "M" of the "MVC"(Model. View, Controller).The model is tasked to deal directly with the database. Models already connected in the database will be used or dialed via controller, that's the concept of MVC .
To make models in Laravel like this.
php artisan make:model blog : blog is the model name we created .The model name is up to you
We have successfully mebuat model, and we can open our text editor and go into laravel folder, we can see laravel automatically create a file with the name that we have defined that is blog
Description of the contents of the model file
This is the content of the blog.php file
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class blog extends Model
{
//
}
Laravel has automatically created a class named blog. For information, for example the name of our class blog, laravel automatically will search the table in the database with the name of blogs. If its class name is user, laravel will automatically search for table users in our database.
If you have a different table name with the model class. you can do like this, coincidentally in this tutorial my class and table name is different. You can added in the class { }
protected $table = "categories" ;
And automatically laravel also expectations if we have two columns, namely created_at and updated_at. And if your table does not have created_at and upadated_at, you can change it .
public $timestamps = false ;
Make Controller to Retrieve Data
We must to make a Controller , let's make it.
php artisan make:controller blogController : I created a controller with the name of blogController.
blogController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\blog;
class blogController extends Controller
{
public function index(){
$blogs = blog::all();
return view('welcome',compact('blogs'));
}
}
public function index(){ } : We created function index.php in blogController.php.
$blogs = blog::all() ; : This is Eloquent Syntax , this function to get all data from table categories.
return view('welcome',compact('blogs')) ; : We return view welcome.blade.php , and we pass blogs to the view.
Output data
I want to show the data we have passed. in view welcome.blade.php.
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
<style>
html, body {
background-color: #fff;
color: #4a4a4a;
font-family: 'Raleway', sans-serif;
font-weight: 600;
height: 100vh;
margin: 0;
}
</style>
</head>
<body>
<p>Welcome Eloquent !!</p>
<ul>
@foreach($blogs as $blog)
<li>{{$blog->name}} : {{$blog->desc}}</li><br>
@endforeach
</ul>
</body>
</html>
@foreach($blogs as $blog) : forech the object in $blogs . and initialize as $blog
< li > {{$blog->name}} : {{$blog->desc}}< /li > : Extract the data from object $blogs
We have successfully extracted the data from the database to the blade view. this is a simple way to use eloquent, in the next post I will discuss about the relationship between databases with eloquent, so much of me hopefully, this tutorial useful for you, thank you
Posted on Utopian.io - Rewarding Open Source Contributors