濮阳杆衣贸易有限公司

主頁 > 知識庫 > laravel5.6框架操作數(shù)據(jù)curd寫法(查詢構建器)實例分析

laravel5.6框架操作數(shù)據(jù)curd寫法(查詢構建器)實例分析

熱門標簽:百應電話機器人服務 岳陽外呼型呼叫中心系統(tǒng)在哪里 河南電銷卡外呼系統(tǒng)哪家強 山西回撥外呼系統(tǒng) 山西探意電話機器人 騰訊外呼管理系統(tǒng) 青島語音外呼系統(tǒng)招商 揚州地圖標注app 昭通辦理400電話

本文實例講述了laravel5.6框架操作數(shù)據(jù)curd寫法(查詢構建器)。分享給大家供大家參考,具體如下:

laravel5.6 數(shù)據(jù)庫操作-查詢構建器

?php
//laravel5.6 語法 demo示例
namespace App\Http\Controllers;//命名該控制App空間下名稱
use Illuminate\Support\Facades\DB;//使用DB操作數(shù)據(jù)庫
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() 方法獲取單個列值的數(shù)組
  $data = DB::table('users')->pluck('name');
  //count() 統(tǒng)計數(shù)量
  $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() 參數(shù)說明:(一)參數(shù)是列名,(二)參數(shù)是操作符,(三)參數(shù)是該列要比較的值
  $data = DB::table('users')
   ->where('id', '>=', 1)
   ->where('name', 'like', '測試%')
   ->get();
  //傳遞條件數(shù)組到where中寫法,建議多where查詢使用這個方法
  $data = DB::table('users')
   ->where([
    ['id', '>=', 1],
    ['name', 'like', '測試%']
   ])
   ->get();
  //whereBetween() 方法驗證列值是否在給定值之間
  $data = DB::table('users')
   ->whereBetween('id', [1, 3])->get();
  //whereIn 方法驗證給定列的值是否在給定數(shù)組中:
  $data = DB::table('users')
   ->whereIn('id', [1, 2, 3])
   ->get();
  //orderBy() 方法排序
  $data = DB::table('users')
   ->orderBy('id', 'desc')
   ->get();
  //insert()  方法插入記錄到數(shù)據(jù)表
  //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() 方法分頁 每頁顯示數(shù)量
  //注意:目前使用 groupBy 的分頁操作不能被Laravel有效執(zhí)行
  $data = DB::table('users')->paginate(2);
  //前臺分頁中鏈接附加參數(shù)實現(xiàn)分頁
  $getName = $GET['name']?:'';
  $data = DB::table('users')
    ->select('id','name','age')
    ->where('name', 'like', $getName.'%')
    ->paginate(2);
  //返回給前端視圖數(shù)據(jù)
  return $this->view('index',['data'=>$data,'namePage'=>$getName]);
  //前端引用代碼 
  //appends 方法添加查詢參數(shù)到分頁鏈接查詢字符串; 添加 name=$namePage到每個分頁鏈接中.
  {{ $data->appends(['name' => $namePage])->links() }}
  //simplePaginate() 方法分頁視圖中簡單的顯示“下一頁”和“上一頁”鏈接
  $data = DB::table('users')->simplePaginate(2);
  //返回給前端視圖數(shù)據(jù)
  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();
  //返回數(shù)據(jù)視圖文件
  return $this->view('index', ['result' => $result]);
 }
}

groupBy  對查詢結果進行分組出現(xiàn)問題

當select和groupBy中列表不一致時候會報錯。mysql從5.7以后,默認開啟group by的嚴格模式。

解決方法:找到config/database​.php 在mysql下面把'strict' => true,改為false。[建議不要修改。寫對正確操作語法。]

例如:

$booked = DB::table('booked_user')
 ->select('game_id', DB::raw('count(*) as total'))
 ->groupBy('game_id')
 ->get();

開啟sql查詢?nèi)罩?/strong>

DB::connection()->enableQueryLog();//開啟QueryLog
$data = DB::table('users')->select('id','name', 'email')->get();//執(zhí)行sql
dump(DB::getQueryLog());//sql語句和查詢時間

寫入日志信息

八種日志級別:emergency、alert、critical、error、warning、 notice、info 和 debug
默認日志存放位置: /storage/logs/laravel.log

引用: use Illuminate\Support\Facades\Log;

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 = []);

laravel5.6 操作數(shù)據(jù)ORM

更多關于Laravel相關內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進階教程》、《php優(yōu)秀開發(fā)框架總結》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》

希望本文所述對大家基于Laravel框架的PHP程序設計有所幫助。

您可能感興趣的文章:
  • Laravel5.1 框架數(shù)據(jù)庫查詢構建器用法實例詳解
  • laravel框架數(shù)據(jù)庫操作、查詢構建器、Eloquent ORM操作實例分析
  • laravel通用化的CURD的實現(xiàn)
  • Laravel框架查詢構造器 CURD操作示例
  • Laravel框架實現(xiàn)model層的增刪改查(CURD)操作示例
  • Laravel框架數(shù)據(jù)庫CURD操作、連貫操作總結
  • laravel5.6 框架操作數(shù)據(jù) Eloquent ORM用法示例
  • laravel 操作數(shù)據(jù)庫常用函數(shù)的返回值方法
  • laravel框架數(shù)據(jù)庫配置及操作數(shù)據(jù)庫示例

標簽:黃南 南陽 寶雞 銅川 宜賓 湛江 鎮(zhèn)江 婁底

巨人網(wǎng)絡通訊聲明:本文標題《laravel5.6框架操作數(shù)據(jù)curd寫法(查詢構建器)實例分析》,本文關鍵詞  laravel5.6,框架,操作,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《laravel5.6框架操作數(shù)據(jù)curd寫法(查詢構建器)實例分析》相關的同類信息!
  • 本頁收集關于laravel5.6框架操作數(shù)據(jù)curd寫法(查詢構建器)實例分析的相關信息資訊供網(wǎng)民參考!
  • 推薦文章
    通州市| 成安县| 四平市| 远安县| 玉林市| 石嘴山市| 石柱| 馆陶县| 芒康县| 永城市| 集安市| 东丰县| 廊坊市| 枣阳市| 和田市| 垦利县| 九龙坡区| 龙游县| 万州区| 棋牌| 黄浦区| 衡阳市| 湘阴县| 雷州市| 彝良县| 洛扎县| 博白县| 普定县| 田林县| 马尔康县| 青川县| 临颍县| 方山县| 盈江县| 武邑县| 长寿区| 中宁县| 沙湾县| 武汉市| 罗城| 盐池县|