[Laravel] ORM

繁體中文:

ORM全文為Object Relational Mapper

ORM會直接與資料做對應,開啟app/models/User.php,此檔就是直接與資料庫的users資料表做連結。

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {
  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'users';
......
}

開啟app/routes.php:查詢資料

Route::get('/', function()
{
  // 查詢:可直接使用下面方式來取得資料,分別是where條件式、全得全部、由id來找特定資料
  $users = User::where("username", "!=", "carlos")->get();
  $users = User::all();  // select * from users
  $users = User::find(1);  // by id
  return $users;

  // 查詢:依特定欄位排序
  return User::orderBy("username", "asc")->get();
  // 指定抓符合的前3筆資料
  return User::orderBy("username", "asc")->take(3)->get();
});

開啟app/routes.php:新增資料

Route::get('/', function()
{
  // 新增的第一種方式
  $user = new User;  // new一個User的物件
  $user->username = 'NewUser';  // 將NewUser傳給$user->username準備儲存
  $user->password = Hash::make('password_string');  // 將密碼字串加密後,傳給$user->password準備儲存
  $user->save();  // 存至資料庫
  
  // 新增的第二種方式:此方式因安全性考量會報錯,故要在app/models/User.php加上protected $fillable = ["username", "password"];
  User::create([
    "username" => "carlos7",
    "password" => Hash::make("password_string")
  ]);

  return User::all();  // 確認資料是否有存入
});

以上要注意users資料表的欄位必須有updated_at以及created_at兩個欄位的存在,因為Laravel的ORM預設會當這兩個欄位是存在的,並且會以timestamp的形式存入。若要取消此限制,需開啟app/models/User.php

class User extends Eloquent implements UserInterface, RemindableInterface {
  // 將$timestamps設成false
  public $timestamps = false;

開啟app/routes.php:更新資料

// 更新資料表特定id的資料
Route::get('/', function()
{
  $user = User::find(2);
  $user->username = "update_name";
  $user->save();
});

開啟app/routes.php:刪除資料

// 刪除特定id的列資料
Route::get('/', function()
{
  $user = User::find(2);
  $user->delete();
});

English Version:

ORM means “Object Relational Mapper”

It will map with table of database. Open “app/models/User.php”, this file just map with users table.

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {
  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'users';
......
}

Open “app/routes.php”: query data

Route::get('/', function()
{
  // Query:you can use the following methods to get data: where condition, get all data and specific id
  $users = User::where("username", "!=", "carlos")->get();
  $users = User::all();  // select * from users
  $users = User::find(1);  // by id
  return $users;

  // Query:order by specific column
  return User::orderBy("username", "asc")->get();
  // fetch three records from condition
  return User::orderBy("username", "asc")->take(3)->get();
});

Open “app/routes.php”: insert data

Route::get('/', function()
{
  // first method about insert
  $user = new User;  // new User object
  $user->username = 'NewUser';  // assign "NewUser" to $user->username, prepare to save.
  $user->password = Hash::make('password_string');  // encrypt password, then pass it to $user->password, prepare to save.
  $user->save();  // save to database
  
  // second method about insert: this method because of security, it will generate error, you have to open app/models/User.php to add: protected $fillable = ["username", "password"];
  User::create([
    "username" => "carlos7",
    "password" => Hash::make("password_string")
  ]);

  return User::all();
});

Notice:updated_at and created_at, these two columns that Laravel will save them to timestamp by default. If you want to cancel, Open app/models/User.php

class User extends Eloquent implements UserInterface, RemindableInterface {
  // assign $timestamps to false
  public $timestamps = false;

Open app/routes.php: update data

// update data from specific id
Route::get('/', function()
{
  $user = User::find(2);
  $user->username = "update_name";
  $user->save();
});

Open app/routes.php:delete data

// delete specific record
Route::get('/', function()
{
  $user = User::find(2);
  $user->delete();
});