laravel 采坑,笔记

20. 使用自自定义分页,且不用连表查询的写法

public function catelist(Request $request) {
$channelId = $request->get('channel_id');
//$list = DB::table('article_channel')->join('article', 'article_channel.article_id', '=', 'article.id')->select('article.*')->where('article_channel.channel_id', $channelId)->where('article.isshow', 1)->orderBy('article_channel.pr', 'desc')->orderBy('article.publishdate', 'desc')->paginate(30);
$channelList = DB::table('article_channel')->select('*')->where('channel_id', $channelId)->orderBy('createdate', 'desc')->paginate(30);
$total = DB::table('article_channel')->select('*')->where('channel_id', $channelId)->count();
$perPage =30;//每页显示几条
$currentPage = $request->get('page')??'1';//当前第几页
$url=preg_replace("/\?.+/","",\Request::getRequestUri());
$ids = [];
foreach($channelList as $lk=>$lv){
$ids[] = $lv->article_id;
}

$select = ['id','title','tag','source_name','pgcname','postimg','posttype','isshow','publishdate'];
$list = DB::table('article')->select($select)->whereIn('id', $ids)->orderByRaw(DB::raw("FIELD(id," . join(",", $ids) . ")"))->where('isshow',1)->get();

//自定义分页类
$result = new \Illuminate\Pagination\LengthAwarePaginator($list,$total,$perPage,$currentPage,[
'path'=>$url,
'pageName'=>'page'
]);

$viewData = array(
'currentcate' => config('channels.' . $channelId . '.title'),
'channel_id' => $channelId,
'nav_parent' => 1,
'nav' => 'all',
'list' => $result
);
return view('admin/article/catelist', $viewData);
}


19.  DB新增单条
$storeDataChannel = ['channel_id' => $channel,'video_id' => $val,'update' => date("Y-m-d H:i:s",time()),'day' => date("Y-m-d",time()),'is_focus' => $video->is_focus];
DB::table('video_channel_app')->insertGetId($storeDataChannel);

* DB新增多条
$param = Array(
[0] => Array([channel_id] => 1,[info_id] => 1,[update] => 2019-10-21 16:24:09,[day] => 2019-10-21,[is_focus] => 0 )
[1] => Array( [channel_id] => 9,[info_id] => 1,[update] => 2019-10-21 16:24:09,[day] => 2019-10-21,[is_focus] => 0 )
)
DB::table('tg6_info_channel')->insert($param);

16. Model返回不为空验证
$result = Model::where(...)->get();
//不为空则
if ($result->first()) { }
if (!$result->isEmpty()) { }
if ($result->count()) { }

15. 关联指定条件查询
if ($request->get('viewtype')){
$viewtype = $request->get('viewtype');
$model->whereHas('advplan',function($q)use ($viewtype){
$q->where('viewtype',$viewtype);
});
}
//$res = $model->orderBy('create_date','desc')->with(['advplan'])->paginate($request->get('limit',30))->toArray();

14. laravel url 携带参数写法
路由:Route::get('system-clear','ClearController@clear')->name('clear');
模板页面传递参数:<a href="{{route('clear',array('type_id'=>1))}}">http://www.xiaoshu168.com</a>
生成地址:127.0.0.1/clear?type_id=1
控制器接收:public function clear(Request $request)
{
dd($request->input('type_id'));
}

13. model关联
// 所属计划
public function advplay()
{
return $this->belongsTo('App\Models\Adv\Advplan','planid','id');
}

# 控制器查询
$model = Article::query();
if ($request->get('category_id')){
$model = $model->where('category_id',$request->get('category_id'));
}
if ($request->get('title')){
$model = $model->where('title','like','%'.$request->get('title').'%');
}
$res = $model->orderBy('created_at','desc')->with(['tags','category'])->paginate($request->get('limit',30))->toArray();

12. 上传文件报错laravel 413 Request Entity Too Large
解决八法:更改nginx conf 再http{}中添加 client_max_body_size 20m;

11. 验证密码哈希值
use Illuminate\Support\Facades\Hash;
$password = trim($request->post('oldpassword'));
$member = Member::findOrFail($id);
if(!Hash::check($password, $member->password)){
return redirect()->to('adv/user/password')->with(['status'=>'旧密码错误']);
}

10. 输出毫秒语句
<?php
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
echo "<!-- ".$msectime." -->";
?>

9.数据库,指定字段按照规则排序 $ids = [1,0,4,2]
$listdb = DB::table('article')->select('*')->whereIn('id', $ids)->orderByRaw("FIELD(id, " . implode(", ", $ids) . ")")->get()->toArray();

8.删除文件
@unlink($videofile);

7.判断是否存在文件
if (is_file(public_path() . '/update/video/' . $dateDir . '/' . $id . '.mp4'))
{
continue;
}

6. 判断是否存在目录
if (!is_dir(public_path() . '/update/video/' . $dateDir))
{
mkdir(public_path() . '/update/video/' . $dateDir, 0777);
}

5.创建模型文件(生成Models目录和Article.php文件)
php artisan make:model Models/Article
生成模型文件的同时生成迁移文件
php artisan make:model Models/Article -m

4.打印sql执行语句
//DB::connection()->enableQueryLog();
//$queries = DB::getQueryLog();

3.页面输出给默认值
<a href="{{url($channel_url or '')}}">{{$channel_name or '推荐'}}</a>

2. 缓存写入

$cacheValue = Cache::store('file')->get($cachekey);
$cacheValue = Cache::store('file')->forget($cachekey);
Cache::store('file')->put($cachekey, $data, $this->cachetime);

1.获取用户信息
Auth::guard('pgc')->user()

==========================================

大坑1:419

描述:登入的时候报419

解决方案:

1.meta头部增加token

<meta name="csrf-token" content="{{ csrf_token() }}" />

2.表单提交增加token

  2.1{ csrf_field() }}

  2.2 <input type="hidden" name="_token" value="{{ csrf_token() }}">

3.js header增加token

$.ajaxSetup({
headers: { 'X-CSRF-TOKEN' : '{{ csrf_token() }}' }
});

以上如果都无法解决问题,则需要注意当前域名下是否使用cdn加速,因为cdn加速会缓存当前页面,所以会导致{{ csrf_token() }}值过期,这样就会每次都会报419错误!! 
 
posted @ 2019-06-27 10:41  Jenkin一H  阅读(334)  评论(0编辑  收藏  举报