新手怎么学习Laravel,有哪些基础知识要掌握
Admin 2022-06-16 群英技术资讯 1265 次浏览
这篇文章给大家介绍了“新手怎么学习Laravel,有哪些基础知识要掌握”的相关知识,讲解详细,步骤过程清晰,有一定的借鉴学习价值,因此分享给大家做个参考,感兴趣的朋友接下来一起跟随小编看看吧。
composer create-project laravel/laravel 项目文件夹名 --prefer-dist

app:应用程序的核心代码
bootstrap:一个引导框架的app.php文件,一个cache目录(包含路由及缓存文件),框架启动文件,一般情况不动。
config:所有配置文件
database:其中migrations目录可以生成数据表。
public:入口文件存放地,以及静态资源(和tp类似)
resources:
routes:应用的所有路由定义
tests:可用来单元测试
vendor:所有composer依赖包
Route::match(['get','post'],'/',function(){});Route::any('/home', function () {
});Route::get('/home/{id}', function ($id) {
echo 'id为:'.$id;});Route::get('/home/{id?}', function ($id = '') {
echo 'id为:'.$id;});Route::get('/home', function () {
echo 'id为:'.$_GET['id'];});Route::any('/home/index', function () {
echo '测试';})->name('hh');例如有如下路由:
如果一个一个添加是比较麻烦的,他们有一个共同的区别,都是有/admin/前缀,可设置一个路由群组进行添加:
Route::group(['prefix'=>'admin'], function () {
Route::get('test1', function () {
echo 'test1';
});
Route::get('test2', function () {
echo 'test2';
});});此时就可通过/admin/test1来进行访问了。
控制器可以建一个前台和一个后台:

命令行创建路由:
php artisan make:controller Admin/IndexController
基本路由建立:
Route::get('test/index','TestController@index');分目录路由建立:
Route::get('/admin/index/index','Admin\IndexController@index');引入:use Illuminate\Support\Facades\Validator
$param = $request->all();$rule = [
'name'=>'required|max:2',];$message = [
'required' => ':attribute不能为空',
'max' => ':attribute长度最大为2'];$replace = [
'name' => '姓名',];$validator = Validator::make($param, $rule, $message,$replace);if ($validator->fails()){
return response()->json(['status'=>0,'msg'=>$validator->errors()->first()]);}在控制器中如果要使用一个类,例如use Illuminate\Http\Request,就可以简写为use Request。
但是需要在config目录下的app.php配置文件中加入:
'aliases' => [
'App' => Illuminate\Support\Facades\App::class,
'Arr' => Illuminate\Support\Arr::class,
'Artisan' => Illuminate\Support\Facades\Artisan::class,
'Auth' => Illuminate\Support\Facades\Auth::class,
'Blade' => Illuminate\Support\Facades\Blade::class,
'Request' => Illuminate\Support\Facades\Request::class,
],Input::get('id')Input::all()
打印出来的是数组
关于dd(dump+die)
Input::only(['id','name'] //只接收id,其余不接受
Input::except(['name'] //不接收name,其余都接收
Input::has('name') //存在返回true 不存在返回false 其中0返回true视图也可分目录管理:

控制器语法:
return view('home/test');也可写为:
return view('home.test');控制器中:
return view('home/test',['day'=>time()]);视图中:
{{$day}}其中控制器中变量映射有三种:
了解一下compact数组。
控制器中:
public function index(){
$arr = [
0 => [
'name' => 'tom',
'age' => '12',
],
1 => [
'name' => 'bby',
'age' => '13',
]
];
return view('home/test',['data'=>$arr]);
}视图中:
@foreach($data as $k=>$v)
键:{{$k}}
值:{{$v['name']}} <br/>@endforeach@if(1==2)
是的
@else
不是的
@endif@include('welcome')php artisan make:model Model/Admin/Member
此时,就会在app目录内创建:
<?phpnamespace App\Model\Admin;use Illuminate\Database\Eloquent\Model;class Member extends Model{
//定义表名
protected $table = 'student';
//定义主键
protected $primaryKey = 'id';
//定义禁止操作时间
public $timestamps = false;
//设置允许写入的字段
protected $fillable = ['id','sname'];}方式一:
$model = new Member(); $model->sname = '勒布朗'; $res = $model->save(); dd($res);
方式二:
$model = new Member();
$res = $model->create($request->all());
dd($res);//查询客户与销售顾问的客资列表$data = Custinfo::select(['custinfo.*', 'customers.name'])
->join('customers', 'customers.id', '=', 'custinfo.cust_id')
->where($where)
->get()
->toArray();<?phpnamespace App\Model\Admin;use Illuminate\Database\Eloquent\Model;class Phone extends Model{
//定义表名
protected $table = 'phone';
//定义主键
protected $primaryKey = 'id';
//定义禁止操作时间
public $timestamps = false;
//设置允许写入的字段
protected $fillable = ['id','uid','phone'];}<?phpnamespace App\Model\Admin;use Illuminate\Database\Eloquent\Model;class Member extends Model{
//定义表名
protected $table = 'student';
//定义主键
protected $primaryKey = 'id';
//定义禁止操作时间
public $timestamps = false;
//设置允许写入的字段
protected $fillable = ['id','sname'];
/**
* 获取与用户关联的电话号码记录。
*/
public function getPhone()
{
return $this->hasOne('App\Model\Admin\Phone', 'uid', 'id');
}} //对象转数组
public function Arr($obj)
{
return json_decode(json_encode($obj), true);
}
public function index(){
$infoObj = Member::with('getPhone')->get();
$infoArr = $this->Arr($infoObj);
print_r($infoArr);
}

在config目录下的logging.php中的channels配置:
'custom' => [
'driver' => 'single',
'path' => storage_path('logs/1laravel.log'),
'level' => 'debug',
]控制器中:
$message = ['joytom','rocker'];Log::channel('custom')->info($message);建立一个迁移文件:php artisan make:migration create_shcool_table
会在database\migrations下创建一个文件:
在up方法中增加如下代码:
<?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;class CreateShcoolTable extends Migration{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('shcool', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('school_name','20')->notNull()->unique();
$table->tinyInteger('status')->default(1);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('shcool');
}}更详细的生成SQL方法请参考:数据迁移文件常用方法速查表
写好SQL文件以后,执行:php artisan migrate
将会生成数据表,其中操作日志将记录在这个表中:

php artisan migrate:rollback:回滚最后一次的迁移操作, 删除(回滚)之后会删除迁移记录,并且数据表也会删除,但是迁移文件依旧存在,方便后期继续迁移(创建数据表)。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
php数组进行堆栈的模拟:1、堆栈容器中,最后进栈的将会被最先出栈,即所谓的“先进后出”的数据结构;2、将数组当做一个栈,使用array_push()函数完成进栈操作;3、完成所有数据进栈之后,先进栈的在最下面。
对于PHP初学者,可能不太清楚超全局变量是什么,这篇文章就给大家介绍一下php超全局变量以及功能,有这方面学习需求的朋友就继续往下看吧。
命令模式:命令模式(CommandPattern):将一个请求封装为一个对象,从而使我们可用不同的请求对客户进行参数化;对请求排队或者记录请求日志,以及支持可撤销的操作。命令模式是一种对象行为型模式,其别名为动作(Action)模式或事务(Transaction)模式。模式动机:在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接收者是谁,也不知道被请求的操作是哪个,
本篇文章由PHP7教程栏目给大家介绍一下PHP7的一些特性用法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
下面由phpstorm教程栏目给大家介绍phpstrom 插件卸载的方法,希望对需要的朋友有所帮助!
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008