溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在laravel5.6框架中利用curd操作數(shù)據(jù)

發(fā)布時間:2021-06-07 17:38:32 來源:億速云 閱讀:126 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在laravel5.6框架中利用curd操作數(shù)據(jù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

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

<?php
//laravel5.6 語法 demo示例
namespace App\Http\Controllers;//命名該控制App空間下名稱
use Illuminate\Support\Facades\DB;//使用DB操作數(shù)據(jù)庫
use App\Http\Controllers\Controller;//繼承基礎(chǔ)控制器
class UserController extends Controller
{
 /**
  * 展示應(yīng)用的用戶列表.
  *
  * @return Response
  */
 public function index()
 {
  //DB使用為每種操作提供了相應(yīng)方法:select(查),update(修改),insert(插入),delete(刪除),statement(聲明)
  //建議占位符,其他框架通用性強(qiáng)
  //原生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() 方法從結(jié)果中獲取單個值,該方法會直接返回指定列的值:
  $data = DB::table('users')->where('name','測試')->value('email');
  //pluck() 方法獲取單個列值的數(shù)組
  $data = DB::table('users')->pluck('name');
  //count() 統(tǒng)計(jì)數(shù)量
  $data = DB::table('users')->count();
  //exists() 方法來判斷匹配查詢條件的結(jié)果是否存在
  $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() 方法驗(yàn)證列值是否在給定值之間
  $data = DB::table('users')
   ->whereBetween('id', [1, 3])->get();
  //whereIn 方法驗(yàn)證給定列的值是否在給定數(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ù)實(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();
  //返回?cái)?shù)據(jù)視圖文件
  return $this->view('index', ['result' => $result]);
 }
}

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

當(dāng)select和groupBy中列表不一致時候會報錯。mysql從5.7以后,默認(rèn)開啟group by的嚴(yán)格模式。

解決方法:找到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
默認(rèn)日志存放位置: /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框架中利用curd操作數(shù)據(jù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI