1. 程式人生 > >php laravel框架學習筆記 (二) 數據庫操作

php laravel框架學習筆記 (二) 數據庫操作

true 數據 mar sql show top 一行 ati del

原博客鏈接:http://www.cnblogs.com/bitch1319453/p/6810492.html

mysql基本配置

你可用通過配置環境變量,使用cmd進入mysql,當然還有一種東西叫做mysql console

創建一個數據庫 create database [數據庫名] [選項];

展示已經創建的數據庫 show datebases;

在登錄後使用 use 語句指定數據庫 use 數據庫名;

展示表show tables;(需要先指定數據庫)

展示表的內容desc [表名];

暫時會這些命令就可以。因為表的創建,刪除,版本管理可以通過migration完成

通過tinker管理mysql

為展示基礎的增刪改查功能,我們先創建一張表。在cmd中輸入

php artisan make:migration creat_articles_table --create=articles (php artisan make:migration creat_articles_table --[選項字段表示表的名字])

然後你會發現在migration文件夾下多了一個*_articles_*.php,修改其中的up

    public function up()
    {
        Schema::create(‘articles‘, function (Blueprint $table) {
            $table->increments(‘id‘);
            $table->string(‘title‘);
            $table->text(‘content‘);
            $table->timestamp(‘pushed_at‘);
            $table->timestamps();
        });
    }

此時並沒有同步到數據庫。你需要php artisan migrate。

嘛,然後在mysql上面檢查一下看看有沒有表吧

---

使用這個命令php artisan make:mode article在app文件夾下創建一個article.php用於管理數據庫。可以發現之前創建的數據庫名字叫articles,但我在article下成功操作了articles數據庫。似乎laravel有某種匹配機制可以去找對應的數據庫

在php中輸入php artisan tinker打開一個類似shell的東西,

1.增

你可以這樣操作

$article=new App\Article

$article->title=‘My first Title‘;

$article->content=‘content‘;

$article->published_at=Carbon\Carbon::now();

$article->save();

給字段賦值,然後保存到數據庫,嘛增添就做完啦。

2.刪

同樣也是給出一個示例,where查找所有title字段為空的記錄然後全部刪掉。這個title是字符串就這樣判空。若不是char型可以用null判空

$article=App\article::where(‘title‘,‘=‘,‘)->delete();

3.改

$article=App\article::find(6);

$article->title=‘fuck‘;

$article->save();

找到修改記得保存

4.查

where查或者find查,方法如上?還是甩個鏈接比較穩官方eloquent文檔

嘛,你要是問我在cmd裏敲的這樣的東西有什麽用在代碼裏改才是真理,其實把cmd裏敲的放代碼裏邏輯一樣跑得通

通過提交表單向數據庫存入數據

先註冊路由

Route::get(‘/article/create‘,[email protected]);
Route::post(‘/article/store‘,[email protected]);

視圖裏/article/create創一個create.blade.php,裏面寫( ps:laravel5.2以上版本要配置form才能用)

@extends(‘app‘)
@section(‘content‘)
    <h1> add new article</h1>
    {!! Form::open([‘url‘=>‘article/store‘]) !!}
        <div class="form-group">
                {!! Form::label(‘title‘,‘Title:‘) !!}
                {!! Form::text(‘title‘,null,[‘class‘=>‘form-control‘]) !!}
        </div>
        <div class="form-group">
            {!! Form::label(‘content‘,‘Content:‘) !!}
            {!! Form::textarea(‘content‘,null,[‘class‘=>‘form-control‘]) !!}
        </div>
        <div class="form-group">
            {!! Form::submit(‘post‘,[‘class‘=>‘btn btn-primary form-controller‘]) !!}
        </div>
    {!! Form::close() !!}
@stop

表單提交到了‘url‘=>‘article/store‘這個東西裏,就是把數據交給這個store來存。在ArticlesController寫

 public function store(Request $request){
        $input=$request->all();
        $input[‘pushed_at‘]=Carbon::now();
        //dd($input);
        Article::create($input);
        return redirect(‘/‘);
    }

存數據庫的地方就一行 Article::create($input);

是的create函數放在cmd裏也能跑通。因為laravel的默認設置,你只需要在app/article.php裏面寫(就是上面用命令創的那個東西)

protected $fillable=[‘title‘,‘content‘,‘pushed_at‘];

就能跑通create了

參考資料:https://laravel.com/docs/5.1/eloquent#mass-assignment

php laravel框架學習筆記 (二) 數據庫操作