Skip to content

Instantly share code, notes, and snippets.

@platinize
Created March 16, 2018 18:42
Show Gist options
  • Save platinize/eb6eaf3211276d6762a7829fdc564081 to your computer and use it in GitHub Desktop.
Save platinize/eb6eaf3211276d6762a7829fdc564081 to your computer and use it in GitHub Desktop.
Laravel_2
<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
class ModelBaseFunction
{
/**
* выборка всех пользователей
* @return Array
*/
public function getAll($tableName){
$all = DB::table($tableName)
->get();
return $all;
}
/**
* выбрать одну запись по id
* @return Array
*/
public function getOne($id, $tableName){
$one = DB::table($this->name_table)
->where('id', '=', $id)
->get();
return $one;
}
/**
* показать количество записей
* @return int
*/
public function getCount($tableName){
return DB::table($tableName)->count();
}
public function getColName($tableName){
return DB::table('INFORMATION_SCHEMA.COLUMNS')
->select('COLUMN_NAME')
->where('TABLE_NAME', '=', $tableName)
->get();
}
}
<?php
namespace App\Models;
use Illuminate\Support\Facades\DB;
use App\Models\ModelBaseFunction;
class ModelTable extends ModelBaseFunction
{
protected $name_table = 'qwe';
/**
* выборка всех пользователей из таблицы $name_table
* @return Array
*/
public function allGet(){
return $this->getAll($this->name_table);
}
/**
* выбрать одну запись по id из таблицы $name_table
* @return Array
*/
public function one($id = 1){
return $this->getOne($id, $this->name_table);
}
/**
* показать количество записей в таблице $name_table
* @return int
*/
public function count(){
return $this->getCount($this->name_table);
}
public function colums(){
return $this->getColName($this->name_table);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\ModelTable;
class TableController extends Controller
{
public function show(){
$table_class = new ModelTable();
return view('tables', ['all'=>$table_class->allGet(), 'one'=>$table_class->one(2),
'count'=>$table_class->count(), 'colums'=>$table_class->colums()]);
}
}
<p>AllEntry</p>
<table border="2">
<tr>
@foreach($colums as $entry)
@foreach($entry as $cell)
<td>{{$cell}}</td>
@endforeach
@endforeach
</tr>
@foreach($all as $entry)
<tr>
@foreach($entry as $cell)
<td>{{$cell}}</td>
@endforeach
</tr>
@endforeach
</table>
<p>One</p>
<table border="2">
<tr>
@foreach($colums as $entry)
@foreach($entry as $cell)
<td>{{$cell}}</td>
@endforeach
@endforeach
</tr>
@foreach($one as $entry)
<tr>
@foreach($entry as $cell)
<td>{{$cell}}</td>
@endforeach
</tr>
@endforeach
</table>
<p>Count = {{$count}}</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment