Laravel Practice

Create Project :
composer create-project --prefer-dist laravel/laravel blog
Start Development Server :
php artisan serve
Make Model With Migration :
php artisan make:model Book -m
Model class Coustomization :
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model
{
    // define table name
    protected $table = "books";
// define which column fill by data
protected $fillable = ['name'];
    // define primary Key
    protected $primaryKey = 'code';
    // define incrementing primary key
    public $incrementing = false;
    // automatically manage created and updated column 
    public $timestumps = false;

    // $book->date->format('m-d-Y');
    // protected $dateFormate = "U"; 
    protected $dateFormat = 'Y-m-d';

    // change created_at And updated_at column name
    const CREATED_AT = 'creation_date';
    const UPDATED_AT = "modify_date";
    // change database Connection Name to use default connection  no need below line 
    protected $connection = "sqlsrv";

   
//   set default attribute for specific column
protected $attributes = ['country'=>"Bangladesh"] ;

}

Retrieving Data From Models :

Get All Data And Print Data :

// Get All Data From Model And Store In a Variable You Can Pass It Any Where
$books = App\Books::all();
// Print Data 
foreach ($books as $value) {
    echo $value->code;
}

Get Data With Specific Condition And Filtering :
$book = App\Book::where('column','value')->orderBy('code','desc')->take(10)->get();

Get A Fresh And Update Data :

$book = App\Book::where('code',14)->first();
$freshdata = $book->fresh(); // you can use refresh() instant offresh()

Get Number Of Record :

    public function username(){
        $user = User::get()->take(10);
      echo $user;
    }

Delete An Record :

  public function username(){
     $delete = User::where('name','ashik')->delete();
     return  $delete;
    }

Published by

Unknown's avatar

Nusrat Faria

I Am A Web Developer And A Android Developer. This Is My Personal Blog So Noted My Work For Helping People .

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.