关联模型的一些用法

关于with的一些用法:(with为链式操作)

$product = self::with('imgs,properties')->find($id);

$product = self::with(['imgs','properties'])->find($id);

$product = self::with(['imgs.imgUrl','properties'])->find($id);

$product = self::with(['imgs'])->with(['properties'])->find($id);

当给关联下的关联属性内容进行排序

$product = self::with(
    ['imgs' =>function($query){
            $query->with(['imgUrl'])->order('order asc');}]
)->with(['properties'])->find($id);    

 关于路由的匹配

//获取最近新品
Route::get('api/:version/product/recent','api/:version.Product/getRecent',[],['id'=>'\d+']);

//获取所有栏目
Route::get('api/:version/category/all','api/:version.Category/getAllCategories');

//以上如果不定义变量约束的话,会根据上下顺序优先访问上面的,造成下在的表达式访问不了. 

 

猜你喜欢

转载自www.cnblogs.com/bing2017/p/9057798.html