?php
//laravel5.6 語法 demo示例
namespace App\Http\Controllers;//命名該控制App空間下名稱
use Illuminate\Support\Facades\DB;//使用DB操作數據庫
use App\Http\Controllers\Controller;//繼承基礎控制器
class UserController extends Controller
{
/**
* 展示應用的用戶列表.
*
* @return Response
*/
public function index()
{
//DB使用為每種操作提供了相應方法:select(查),update(修改),insert(插入),delete(刪除),statement(聲明)
//建議占位符,其他框架通用性強
//原生sql寫法
$data = DB::select('select * from users where id = :id and name = :name ',[':id' => 1,':name' =>'測試']);
//查方法
//get() 方法獲取表中所有記錄(獲取多行多列)
$data = DB::table('users')->get();
//first() 方法將會返回單個對象(獲取一行一列)
//where() 方法查詢指定條件對象
$data = DB::table('users')->where('id','name','3','測試')->first();
//select() 方法可以查詢指定自定義字段
$data = DB::table('users')->select('id','name', 'email')->get();
//value() 方法從結果中獲取單個值,該方法會直接返回指定列的值:
$data = DB::table('users')->where('name','測試')->value('email');
//pluck() 方法獲取單個列值的數組
$data = DB::table('users')->pluck('name');
//count() 統計數量
$data = DB::table('users')->count();
//exists() 方法來判斷匹配查詢條件的結果是否存在
$data=DB::table('users')->where('id', 1)->exists();
//join() 方法連表查詢
$data = DB::table('users')
->join('ceshi', 'users.id', '=', 'ceshi.id')
->select('users.*', 'ceshi.name')
->get();
//leftJoin() 方法左連表查詢
$data = DB::table('users')
->leftJoin('ceshi', 'users.id', '=', 'ceshi.id')
->select('users.*', 'ceshi.name')
->get();
//where() 參數說明:(一)參數是列名,(二)參數是操作符,(三)參數是該列要比較的值
$data = DB::table('users')
->where('id', '>=', 1)
->where('name', 'like', '測試%')
->get();
//傳遞條件數組到where中寫法,建議多where查詢使用這個方法
$data = DB::table('users')
->where([
['id', '>=', 1],
['name', 'like', '測試%']
])
->get();
//whereBetween() 方法驗證列值是否在給定值之間
$data = DB::table('users')
->whereBetween('id', [1, 3])->get();
//whereIn 方法驗證給定列的值是否在給定數組中:
$data = DB::table('users')
->whereIn('id', [1, 2, 3])
->get();
//orderBy() 方法排序
$data = DB::table('users')
->orderBy('id', 'desc')
->get();
//insert() 方法插入記錄到數據表
//insertGetId() 方法插入記錄并返回自增ID值
$data=DB::table('users')->insert(
[
'name'=>'測試',
'email' => 'ceshi.com',
'password' => 'ceshi'
]
);
//update() 方法修改記錄
$data =DB::table('users')
->where('id', 1)
->update(['name' => '測試']);
//delete() 方法刪除記錄
$data=DB::table('users')->where('id', '>', 10)->delete();
//paginate() 方法分頁 每頁顯示數量
//注意:目前使用 groupBy 的分頁操作不能被Laravel有效執行
$data = DB::table('users')->paginate(2);
//前臺分頁中鏈接附加參數實現分頁
$getName = $GET['name']?:'';
$data = DB::table('users')
->select('id','name','age')
->where('name', 'like', $getName.'%')
->paginate(2);
//返回給前端視圖數據
return $this->view('index',['data'=>$data,'namePage'=>$getName]);
//前端引用代碼
//appends 方法添加查詢參數到分頁鏈接查詢字符串; 添加 name=$namePage到每個分頁鏈接中.
{{ $data->appends(['name' => $namePage])->links() }}
//simplePaginate() 方法分頁視圖中簡單的顯示“下一頁”和“上一頁”鏈接
$data = DB::table('users')->simplePaginate(2);
//返回給前端視圖數據
return $this->view('index',['data'=>$data]);
//前端簡單引用代碼
div class="container">
@foreach ($users as $user)
{{ $user->name }}
@endforeach
/div>
{{ $data->links() }}
//原生分頁寫法
$page = 2;
$pageSize = 1;
$offset = ($page - 1) * $pageSize;
$result = DB::table('picasa')
->where('title', 'like', '%'.$title.'%')
->offset($offset)
->limit($pageSize)
->get();
//返回數據視圖文件
return $this->view('index', ['result' => $result]);
}
}
Log::emergency(string $message, array $context = []);
Log::alert(string $message, array $context = []);
Log::critical(string $message, array $context = []);
Log::error(string $message, array $context = []);
Log::warning(string $message, array $context = []);
Log::notice(string $message, array $context = []);
Log::info(string $message, array $context = []);
Log::debug(string $message, array $context = []);
更多關于Laravel相關內容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優秀開發框架總結》、《php面向對象程序設計入門教程》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》