Laravel框架学习(路由、控制器、模型)

1、路由

Route::any('/', function () {
    return view('welcome');
});

/* 路由群组 */
Route::group(['prefix'=>'member'],function(){

	Route::any('test1', ['as'=>'center',function () {
	    /*return route('center');*/
	    return view('welcome');
	}]);

});

Route::get('info',['uses'=>'TestController@index']);

2、控制器

'哈哈']);
    }
}

3、视图

4、模型

5、使用DB façade查询构造器操作数据库

 ?',[0]);
			var_dump($num);
		

				
			//使用查询构造器
			//简单插入
			$bool = DB::table('student')->insert(['name'=>'Li','age'=>18]);
			var_dump($bool);
			//插入之后获取id
			$id = DB::table('student')->insertGetId(['name'=>'LiL','age'=>28]);
			var_dump($id);
			//插入多条数据
			$bool = DB::table('student')->insert([
				['name'=>'Lpi','age'=>16],
				['name'=>'Lia','age'=>108],
			]);
			var_dump($bool);

			//更新
			$num = DB::table('student')->where('id',2)->update(['age'=>50]);
			$num = DB::table('student')->increment('age',1);
			$num = DB::table('student')->decrement('age',1,['name'=>'啦啦啦']);
			var_dump($num);

			//删除
			$num = DB::table('student')->where('id','>=',4)->delete();
			var_dump($num);
			/////////多注释几个以防万一/////////$num = DB::table('student')->truncate();
			var_dump($num);

			//获取表的所有数据
			$students = DB::table('student')->get();
			dd($students);
			$students = DB::table('student')->orderBy('id','desc')->where('id','>=',2)->get();
			dd($students);
			//获取结果信息第一条
			$students = DB::table('student')->orderBy('id','desc')->first();
			dd($students);
			//多条件查询
			$students = DB::table('student')->where('id','>',2)->where('id','<',4)->get();
			dd($students);
			$students = DB::table('student')->whereRaw('id >= ? and id < ?',[2,4])->get();
			dd($students);
			//pluck
			$names = DB::table('student')->pluck('name');
			dd($names);
			//指定键名
			$names = DB::table('student')->pluck('name','id');
			dd($names);
			//select
			$students = DB::table('student')->select('id','name','age')->get();
			dd($students);
			//chunk
			DB::table('student')->orderBy('id','desc')->chunk(2,function($students){
				var_dump($students);
				if (condition) {
					return false;
				}
			});

			//聚合函数
			$num = DB::table('student')->count();
			$max = DB::table('student')->max('age');
			$min = DB::table('student')->min('age');
			$avg = DB::table('student')->avg('age');
			$sum = DB::table('student')->sum('age');
		
    }
    
}
 

6、使用ORM进行数据库操作

//模型
//操作
',1)->orderBy('age','desc')->first();
			Student::orderBy('id','desc')->chunk(2,function($students){
				var_dump($students);
			});
			$num = Student::count();

			//使用模型新增数据
			$student = new Student();
			$student->name = '哈哈';
			$student->age  = 108;
			$bool = $student->save();
			dd($bool);
			//使用模型Create方法新增数据
			$student = Student::create(['name'=>'ico','age'=>18]);
			//firstOrCreate以属性查找用户,若没有则新增
			$student = Student::firstOrCreate(['name'=>'ipco']);
			//firstOrNew以属性查找用户,若没有则返回新的实例,若要保存可保存实例
			$student = Student::firstOrNew(['name'=>'ipcoooo']);
			$bool = $student->save();
			dd($bool);

			//通过模型更新数据
			$student = Student::find(1);
			$student->name = 'kitty';
			$bool = $student->save();
			var_dump($bool);
			$num = Student::where('id','>',2)->update(['age'=>41]);
			var_dump($num);

			//通过模型删除
			$student = Student::find(1);
			$bool = $student->delete();
			//通过主键删除
			$num = Student::destory(1);
			$num = Student::destory(2,3);
			$num = Student::destory([2,3]);
			$num = Student::where('id','>',1)->delete();
    }
}
 

 

你可能感兴趣的:(PHP,mysql,laravel)