1:Laravel的启动
javascript
切换到laravel项目下 www/my_laravelphp
开启laravel服务 php artisan serve
html
2:快速建立一个laravel控制器/模型java
php artisan make:controller NameController;linux
php artisan make:model modelName;laravel
3:查看路由列表 数据库
php artisan route:list数组
(路由的做用是宏理请求)观的分发/处session
请求方式:get/post/anycomposer
4:down / up
php artisan down /up
5:laravel操做数据库--需提早建立好数据库模型 php artian make:model modelName;
Route::get('/test', function(){ $user = new App\User(); //数据库内容查询 // return $user->all(); //数据库内容添加 // $user->userAdd(); //数据库内容修改 // $user->userUpdate(); //数据库内容的删除 $user->userDelete(); return $user->userRead(); });
public function userRead(){ return $this->all(); // return $this->find(1); // return $this->where('user_id','>',1)->get(); } public function userAdd(){ $this->username="user2"; $this->age=30; //数组方式添加 // $user_data=['username'=>"user2", 'age'=>"30"]; // $this->fill($user_data); $this->save(); } public function userUpdate(){ // $user = $this->find(21); $users=$this->where('user_id','<=','10'); $users->update(['username'=>'curry','age'=>27]); //save()用于单条数据的操做保存,update自带保存属性 // $users->save(); } public function userDelete(){ $user=$this->find(20); $user->delete(); }
6:数组的赋值与值得访问
controller文件
public function index(){ $data=['name'=>'冬冬','age'=>22]; // return view('index')->with('Laravel',$data); return view('index',$data); }
view文件,直接访问数组的索引
<div class="title">欢迎你<?php echo $name; ?></div> <div class="title">年龄是<?php echo $age; ?></div>
7:变量值的访问
public function page(){ $lang="php"; return view('page',compact('lang')); return view('page')->with('lang',"haskell"); //后面再次赋值会被忽略掉 }
8: 数据的插入
// $this->username='aaron'; // $this->age =26; $data = ["username"=>"linux","age"=>23]; $this->fill($data); $this->save();
9:条件性的更新数据库
$users = $this->where('user_id','>','10'); $users->update(['username'=>"dongdong11019",'age'=>27]);
public function infoAdd(){ $this->name="haskell"; $this->user="lisa"; $this->save(); } public function infoUpdateEasy(){ $data=$this->find(4); $data->name="Golang"; $data->user="xiaoming"; $data->update(); } public function infoUpdate(){ $datas = $this->where("id",'>',3); $datas ->update(["name"=>"javascript","name"=>"jerry"]); } public function infoDel(){ $obj = $this->find(5); $obj -> delete(); }
10: laravel解决表单经过post提交时遇到的问题
“TokenMismatchException in VerifyCsrfToken.php line xx:”
解决方法是经过在表单的开始隐藏提交一个“csrf_token()”值
eg:
<form role="form" action="test" method="post"> //提交这个东东 <input type="hidden" name="_token" value="{{csrf_token()}}"> <div class=""> <label for="">姓名</label> <input type="text" name="name"> </div> <div class=""> <label for="">年龄</label> <input type="text" name="age"> </div> <input type="submit" value="提交"/> </form>
Route::get('testCsrf',function(){ $csrf_field = csrf_field(); $html = <<<GET <form method="POST" action="/testCsrf"> {$csrf_field} <input type="submit" value="Test"/> </form> GET; return $html; });
11:请求检索的4中方法
only-except-url-fullUrl
12: 请求历史处理
存-->flash-flashOnly-flashExcept
取-->old
Route::any("/test", function(){ // $rest = Request::flash(); //所有 // $rest = Request::flashOnly("name"); //惟一 $rest = Request::flashExcept("name"); //出去惟一后的所有 }); Route::any("/test2", function(){ return Request::old(); });
13:将session存储在数据库表中
php artisan session:table
composer dump-autoload
php artisan migrate