Useful Laravel Snippets

Useful Laravel Snippets

How to check current installed version of Laravel ? 

php artisan --version

Last executed queries in Laravel 5

DB::getQueryLog();

Getting all tables inside a database using Laravel 

$tables = DB::select('SHOW TABLES');

dd($tables);

Injecting data With Blade

// categoriesCount.blade.php
@inject('categories','App\categories')
Total categories : {{ $categories->count(); }}

(or)

// routes.php
view::composer('categories',function($view) {
$view->with('categories','App\categories');
});

// categoriesCount.blade.php
Total categories : {{ $categories->count(); }}

**********

Named Route Groups

Route::get('admin/home',['as' => 'home',function(){
    echo 'admin home';
}]);

// view 

echo route('home'); // output : http://localhost/admin/home
Route::group(['prefix' => 'admin'],function(){
    Route::get('home',['as' => 'home',function(){
        echo 'admin home';
    }]);
});

// view 

echo route('admin.home'); // output : http://localhost/admin/home

***************

Get table column names as array from Eloquent model 

class NewMediaModel extends Model {
    public function getTableColumns() {
        return $this->getConnection()->getSchemaBuilder()->getColumnListing($this->getTable());
    }
}

**************

Show All Validation Errors in Laravel’s Blade View  

@if($errors->has())
    @foreach ($errors->all() as $error)
        <div>{{ $error }}</div>
    @endforeach
@endif

← Go Back Author: Niyaz