1.安装
2.路由的写法
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('hello',function(){
return 'hello world';
});
Route::post('color',function(){
return 'post';
});
Route::match(['get','post'],'color1',function(){
return 'pogt';
});
Route::any('multy',function(){
return 'multy2';
});
//路由参数
Route::get('user/{id}',function($id){
return 'id-'.$id;
});
Route::get('user/{name?}',function($name = null){
return 'name-'.$name;
});
//正则判断
Route::get('password/{id?}/{password?}',function($id,$password){
return 'password:'.$password.'--id:'.$id;
})->where(['id'=>'[0-9]+','password'=>'[A-Za-z]+']);
//路由别名
Route::get('home/usercenter',['as'=>'center',function(){
return route('center');
}]);
//路由群组
Route::group(['prefix' => 'member'],function(){
Route::get('home/list',function(){
return 'member-home-list';
});
Route::get('rou',function(){
return 'member-rou';
});
});
//路由中输出视图
Route::get('view',function(){
return view('welcome');
});
3.路由和控制器联系在一起
<?php
//路由和控制器联系在一起
Route::get('member/index','MemberController@index');
Route::get('member/index',['uses'=>'memberController@index']);
//加别名
Route::get('member/index',
[
'uses'=>'memberController@index',
'as' =>'memberinfo'
]
);
4.控制器输出到视图
Http/MemberController.php
<?php
namespace App\Http\Controllers;
class MemberController extends Controller
{
public function index()
{
return view('home/index',['name'=>'color']);
}
}
?>
5.视图输出
{{$name}}
6.Model模型
7.连接数据库
在config.php下面的database.php
.env文件有具体信息修改
8.操作数据库 - 原始sql
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class StudentController extends Controller
{
public function index()
{
//查询
$stu = DB::select('select * from student');
//插入
//$bool = DB::insert('insert into student(name,age) values (?,?)',['color','18']);
//修改
//$num = DB::update('update student set age = ? where name = ?',['20','color']);
//删除
$del = DB::delete('delete from student where id > ?',[2]);
dd($del);
//return 'student-index';
}
}
9.查询构造器
<?php
namespace App\Http\Controllers;
use App\Student;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class StudentController extends Controller
{
public function index()
{
//查询
$stu = DB::select('select * from student');
//插入
//$bool = DB::insert('insert into student(name,age) values (?,?)',['color','18']);
//修改
//$num = DB::update('update student set age = ? where name = ?',['20','color']);
//删除
$del = DB::delete('delete from student where id > ?',[2]);
dd($del);
//return 'student-index';
}
public function check()
{
//$bool = DB::table('student')->insert(['name'=>'color','age'=>'20']);
//$id = DB::table('student')->insertGetId(['name'=>'mycolor','age'=>'20']);
$bool = DB::table('student')->insert(
[
['name'=>'colorrrr','age'=>'19'],
['name'=>'colorrrr','age'=>'19'],
['name'=>'colorrrr','age'=>'19'],
['name'=>'colorrrr','age'=>'19'],
['name'=>'colorrrr','age'=>'19']
]
);
dd($bool);
}
public function query2()
{
//$num = DB::table('student')->where(['id'=>[4]])->update(['age'=>'30']);
//$num = DB::table('student')->increment('age',5);
$num = DB::table('student')->where('id',5)->decrement('age',5,['name'=>'wyl']);
dd($num);
}
public function query3()
{
//$num = DB::table('student')->where('id','>=',5)->delete();
//$num = DB::table('student')->whereRaw('id >= ? and age > ?',['3','18'])->get();
//$news = DB::table('student')->pluck('name');
//$news = DB::table('student')->select('name')->get();
//dd($news);
}
public function query4()
{
//$num = DB::table('student')->count();
//$num = DB::table('student')->max('age');
//$num = DB::table('student')->min('age');
//$num = DB::table('student')->avg('age');
$num = DB::table('student')->sum('age');
dd($num);
}
public function query5()
{
//$student = new Student();
//$num = Student::all();
//$num = $student ->allperson();
//$num = Student::find('1');
//$num = student::findOrFail('111');
//var_dump($num);
//$num = Student::get();
//$num = Student::max('age');
//$num = Student::where('id','>','5')->orderBy('age')->first();
echo '<pre>';
$num = Student::chunk(2,function($students){
var_dump($students);
});
//dd($num);
}
}
10.关于Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Student extends Model{
protected $table = 'student';
protected $primaryKey = 'id';
public function allperson()
{
$num = Student::all();
return $num;
}
}
?>