Home  >  Article  >  PHP Framework  >  Detailed explanation of Laravel ORM operations

Detailed explanation of Laravel ORM operations

藏色散人
藏色散人forward
2021-04-20 09:42:243303browse

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!

##1. Laravel gets the last sql statement and the passed-in value:

    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 "
";Go directly Picture:

Take out some data:

    public function getOrderDetail($uid,$orderId){
        $user = $this->check_user($uid);        $columns = [&#39;id&#39;, &#39;order_id&#39;, &#39;item_id&#39;, &#39;item_name&#39;, &#39;item_price&#39;, &#39;original_price&#39;, &#39;buy_num&#39;, &#39;real_num&#39;, &#39;cancel_num&#39;, &#39;status&#39;, &#39;create_time&#39;];        $ordeList = OrderItem::where(&#39;order_id&#39;,$orderId)
            -> orderBy(&#39;create_time&#39;,&#39;desc&#39;)
            ->get($columns)->toArray();        echo "<pre class="brush:php;toolbar:false">";
            print_r($ordeList);        echo "
";        exit;     }

Detailed explanation of Laravel ORM operations

##Create if it does not exist, update if it exists:

Model::updateOrCreate(
   [&#39;primary_key&#39; => 8],
   [&#39;field&#39; => &#39;value&#39;, &#39;another_field&#39; => &#39;another value&#39;]
);

Laravel subquery, multi-condition judgment:

    public function getCourseProgress($uid,$levelId=0,$lessonId=0,$type=0,$page=0)
    {
        //检测用户合法性
        $user = $this->check_user($uid);        //当前页数
        $page = $page>0?$page:0;        //每页显示数量
        $perPage = config(&#39;bcc.per_page&#39;);        //显示字段
        $columns = [&#39;*&#39;];        #课程学习进度信息
        $result = LessonProgress::where(&#39;customer_id&#39;,$uid)
            ->where(function($query) use ($type){
                if($type) $query->where(&#39;source_type&#39;,$type);
            })
            ->where(function($query) use ($levelId,$lessonId){
                if($levelId && $lessonId) {                    $query->where([&#39;level_id&#39;=>$levelId,&#39;lesson_id&#39;=>$lessonId]);
                }elseif($levelId){                    $query->where(&#39;level_id&#39;, $levelId);
                }
            })->get();            //->paginate($perPage,$columns,$pageName=&#39;&#39;,$page);

        if($result->isEmpty()) return $this->responseSuccess([],&#39;No relevant information&#39;,20000);        return $this->responseCollection($result,new CourseProgressTransformer);
    }
Detailed explanation of Laravel ORM operationsLaravel runs complex native statements:

    // 声母韵母分两类
    $data=Pronounce::selectRaw(&#39;group_concat(`letter`) as letters&#39;)
    ->groupBy(&#39;pronounce_type&#39;)
    ->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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete