Laravellaravel | 系统学习笔记四公共专题列表 视图合成器 \View::composer('mobandizhi',function($view){ //加载 }) 使用 位置\app\Providers\AppServiceProvider.php public function boot() { \View::composer('layout.sidebar',function($vi作者王明昌发布时间2018-12-19复制公共专题列表 视图合成器 \View::composer('mobandizhi',function($view){ //加载 }) 使用 位置\app\Providers\AppServiceProvider.php public function boot() { \View::composer('layout.sidebar',function($view){ $topics = \App\Topic::all(); $view->with('topics',$topics); }) } view @foreach($topics as $topic) {{$topics->id}} @endforeach scope 属于/不属于 定义 //scope+名 //必须返回$query public function scopeActiveee($query){ return $query->where('active',1); } 使用 $users = App\User::popular()->activeee()->orderBy('created_at')->get(); demo 属于某个作者的文章(post.php) public function scopeAuthorBy(Builder $query,$user_id){ return $query->where('user_id',$user_id); } 不在某个topic中的文章(post.php) //获取专题中的文章 public function postTopics(){ return $this->hasMany(\App\PostTopic::class,'post_id','id'); } public function scopeTopicNotBy(Build $query,$topic_id){ return $query->doesntHave('postTopics','and',function($q) use($topic_id){ $q->where('topic_id',$topic_id); }); } 专题 //专题的文章 public function posts(){ 表 这个表的外键 当前的键 $this->belongsToMany(\App\Post::class,'post_topics','topic_id','post_id'); } //专题的文章数 public function postTopics(){ $this->hasMany(\App\PostTopic::class,topic_id); //id可不用写 } 控制器/模板 public function show(Topic $topic){ //带文章的专题 $topic = Topic::withCount('postTopics')->find($topic->id); //专题的文章列表,,,按创建时间前十条 $posts = $topic->posts()->orderBy('create_at','desc')->take(10)->get(); //属于我的文章,未投稿 $myposts = \App\Post::authorBy(\Auth::id())->topicNotBy($topic->id)->get(); } 后台搭建 psr4规定了命名方式 把前后端的路由分开 web.php 中引入include_once('admin.php'); 同目录创建admin.php Route::group(['prefix'=>admin],function(){ Route::get('/ceshi',function(){ return 111; }); }) \resources\views\admin \app\Admin\Controllers 后台页面模板 https://github.com/almasaeed2010/AdminLTE composer require "almasaeed2010/adminlte=~2.0" view return view('admin.login.index') 人员验证 config/auth.php //定义providers 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], 'amdins'=>[ 'driver' => 'eloquent', 'model' => App\AdminUser::class, ], ], //定义guards 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' =>[ 'driver' => 'session', 'provider' => 'admins', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], AdminUser.php use App\Model use Illuminate\Foundation\Auth\User as Authenticatable; class AdminUser extends Authenticatable{ } 路由 Route::group(['prefix' => 'admin'], function() { Route::get('/login', '\App\Admin\Controllers\LoginController@index'); // 需要登陆的 auth.php文件的admin Route::group(['middleware' => 'auth:admin'], function(){ Route::get('/home', '\App\Admin\Controllers\HomeController@index'); }) }) 修改表 public function up() { Schema::table('posts', function (Blueprint $table) { $table->tinyInteger('status')->default(0); //文章状态 0 未知/1 通过/ -1 删除 }); } public function down() { Schema::table('posts', function (Blueprint $table) { $table->dropColumn('status'); //文章状态 0 未知/1 通过/ -1 删除 }); }
2019-01-10laravel多条件排序$goods = Goods::orderBy('created_at','desc')->orderBy('id','desc')->paginate(6);
2018-03-11laravel开发博客遇到的问题(一)1.引入css,js,img等最好使用asset 使用此方式,获取的是带域名的绝对路径 2.测试数据库是否连接正常数据库 use DB; $pdo = DB::connection()->getPdo
2019-01-12laravel 使用生成二维码扩展composer require simplesoftwareio/simple-qrcode 1.5.1 // 在config/app.php 注册服务提供者: SimpleSoftwareIO\Q
2019-02-09laravel新建应用报错:The Process class relies on proc_open, which is not#laravel new blog Crafting application... [Symfony\Component\Process\Exception\RuntimeException] The
2020-01-01laravel 使用 Editor.md关于图片上传csrf的处理 大家都知道laravel默认提交表单时是启用csrf验证的 那么关于Editor.md关于图片上传csrf的处理有两个 1. 关闭csrf验证  加入csrf验证 上传方法:  视图: 加入t
2019-01-12laravel migration 中integer方法无法指定长度查看源代码后发现integer方法的第二个参数并不是指定长度,而是是否设置auto increment, 所以integer方法无法指定子段长度,默认为11。