LOADING

标签 Laravel 下的文章

$keyword = $this->filters['keyword'];
$options = $this->parseOptions();
$datas = TableModel::with('hasBook')
    ->with('hasGradeCell.hasGrade')
    ->when($keyword,function($query)use($keyword){
        return $query->where('title','like','%'.$keyword.'%');
    })
    ->when($options,function ($query) use ($options){
        if (isset($options['cell_id'])) {
            return $query->where('cell_id', $options['cell_id']);
        }
        if (isset($options['grade_id'])) {
            return $query->whereHas('hasGradeCell',function ($query) use ($options){
                return $query->whereHas('hasGrade',function ($query) use ($options){
                    return $query->where('id',$options['grade_id']);
                });
            });
        }

    });
$counts            = $datas->count();

$datas = $datas->where('type', 1)
        ->orderBy($this->filters['sort'], $this->filters['sort_by'])
        ->paginate($this->filters['limit'])
        ->appends($this->filters);

try {

works::insertGetId($adata);
workContents::insertGetId($diyAdata);
DB::commit();

}catch (Exception $e){

DB::rollback();
Log::debug('添加失败'.'$e->getMessage:'.$e->getMessage().'|getCode:'.$e->getCode());
return output_error('添加失败');

}

public function upload(Request $request){

$imgs = [];
if ($request->hasFile('upload_img')){
    foreach ($request->file('upload_img') as $v){
        $time      = strtotime(date("Y-m-d 00:00:00"));
        $path = $v->move(public_path().'/upload/'.$time,$v->hashName());
        dump($path->getRealPath());die;
        $imgs[]= asset($path);
    }
    return response()->json([
        'errno'=>0,
        'data'=>$imgs
    ]);
}else{
    return response()->json([
        'info'=>'没有图片'
    ]);
}

}

use SoftDeletes;//使用软删除
/**

  • 隐藏字段(必有得参数不然返回值永远多一个hasOnetable)
  • @var array
    */

public $hidden = ['hasOnetable','hasTwotable'];

public function hasOnetable(){

return $this->hasOne('App\hasOnetable','id','one_id');//第一个是关联表主键,第二个是本表的外键

}

public function hasTwotable(){

return $this->hasOne('App\hasTwotable','two_table_id','id');

}

const limit = 6;//设置一个常量

$taskId = $request->task_id;
$type = $request->type;
$map['task_id'] = $taskId;
$map['finished'] = 2;
$isAll = $request->is_all ? $request->is_all: 0;
$limit = $request->limit ? $request->limit : self::limit;
$data = UserHomeworks::where($map)

->when($type == 2  , function ($query) use ($type) {//            未复读
    return $query->where('status',0);
})
->select('id','finished_time')
->orderBy('created_at', 'desc')
->with('hasOne')   //这部是为了之后循环的时候不每一次都查库,渴求式加载
->when($isAll == 1 , function ($query) use ($isAll) {//              没有分页
    return $query->get();
})
->when($isAll != 1 , function ($query) use ($isAll,$limit) {//              有分页
    return $query->paginate($limit);
});

Laravel中对数据库的连贯操作叫做 查询构造器,我本人是比较推荐使用查询构造器的,方便易读好交接,而且可以使用tp的思想进行操作
例子如下

$map['aggratepage_game_statistic.channel'] = $channel;
$data = DB::table('a')
    ->leftJoin('b','a.ad_id','=','b.task_id')
    ->where($map)
    ->whereBetween('a.date',[$startTime,$endTime])
    ->select('a.*','b.task_name')
    ->paginate($row);

具体使用上跟TP5很相似只不过需要注意的是Laravel的select 就是TP的field,使用了一周Laravel 我至今觉得TP5的Db类使用方便程度上略胜Laravel,TP5的page简直就是碾压Laravel!
切入正题
Laravel DB类取出的数据都是对象

LengthAwarePaginator{#460 ▼
  #total: 2
  #lastPage: 1
  #items:Collection{#458 ▼
    #items: array:2 [▼
      0 => {#454 ▼
        +"date": "2018-09-18"
        +"channel": "123"
        +"haha_id": 1
         }    ]  }
  #perPage: 10
  #currentPage: 1
  #path: "http://localhost/youzhuan/public/Admin/Statistics"
  #query: []
  #fragment: null
  #pageName: "page"}

这种对象内是否有数据是不能用!来分辨的,如果你想判断这个对象中是否有数据可使用

if($data->count()){

}

来进行判断,如果想获取数据可以使用
$data->items();这样获取出来的数据还是对象
如果你想在使用中直接在查询中就获取到$data->items()这样的对象的话 你可以在连贯操作后加->toArray();
你在使用Laravel中会发现数据库取出来的都是对象,真的超级难用,至少我是这么觉得的,我还是觉得从数据库取出来的直接就是数组比较爽