PHP laravel Framework Learning Notes (ii) database operations

Source: Internet
Author: User

Original Blog Link: http://www.cnblogs.com/bitch1319453/p/6810492.html

MySQL Basic configuration

You can use CMD to enter MySQL by configuring environment variables, and of course there's a thing called MySQL console

Create a database (database name) [option];

Show the database you have created show datebases;

Using the USE statement after logging in to specify the database usage database name;

Show table show tables; (You need to specify a database first)

Content of the Display table desc [table name];

These commands will be available for the time being. Version management can be completed by migration because the table is created, deleted, and

Manage MySQL with Tinker

In order to display the basic additions and deletions, we first create a table. Enter in cmd

PHP artisan make:migration creat_articles_table--create=articles (php artisan make:migration creat_articles_table--[selection Item field represents the name of the table])

Then you will find a *_articles_*.php in the Migration folder, modify the 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 ();        });    }

There is no synchronization to the database at this time. You need PHP artisan migrate.

And check it out on MySQL to see if there's a watch.

---

Using this command PHP artisan make:mode article Create a article.php in the app folder to manage the database. You can find that the database you created earlier is named articles, but I successfully manipulated the articles database under article. It seems that Laravel has some kind of matching mechanism to find the corresponding database.

Enter PHP artisan Tinker in PHP to open a shell-like thing,

1. Increase

You can do this.

$article =new app\article

$article->title= ' My first title ';

$article->content= ' content ';

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

$article->save ();

Assign a value to a field, and then save it to the database, and the addition is done.

2. By deleting

It is also given an example where to find all records where the title field is empty and then delete all. This title is a string that is then empty. If it is not a char type, you can empty it with null

$article =app\article::where (' title ', ' = ', ')->delete ();

3. Change

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

$article->title= ' fuck ';

$article->save ();

Find changes remember to save

4. Check

Where check or find, method like above? Or just throw a link and stabilize the official eloquent document

Well, if you ask me what I'm doing in the CMD, what's the use of the code to change the truth, in fact, the CMD in the code to play the logic of the same run through

Depositing data by submitting a table-to-one database

Register the Route first

Route::get ('/article/create ', ' [email protected] '); Route::p ost ('/article/store ', ' [email protected] ');

View/article/create Create a create.blade.php, inside write (ps:laravel5.2 above version to configure form to use)

@extends (' app ') @section (' content ')    

The form was submitted to the ' url ' = ' article/store ' This thing, which is to give the data to this store to save. Write in Articlescontroller

Public function Store (Request $request) {        $input = $request->all ();        $input [' Pushed_at ']=carbon::now ();        DD ($input);        Article::create ($input);        Return redirect ('/');    }

The place where the database is stored is a row of article::create ($input);

Yes, the CREATE function can run through CMD. Because of the default settings of Laravel, you only need to write in app/article.php (that is, the thing created with the command above)

Protected $fillable =[' title ', ' content ', ' pushed_at '];

You can run through create.

Reference: Https://laravel.com/docs/5.1/eloquent#mass-assignment

PHP laravel Framework Learning Notes (ii) database operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.