关于Laravel ORM操作的详解
时间:2022-03-03 11:47
下面由laravel教程栏目给大家介绍详解Laravel ORM操作,希望对需要的朋友有所帮助!
1、laravel获取最后一条sql语句和传入的值:
public function getOrderDetail($orderId){ \DB::connection()->enableQueryLog(); // 开启查询日志 $ordeList = OrderItem::where('order_id',$orderId) ->get()->toArray(); $queries = \DB::getQueryLog(); // 获取查询日志 echo "<pre>"; print_r($queries); echo PHP_EOL; print_r($ordeList); echo "</pre>";
直接上图:
取出部分数据:
public function getOrderDetail($uid,$orderId){ $user = $this->check_user($uid); $columns = ['id', 'order_id', 'item_id', 'item_name', 'item_price', 'original_price', 'buy_num', 'real_num', 'cancel_num', 'status', 'create_time']; $ordeList = OrderItem::where('order_id',$orderId) -> orderBy('create_time','desc') ->get($columns)->toArray(); echo "<pre>"; print_r($ordeList); echo "</pre>"; exit; }
不存在就创建,存在就更新:
Model::updateOrCreate( ['primary_key' => 8], ['field' => 'value', 'another_field' => 'another value'] );
Laravel子查询,多条件判断:
public function getCourseProgress($uid,$levelId=0,$lessonId=0,$type=0,$page=0) { //检测用户合法性 $user = $this->check_user($uid); //当前页数 $page = $page>0?$page:0; //每页显示数量 $perPage = config('bcc.per_page'); //显示字段 $columns = ['*']; #课程学习进度信息 $result = LessonProgress::where('customer_id',$uid) ->where(function($query) use ($type){ if($type) $query->where('source_type',$type); }) ->where(function($query) use ($levelId,$lessonId){ if($levelId && $lessonId) { $query->where(['level_id'=>$levelId,'lesson_id'=>$lessonId]); }elseif($levelId){ $query->where('level_id', $levelId); } })->get(); //->paginate($perPage,$columns,$pageName='',$page); if($result->isEmpty()) return $this->responseSuccess([],'No relevant information',20000); return $this->responseCollection($result,new CourseProgressTransformer); }
Laravel运行复杂的原生语句:
// 声母韵母分两类 $data=Pronounce::selectRaw('group_concat(`letter`) as letters') ->groupBy('pronounce_type') ->get() ->toArray();
selectRaw里面可以直接写sql子句
以上就是关于Laravel ORM操作的详解的详细内容,更多请关注www.gxlsystem.com其它相关文章!