laravel数据库

创建建表文件

php artisan make:migration create_comments_table

这里写图片描述
打开database/migrations/xxx_create_comments_table.php:
编辑代码

<?php

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

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->integer('article_id');
            $table->integer('user_id');
            $table->string('content');
            $table->timestamps();
        });
    }

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

生成表

php artisan migrate

这里写图片描述

框架自带的代码—这个自带的运行时候报错 稍微修改即可

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

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

class CreatePasswordResetsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('password_resets', function (Blueprint $table) {
            $table->increments('id');
            $table->string('email');
            $table->string('token');
            $table->timestamp('created_at');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('password_resets');
    }
}

这里写图片描述


php artisan migrate:rollback // 会调用建表文件中的down方法,把数据库结构恢复到最初状态

参考链接
https://laravel-china.org/docs/laravel/5.6/seeding/1401#writing-seeders

数据库使用

//测试
Route::get('test1','PhotoController@test1');
Route::get('test2','PhotoController@test2');
 //DB facade实现CURD public function test1(){ // $res = DB::select('select * from photo where Id > ?',[8]); // dd($res); // $timestamp = date('Y-m-d H:i:s',time()); // $res = DB::insert('insert into photo(src,created_at,updated_at) value(?,?,?)',['www.baidu.com',$timestamp,$timestamp]); // var_dump($res);//Boolean值 // $res = DB::update('update photo set src = ? , updated_at = ? where Id > ?',[$timestamp,$timestamp,7]); // var_dump($res);//修改的行数 // $res = DB::delete('delete from photo where Id > ?',[0]); // var_dump($res);//删除的行数 }
 //查询构造器 public function test2(){ $timestamp = date('Y-m-d H:i:s',time()); // $res = DB::table('photo')->insert( // [ // 'src' => str_random(10), // 'age' => random_int(20,99), // 'created_at' => $timestamp, // 'updated_at' => $timestamp // ] // ); // var_dump($res);//Boolean值 // // $res = DB::table('photo')->insertGetId( // [ // 'src' => str_random(10), // 'age' => random_int(20,99), // 'created_at' => $timestamp, // 'updated_at' => $timestamp // ] // ); // var_dump($res);//Id // // $res = DB::table('photo')->insert([ // [ // 'src' => str_random(10), // 'age' => random_int(20,99), // 'created_at' => $timestamp, // 'updated_at' => $timestamp // ], // [ // 'src' => str_random(10), // 'age' => random_int(20,99), // 'created_at' => $timestamp, // 'updated_at' => $timestamp // ] // ]); // var_dump($res);//Id // $res = DB::table('photo')->where('id','>','20') // ->update( // ['src' => $timestamp , 'updated_at'=>$timestamp] // ); // var_dump($res);//行数 // $res = DB::table('photo')->where('id','11') // ->update( // ['src' => $timestamp , 'updated_at'=>$timestamp] // ); // var_dump($res);//行数 // $res = DB::table('photo')->increment('age');//自增1 // $res = DB::table('photo')->increment('age',3);//自增3 // $res = DB::table('photo')->decrement('age');//自减1 // $res = DB::table('photo')->decrement('age',3);//自减1 // var_dump($res);//行数 // $res = DB::table('photo')->where('id',1)->decrement('age');//自减1 // var_dump($res);//行数 // $res = DB::table('photo')->where('id',1)->decrement('age',3,['src' => 'as']);//自减1 修改数据 // var_dump($res);//行数 // $res = DB::table('photo') // ->where('Id',3) // ->delete(); // var_dump($res);//行数 // $res = DB::table('photo')->count(); // var_dump($res);//条数 // // $res = DB::table('photo')->max('age'); // var_dump($res);//最大值 // // $res = DB::table('photo')->min('age'); // var_dump($res);//最小值 // // $res = DB::table('photo')->avg('age'); // var_dump($res);//平均值 // // $res = DB::table('photo')->sum('age'); // var_dump($res);//和 $res = DB::table('photo')->select()->get(); dd($res); $res = DB::table('photo')->select('src','age')->get(); dd($res); }

参考地址
https://laravel-china.org/docs/laravel/5.6/queries/1398#selects

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值