laravel 初学

1.使用命令创建控制器会自带一些方法   php artisan make:controller ArticleController --resource

2.使用命令行创建不同模块的控制器  php artisan make:controller Admin/ArticleController --resource

3.创建路由 

(1)普通路由  Route::get('article/index','ArticleController@index');

  (2)路由的分组 

 Route::prefix('article')->group(function(){

  Route::get('index','ArticleController@index');
  Route::get('create','ArticleController@create');
  Route::get('store','ArticleController@store');
});

  (3) 多级路由的分组

Route::prefix('admin')->namespace('Admin')->group(function(){
  Route::prefix('article')->group(function(){
    Route::get('index','ArticleController@index');
    Route::get('create','ArticleController@create');
    Route::get('store','ArticleController@store');
  });
});

(4)中间键

Route::middleware(['first', 'second'])->group(function () {

Route::get('/', function () {

// 使用 first 和 second 中间件

});

Route::get('user/profile', function () {

// 使用 first 和 second 中间件

});

});

 

配置项的修改

(1)数据库的格式的修改  config/database.php  里面的 charset = ‘utf8’ collation='utf8_unicode_ci';

  还要修改 strict ='false' 防止使用group 的时候会报错

(2)修改 config/app.php里面   时间 timezone='PRC' 中国的时区‘  locale='zh-CN'  文字改成中文  

 (3){{ asset('js/1.js') }}  加载样式和js文件

数据查询

where()  whereIn()  whereNotIn()

->join('users as u', 'u.id', 'articles.user_id')

->groupBy('u.id')

->orderBy('created_at', 'desc')

->select('u.id', 'u.name', 'u.email')

相应的最后的 ->get() 还可以替换为 firstcountsumpluckvalue

first(一维数组,并且是第一个)

pluck(当里面的参数是一个的时候是去取某一个字段的值 0=>‘标题1’,当里面的参数是两个的时候 是把第二个字段当做key,第一个当做值  ‘标题1’=>'内容1' )

value( 里面有一个参数,取出的就是这个字符)

 

 添加数据

$data = $request->except('_token')

$arr = $articleModel->create($data)   要返回这条添加数据的id  $arr->id

Model 的操作

php artisan make:model Models/Article     添加一个article 的model

在控制器中使用Model 

(1)先应用model  use App\Models\Article     

(2)在方法中使用  public function create(Article $articleModel){}

要往表里面添加数据需要先在model 里面设置 protected $fillable=['category_id','title']   这个是允许添加的字段

protected $guarded =[]  这个是不允许添加的字段  ,当设置为空数组的时候,就是所有的都可以添加

这两个只能定义一个,不能一起使用

protected $table= 'my_user'  定义一个表 ,如果不定义则自动为文件名 为表名

protected $primaryKey = 'user_id'  ; 定义主键的id  ,如果不定义则默认为id

protected $timestamps = false ; 之前的数据库中的时间 有个默认的时间 update_at create_at 两个时间,如果不想自动使用,则定义为false 

修改数据

$articleModel->where(['id'=>1])->update($data);   返回修改的条数

删除数据

使用软删除的时候要先在 Model 中开启

use Illuminate\Database\Eloquent\SoftDeletes;    这个是在头部定义的

use SoftDeletes;  这个是在类里面定义的

$articleModel->where(['id'=>1])->delete(); 这个删除就是在数据表  deleted_at 里面添加一条数据

获取所有的数据包括删除的数据  $articleModel->withTrashed()->get();

只获取删除的数据 $articleModel->onlyTrashed()->get();

恢复删除的数据 $articleModel->where(['id'=>1])->restore();

彻底删除数据  $articleModel->where(['id'=>1])->forceDelete();

(4)设置正则的规则

这个是定义单个的 Route::get('edit/{id}/{name}','ArticleController@edit')->where('id','[0-9]+');

定义全局的 在这个文件里面 app/providers/RouteServiceProvider.php boot 方法里面 添加 Route::pattern('id','[0-9]+');

4.数据迁移

php artisan make:migration create_articles_table    articles 是要创建的表名

php artisan make:migration add_deleted_at_to_users_table  往users表里面添加一个deleted_at 字段

 

视图

渲染一个视图 return view('admin.article.index')   这个 index.blade.php  是在 resources/views/admin/article/index.blade.php  这个文件

如果要往里面赋值使用 $assign = ['data'=>$data];  这个等同于  compact('data');   然后赋值      return view('admin.article.index',$assign);

添加内容的页面 要默认加一个 {{ csrf_field() }} 用来防止csrf 攻击    接受值的时候用  $request->except('_token');

添加默认的HTTP请求  {{method_field('PUT')}}

src="{{url('admin/article/edit',id=$value->id)}}"

redirect('index')  页面的跳转 

判断视图是否存在  View::exists('admin.article')

return view('greeting')->with('name', 'Victoria');

 

 

 

关系的继承

在父类里面把一些公共的部分写出来,然后有变化的地方使用  @yield('title')  @yield('content') 来代替  分别代表标题 和内容   ,可以自己定义变量

然后在子级里面引用的时候  @extends('layout.home')  先调用这个文件   @section('title',$title)  直接赋值这个变量 ,如果是一个块级的(内容) @section('content')    @endsection

 

添加信息的规则的验证   

首先创建一个规则验证的文件   php artisan make:request Article/Store    Article  是要验证的规则的控制器的名称    Store 文件名是   要验证的方法名

在控制器里面 调用判断 use App\Http\Request\Article\Store  然后再方法中使用  function (Stroe $request){  $request=>all()}

在html  页面判断   @if($errors->has('title'))  {{ $errors->first('title')}} @endif  进行页面的错误展示

设置规则 在文件中 authorize(){ return true}  设置为true

设置规则

public function rules(){

 return [

  'username'=>'required',

  'password'=>'required|string',

]

}

给规则定义中文

public function messages(){

return[

  'username.required'=>'用户名不能为空'

]

}

 

中间件

添加一个中间键   php artisan make:middleware AdminLogin

可以给指定的路由添加中间键 需要在 app/Http/Kernel.php  这个里面的 $routeMiddleware 里面 

  ‘admin.login’ => \App\Http\Middleware\AdminLogin::class,    指定AdminLogin 这个中间键

然后需要在要添加中间件的路由后面 添加   Route::get('index','IndexController@index')->middleware('admin.login');   来指定这个路由要加载中间件

如果要过滤掉某个提交的csrf 的防止可以在中间件的 app/Http/Middleware/verifyCsrfToken.php  这个文件的  

protected $except = [
'admin/article/*',
];

添加要过滤的url 路径

 

重定向

return back()->withInput();  //返回上一级

return redirect()->route('login',['id'=>7]);   //指定路由和指定的参数

return redirect()->action('HomeController@index');   //指定控制器的方法

return response()->json([ 'name' => 'Abigail', 'state' => 'CA' ]);    //json 的传值

 

return response()->download('./Public/', '1.txt', $headers);   //设置下载  第一个是下载的路径 第二个是下载的文件名

 return response()->file($pathToFile);   //直接打开一个文件,类似打开图片或者pdf 文件

posted @ 2019-03-29 10:59  烟影如画在行动  阅读(154)  评论(0编辑  收藏  举报