Home >PHP Framework >Laravel >Detailed explanation of Laravel ORM operations
The following tutorial column will introduce you to the detailed explanation of Laravel ORM operations. I hope it will be helpful to friends who need it!
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; }##Create if it does not exist, update if it exists:
Model::updateOrCreate( ['primary_key' => 8], ['field' => 'value', 'another_field' => 'another value'] );
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 runs complex native statements:
// 声母韵母分两类 $data=Pronounce::selectRaw('group_concat(`letter`) as letters') ->groupBy('pronounce_type') ->get() ->toArray();
sql clauses can be written directly in selectRaw
The above is the detailed content of Detailed explanation of Laravel ORM operations. For more information, please follow other related articles on the PHP Chinese website!