限时免费试用:欢迎注册 api.bigmodel.org ,快速体验大模型 API 接入服务。
当前位置:首页 >开发者 >网站框架 >后端框架 >Laravel

laravel | 系统学习笔记四

分类:Laravel时间:2018-12-19浏览:2850
公共专题列表
	视图合成器
		\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 删除
	        });
	    }
	
本站文章如未注明出处均为原创,转载请注明出处,如有侵权请邮件联系站长。
0/500
Share your thoughts respectfully.