laravel学习篇-基础篇

本文是Laravel学习的基础篇,涵盖了从安装到数据库操作的全过程。首先介绍如何安装Laravel,然后讲解路由的设定及其与控制器的结合使用。接着,通过控制器将数据传递给视图进行展示,并探讨了视图的输出方式。进一步,文章讨论了如何配置和连接数据库,以及使用原始SQL和查询构造器进行数据库操作。最后,重点介绍了Model模型在Laravel中的应用。
摘要由CSDN通过智能技术生成

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;
	}


}


?>

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值