Home PHP Framework Laravel How to learn laravel framework in php (novice beginner)

How to learn laravel framework in php (novice beginner)

Jul 16, 2020 am 11:59 AM
laravel

How to learn laravel framework in php (novice beginner)

关于laravel的介绍就不讲了,总之laravel是款比较强大的框架,它是国外框架所以在安装的上面可能比较麻烦。

laravel的安装

首先安装laravel之前要安装composer,如果是linux系统即可直接下载安装,下载完后不能安装记得修改下文件权限用命令chmod,这边主要讲下window下如何使用composer这个工具。 

首先百度搜索中国composer镜像,就可以找到composer config -g repositories.packagist composer http://packagist.phpcomposer.com这条命令,运行cmd在命令行运行上面的命令,就可以下载composer工具,

下载成功后可以看到composer文件底下有个composer.json文件这是一个配置文件,打开配置文件写明php版本信息和要下载的laravel信息,格式如下:

  {
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=5.5.9",
        "laravel/framework": "5.1.*"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "pre-update-cmd": [
            "php artisan clear-compiled"
        ],
        "post-update-cmd": [
            "php artisan optimize"
        ],
        "post-root-package-install": [
            "php -r \"copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    },
    "repositories": [
        {"type": "composer", "url": "http://packagist.phpcomposer.com"},
        {"packagist": false}
    ]
}```
Copy after login

配置好之后输入composer install 进行安装laravel,这边要比较注意的是安装目录的路径问题,如果你想安装在d盘底下就在把命令行切到d目录底下进行安装(在此操作之前要配置好环境变量)。

laravel的目录结构介绍

安装完的第一次肯定是要想怎么去运行它,很简单,直接进入public文件就可以打开一个开始页面,如果在本地的话那就是localhost/laravelproject/public,就可以运行。

接下来介绍下laravel目录结构,首先介绍下public的index.php文件 里面主要是加载了开始文件然后才能成功运行laravel,具体的两个文件你可以在根目录下bootstrap文件夹中找到。现在看下app中的结构:

How to learn laravel framework in php (novice beginner)
view中主要放的是视图文件(创建文件时要用到blade模板,比如创建test.blade.php,laravel中是结合blade模板引擎来调用视图模板)

controller放的是控制器(手动创建时记得要用composer 命令进行更新)

config中主要是配置文件(比如配置数据库时要用到database.php文件)

models主要是放模型(也就是数据库的表)

routes则是路由配置,

filters则是过滤器。

laravel是怎么运行的

刚学习时肯定是要先尝试下如何运行这个laravel,首先手动创建一个controller,文件命名为TestController.php,然打开命令行进入项目的根目录下 执行 composer dumpautoload,里面内容可以模仿homeController.php。

然后编辑routes.php文件,将原来的Route::GET(‘/’,function()…);修改为Route::Get(‘/’,’TestController@showWelcome’); 然后运行也会跳到laravel欢迎界面。

如果Route::Get(‘test’,’TestController@showWelcome’);则在网站根目录下后面直接增加test就可以访问了,到了这里应该明白了怎么到Controller,Controller怎么到View了。

laravel数据库配置

这边用到的是mysql,进行了简单的配置

'mysql' => array(
'driver'    => 'mysql',
'host'      => 'localhost',
'database'  => 'oss',
'username'  => 'root',
'password'  => '',
'charset'   => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix'    => '',
)
Copy after login

laravel的数据库使用

数据表比较多时且数据表的前缀不一样,则可以先配置模型model,在models文件夹中建立一个文件要与表名一样的php文件,内容如下:
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
    use UserTrait, RemindableTrait;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = &#39;users&#39;;
    /**
     * The attributes excluded from the model&#39;s JSON form.
     *
     * @var array
     */
    protected $hidden = array(&#39;password&#39;, &#39;remember_token&#39;);
}
Copy after login

即可以直接使用 User ::all() 查询所有结果 ,User::find(2)查询一个,Post::findOrFail(2)

如果没找到就会返回错误,Post::save()、Post::where()->find()、Post::add()、Post::delete()

数据库的简便操作:

DB::table(‘tablename’)->insert([
        插入多个时要再加一个数组
        [&#39;title&#39;=>&#39;title&#39;,&#39;name&#39;=>&#39;name&#39;]
        [&#39;title&#39;=>&#39;title&#39;]
        [&#39;title&#39;=>&#39;title&#39;]
        ])
        插入时要想得到ID
        DB::table(&#39;tablename&#39;)->insertGetId([&#39;title&#39;=>&#39;titles&#39;])
        更新数据要有ID
        DB::table(&#39;tablename&#39;)->where(&#39;id&#39;,1)->update([&#39;title&#39;=>&#39;titles&#39;])
        删除数据
        DB::table(&#39;tablename&#39;)->where(&#39;id&#39;,1)->delete();
        查询数据
        DB::table(&#39;tablename&#39;)->get();  得到全部的值
        DB::table(&#39;tablename&#39;)->get([&#39;title&#39;]); 只查询title的值
        DB::table(&#39;tablename&#39;)->first();  只拿第一个
        DB::table(&#39;tablename&#39;)->orderBy(&#39;id&#39;,&#39;desc&#39;)->first(); 根据id排序
        DB::table(&#39;tablename&#39;)->where(&#39;id&#39;,&#39;!=&#39;,2)->get(); 不等于2
        DB::table(&#39;tablename&#39;)->where(&#39;id&#39;,&#39;!=&#39;,2)->where(&#39;id&#39;,&#39;>&#39;,5)->get(); 可以使用多个where
        DB::table(&#39;tablename&#39;)->where(&#39;id&#39;,&#39;!=&#39;,2)->OrWhere(&#39;id&#39;,&#39;>&#39;,5)->get(); 或者
        DB::table(&#39;tablename&#39;)->whereBetween(&#39;id&#39;,[2,5])->get();  闭包之间
        DB::table(&#39;tablename&#39;)->whereIn(&#39;id&#39;,[2,5,9])->get();
        DB::table(&#39;tablename&#39;)->whereNotIn(&#39;id&#39;,[2,5,9])->get();
        DB::table(&#39;tablename&#39;)->whereNull(&#39;id&#39;)->get();  为空的话就可以查询出来
        DB::table(&#39;tablename&#39;)->take(3)->get();  只查询3个
        DB::table(&#39;tablename&#39;)->limit(3)->get();  只查询3个
        DB::table(&#39;tablename&#39;)->skip(2)->take(3)->get();  只查询3个跳过第二个
        DB::table(&#39;tablename&#39;)->where(&#39;id&#39;,&#39;!=&#39;,2)->pluck(&#39;title&#39;); 只返回它的title
        DB::table(&#39;tablename&#39;)->count();  有多少条记录
        DB::table(&#39;tablename&#39;)->max(&#39;id&#39;);
        DB::table(&#39;tablename&#39;)->min(&#39;id&#39;);
        DB::table(&#39;tablename&#39;)->avg(&#39;id&#39;);
        DB::table(&#39;tablename&#39;)->sum(&#39;id&#39;);
Copy after login

多表关联

在Post中定义

public function comment(){ return $this->hasMany(&#39;Comment&#39;,&#39;post_id&#39;) }
 正向关联   一对多   一对一是hasOne
Copy after login

在Comment中定义

public function post(){ return $this->belongsTo(&#39;Post&#39;,&#39;post_id&#39;) }
  反向关联
Copy after login

取得关联值

    Post::find(2)->comment  就可以得到Comment这张表的内容   //这样查询一个是可以的  查询多个就要设置预载入
            查询多个
                Post::with(&#39;comment&#39;)->get();
                Post::with([&#39;comment&#39;=>function($query){$query->where(&#39;id&#39;,&#39;>&#39;,2)}])->get();  加条件
Copy after login

感谢大家的阅读,希望大家有所收益。

本文转自:https://blog.csdn.net/Happy_CSDN/article/details/49363219

推荐教程:《php教程

The above is the detailed content of How to learn laravel framework in php (novice beginner). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the return code when email sending fails in Laravel? How to get the return code when email sending fails in Laravel? Apr 01, 2025 pm 02:45 PM

Method for obtaining the return code when Laravel email sending fails. When using Laravel to develop applications, you often encounter situations where you need to send verification codes. And in reality...

Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Laravel schedule task is not executed: What should I do if the task is not running after schedule: run command? Mar 31, 2025 pm 11:24 PM

Laravel schedule task run unresponsive troubleshooting When using Laravel's schedule task scheduling, many developers will encounter this problem: schedule:run...

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

The method of handling Laravel's email failure to send verification code is to use Laravel...

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

Laravel - Dump Server Laravel - Dump Server Aug 27, 2024 am 10:51 AM

Laravel - Dump Server - Laravel dump server comes with the version of Laravel 5.7. The previous versions do not include any dump server. Dump server will be a development dependency in laravel/laravel composer file.

Laravel Redis connection sharing: Why does the select method affect other connections? Laravel Redis connection sharing: Why does the select method affect other connections? Apr 01, 2025 am 07:45 AM

The impact of sharing of Redis connections in Laravel framework and select methods When using Laravel framework and Redis, developers may encounter a problem: through configuration...

Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Laravel multi-tenant extension stancl/tenancy: How to customize the host address of a tenant database connection? Apr 01, 2025 am 09:09 AM

Custom tenant database connection in Laravel multi-tenant extension package stancl/tenancy When building multi-tenant applications using Laravel multi-tenant extension package stancl/tenancy,...

Laravel - Action URL Laravel - Action URL Aug 27, 2024 am 10:51 AM

Laravel - Action URL - Laravel 5.7 introduces a new feature called “callable action URL”. This feature is similar to the one in Laravel 5.6 which accepts string in action method. The main purpose of the new syntax introduced Laravel 5.7 is to directl

See all articles