php laravel架構學習筆記 (二) 資料庫操作

來源:互聯網
上載者:User

標籤:https   primary   樣本   shell   ssi   where   方法   san   成功   

 

原部落格連結: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架構學習筆記 (二) 資料庫操作

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.