下面由laravel教程栏目给大家介绍详解Laravel ORM操作,希望对需要的朋友有所帮助!
public function getOrderDetail($orderId){ \DB::connection()->enableQueryLog(); // 开启查询日志 $ordeList = OrderItem::where('order_id',$orderId) ->get()->toArray(); $queries = \DB::getQueryLog(); // 获取查询日志 echo "<pre class="brush:php;toolbar:false">"; print_r($queries); echo PHP_EOL; print_r($ordeList); echo "";
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 class="brush:php;toolbar:false">"; print_r($ordeList); echo ""; 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操作的详解的详细内容。更多信息请关注PHP中文网其他相关文章!