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

 

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

mysql基本配置php

你可用經過配置環境變量,使用cmd進入mysql,固然還有一種東西叫作mysql consolehtml

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

展現已經建立的數據庫 show datebases;laravel

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

展現表show tables;(須要先指定數據庫)shell

展現表的內容desc [表名];數據庫

暫時會這些命令就能夠。由於表的建立,刪除,版本管理能夠經過migration完成app

經過tinker管理mysql函數

爲展現基礎的增刪改查功能,咱們先建立一張表。在cmd中輸入post

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','ArticlesController@create');
Route::post('/article/store','ArticlesController@store');

 視圖裏/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

相關文章
相關標籤/搜索