How To Fetch Data Using Model Name With Example Using Laravel

admin_img Posted By Bajarangi soft , Posted On 23-09-2020

In Laravel, Model is a class that represents the logical structure and relationship of underlying data table. In Laravel, each of the database table has a corresponding “Model” that allow us to interact with that table. Models gives you the way to retrieve, insert, and update information into your data table

How To Fetch Data Using Model Names In Laravel With Example

Retrieving Models

Once you have created a model and its associated database table, you are ready to start retrieving data from your database. 

To learn more about creating model visit our Bajarangisoft site.

Example(1)

<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class demo
 * @package App\Models
 * @version September 03, 2020, 6:44 pm UTC
 *
 * @property string name
 * @property string desc
 */
class demo extends Model
{
    use SoftDeletes;

    public $table = 'demos';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'name',
        'desc'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'id' => 'integer',
        'name' => 'string',
        'desc' => 'string'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [

    ];
}
 

You can see above code which is implement for demo model .demo model have different columns with datatype and it connected to table demos which is in database.

Now you can use model in controller to fetch data 

<?php
namespace App\Http\Controllers;

use App\Models\demo;
use Illuminate\Http\Request;

class demoController extends AppBaseController
{
     
  public function index(){
        $demo=demo::all();
        return view('demo',compact('demo'));
   }
}


above example retrieving data from database using demo model name and all data store in $demo variable that variable returns to demo. blade.php file by the help of route

<!DOCTYPE html>
<html>
<head>
    <title>Laravel </title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>

<div class="container">
    <br>
    <br>
    <div class="text-center">
        <h1>Laravel</h1>
    </div>
    <br>
    <br>

    <div class="well ">
        <div class="table-responsive">
            <table class="table table-bordered">
                <thead>
                <tr class="heading">
                    <th>Name</th>
                    <th>Description</th>
                </tr>
                </thead>
                <tbody>
                @foreach ($demo as $de)
                    <tr>
                        <td>{!! $de->name !!}</td>
                        <td>{!! $de->desc !!}</td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            {!! $demo->links('pagination') !!}
        </div>

    </div>
    <br>
</div>
</body>
</html>

Web.php

Route::get('Laravel','demoController@index');


Adding Additional Constraints
The Eloquent all method will return all of the results in the model's table. Since each Eloquent model serves as a query builder, you may also add constraints to queries, and then use the get method to retrieve the results:

$demos = App\demo::where('active', 1)
->orderBy('name', 'desc')
->take(10)
->get();


Refreshing Models
You can refresh models using the fresh and refresh methods. The fresh method will re-retrieve the model from the database. The existing model instance will not be affected:

$demo = App\demo::where('number', '900')->first();

$freshdemo = $demo->fresh();


The refresh method will re-hydrate the existing model using fresh data from the database. In addition, all of its loaded relationships will be refreshed as well:

$demo = App\demo::where('number', '900')->first();

$demo->number = '456';

$demo->refresh();

$demo->number; // "900"

 

Related Post