以下由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 "
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中文網其他相關文章!