昨日开发中遇到的两个问题? ( 数据查询 & 公用代码 )
数据库查询:
- 本来今天先用 Eloquent 做多表查询的 结果发现自己理解错了,貌似它不能用来返回一个包含多个表的字段的结果,(可能我文档文档看得不够自己,如果有希望帮忙指点下),后来我才用了普通的查询构建器 就那种
DB :: select()
配合join()
之类的,但是这样我得写好长的如果字段一多的话 因为我还得给字段弄别名,请问有没有更优雅的实现?
# @ 写的我自己都觉得恶心
$Obj_Coupon_List = Self :: select( 'activity_goods.name as name', 'activity_goods.active_code as No', 'lottery_records.coupon_code as TNo', 'is_win as time', 'activity_goods.coupon_img as codeImg', 'lottery_records.id as discount' ) -> join( 'activity_goods', 'lottery_records.product_id', '=', 'activity_goods.id' ) -> where( 'lottery_records.openid', $Str_Openid ) -> orderBy( 'lottery_records.id', 'asc') -> offset( 0 ) -> limit( 100 ) -> get();
公共代码:
- 项目有需要对接一些第三方的接口,现在我是直接现在一个控制器里的,但如果其他控制器也需要用,我该怎么弄?直接引入这个空间然后调用么,(貌似搜索下说是并不建议这么做?)有比较好的实践么,还是说直接写个项目自己的 Package 包?
# @ 类似这样的请求第三方 API 的
protected static function Create_FreeMud_Coupon ( $Str_Title , $Str_OrderId, $Str_Active_Code ) {
# @ Params Make
$Obj_Collection_Data = collect( Self :: $Arr_Freemu_Params );
$Obj_Collection_Data['title'] = $Str_Title;
$Obj_Collection_Data['order_id'] = $Str_OrderId;
$Obj_Collection_Data['active_code'] = $Str_Active_Code;
$Arr_Collection_Data = $Obj_Collection_Data -> map ( function ( $Str_Item, $Str_Key ) {
return $Str_Key . '=' . $Str_Item;
}) -> sort() -> all();
$Str_Params = implode( '&', $Arr_Collection_Data );
$Str_Final_Data = $Str_Params . '&sign=' . md5( $Str_Params . Self :: $FreeMuKey );
# @ Send Request
$Arr_Config = ['Content-Type' => 'application/x-www-form-urlencoded'];
$Obj_Guzzle = new Guzzle_Client;
$Obj_Request = new Guzzle_Client_Psr7( 'POST', '...................', $Arr_Config, $Str_Final_Data );
$Obj_Response = $Obj_Guzzle -> send( $Obj_Request, [ 'timeout' => 2 ] );
$Str_Response = $Obj_Response -> getBody();
# @ Return Data
return $Str_Response;
}
- 希望大家帮小弟我解解惑
推荐文章: