laravel入门基础知识 1

Composer下载Laravel:

composer create-project --prefer-dist laravel/laravel:^5.7 you_project

php使用通过migration新建或更新数据表结构:

创建表:

#使用以下任意一行命令即可
php artisan make:migration create_table_myUser

php artisan make:migration create_users_table --create=my_user

php artisan make:migration add_votes_to_users_table --table=my_user

输入命令后,可在目录database/migrations/下看到可以创建数据表的php文件(2023_07_06_140104_create_table_my_user.php):

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableMyUser extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('table_my_user', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('table_my_user');
    }
}

通过以下命令执行该文件,生成数据表:

php artisan migrate

#在生产环境强制执行迁移,非必要不使用
php artisan migrate --force

返回如下结果,就是成功:
在这里插入图片描述

如果想要回滚到之前的数据表结构,就可以执行以下命令:

#回滚到上一次
php artisan migrate:rollback
#step 参数是用来限制回滚迁移的个数的,以下命令将回滚最近三次迁移
php artisan migrate:rollback --step=3
#以下命令将回滚所有迁移
php artisan migrate:reset

返回如下结果,就是成功:
在这里插入图片描述

也可以对该php文件(2023_07_06_140104_create_table_my_user.php)做一些简单的修改,实现你想要的效果:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTableMyUser extends Migration
{
    /**
     * Run the migrations.
     * 用于添加新的数据表, 字段或者索引到数据库
     * @return void
     */
    public function up()
    {
        Schema::create('table_my_user', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('username', 25)->unique();//即该字段不可重复,指定长度为255
            $table->string('password')->nullable();//即结构为string,且可为空
            $table->text('desc');
//            $table->timestamps();
//            $table->dateTime('created_at');//时间
//            $table->dateTimeTz('created_at');//相当于带时区 DATETIME
            $table->date('created_at');//日期
            $table->decimal('round', 8, 2);//带有精度与基数 DECIMAL
//            $table->float('round', 8, 2);
            $table->json('user_info');//相当于json
            $table->integer('user_num')->default(0);//int整型,默认为0
//            $table->tinyInteger('tiny_user_num');
            //判断表是否存在
            if (Schema::hasTable('my_user')) {
                //判断表中是否存在指定字段
                if (Schema::hasColumn('my_user', 'email')) {
                }
            }


//            Schema::connection('foo')->create('my_user', function (Blueprint $table) {
//                $table->increments('id');
//            });//对非默认连接的数据库连接执行结构操作,可以使用 connection 方法



        });

        // Schema::rename('table_my_user','table_my_user_real');//修改表名,将my_user改为my_user_real
    }

    /**
     * Reverse the migrations.
     * 回滚数据库迁移
     * @return void
     */
    public function down()
    {
        // Schema::dropIfExists('table_my_user_real');//记得如果要修改表名,在回滚的时候就要删除对应的表名
        Schema::dropIfExists('table_my_user');//记得如果要修改表名,在回滚的时候就要删除对应的表名
    }
}

如果对创建的表不确定,可以通过以下命令来查看创建数据表的sql语句是否符合你的需求:

php artisan migrate --pretend

返回结果如下:
在这里插入图片描述

其他php创建文件命令:

#创建model文件
php artisan make:model User

如果Laravel默认的命令行中没有你想要的,也可以通过以下命令自行创建:

php artisan make:command MakeView

该命令将在 app/Console/Commands 目录中创建一个新的命令类(MakeView.php):

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class MakeView extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}

可以修改以上代码达到通过命令行生成blade文件的目的,具体代码如下:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class MakeView extends Command
{
    /**
     * 命令名称及签名
     *
     * @var string
     */
    protected $signature = 'make:view {name}';

    /**
     * 命令描述
     *
     * @var string
     */
    protected $description = 'Create a new Blade view file';

    /**
     * 创建命令
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 执行命令
     *
     * @return mixed
     */
    public function handle()
    {
        $name = $this->argument('name');
        $path = 'views/' . $name . '.blade.php';
        $dir = dirname('resources/'.$path);
        if(!file_exists($dir)){
            mkdir($dir, 0777, true);
        }
        
        
        $viewPath = resource_path($path);

        if (File::exists($viewPath)) {
            $this->error('View already exists!');
        } else {
            File::put($viewPath, '');
            $this->info('View created successfully!');
        }
        
    }
}

只要输入以下命令就会在resources/views目录下生成test.blade.php文件,如果有其他需要也可自行修改以上代码:

php artisan make:view test
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值