On the problem of data query preloading in tp5

   CompanyModel::with(["user"=>function($query) use($search){
        $query->field("user_id,user_name,company_id")->where($search["ucon"]);
    }])->field("id,name,title,type,utm_tag,status")->where($search["con"])->paginate(15);
    
    

how to write such a code: when the closure condition is not satisfied, the data cannot be found;
now, when the closure condition is not satisfied, all the data in the companymodel table will come out.

Php
Feb.16,2022

you can use the when method. You can also call the load method after the query is completed without with.

Menu