How to use Eloquent in Laravel

in #utopian-io7 years ago (edited)

laravel-consultas-sql.jpg
Image-source

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.
Screenshot_9.png

Screenshot_10.png
I have a structure table and columns. id, name, desc, created_at, updated_at

Screenshot_11.png
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

Screenshot_12.png
Screenshot_13.png

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.
Screenshot_15.png

blog.png

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




Screenshot_18.png

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

Sort:  

Hey @alfarisi94 I am @riyo.s94. I have just upvoted you!

untung di steemit :)

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @alfarisi94 I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 56586.53
ETH 2392.96
USDT 1.00
SBD 2.30