1. LARAVEL FRAMEWORK
DB OPERATIONS
ELOQUENT MODEL
Sayed Ahmed
Computer Engineering, BUET, Bangladesh (Graduated on
2001)
MSc., Computer Science, Canada
http://www.justetc.net
2. INSTALLING
You can install by using Composer
http://getcomposer.org/doc/00-intro.md
3. DB STUFF
How and where to configure
app/config/database.php
Supported Databases
MySQL, Postgres, SQLite, and SQL Server
Running A Select Query
$results = DB::select('select * from users where id =
?', array(1));
Running An Insert Statement
DB::insert('insert into users (id, name) values (?, ?)',
array(1, 'Dayle'));
4. DB STUFF
Running An Update Statement
DB::update('update users set votes = 100 where
name = ?', array('John'));
Running A General Statement
DB::statement('drop table users');
Listening For Query Events
DB::listen(function($sql, $bindings, $time) { // });
20. ELOQUENT ORM
The Eloquent ORM included with Laravel
provides a beautiful, simple ActiveRecord
implementation for working with your
database. Each database table has a
corresponding "Model" which is used to
interact with that table.
21. ELOQUENT MODEL
Defining An Eloquent Model
class User extends Eloquent {}
By default it will point to the table users
Lowercase and plural form
class User extends Eloquent {
protected $table = 'my_users';
}
Note: you can use properties such as primaryKey
and connection
All table should have createdat and updatedat
columns else use $timestamps = false in the model
22. ELOQUENT MODEL AND DB OPERATIONS
Retrieve All
$users = User::all();
Retrieve by Primary key
$user = User::find(1);
var_dump($user->name);
Retrieve or Throw exception
$model = User::findOrFail(1); $model =
User::where('votes', '>', 100)->firstOrFail();
25. FILLABLE
The fillable property specifies which attributes
should be mass-assignable. This can be set at
the class or instance level.
class User extends Eloquent {
protected $fillable = array('first_name', 'last_name',
'email'); }
Guarded (not mass assignable)
class User extends Eloquent {
protected $guarded = array('id', 'password');
}
26. BLOCKING AND INSERTION
Block all attributes from mass assignment
protected $guarded = array('*');
Insert a new row
$user = new User;
$user->name = 'John';
$user->save();
27. UPDATE A ROW
$user = User::create(array('name' =>
'John'));
Updating a retrieved model
$user = User::find(1); $user->email =
'john@foo.com'; $user->save();
28. SOME SAVE, UPDATE, DELETE STUFF
Saving a model and relationship
$user->push();
Updates as queries
$affectedRows = User::where('votes', '>', 100)
->update(array('status' => 2));
Delete by key
User::destroy(1);
User::destroy(array(1, 2, 3));
User::destroy(1, 2, 3);
30. Restore softdeleted into active
$user->restore();
On query
User::withTrashed()->where('account_id', 1)-
>restore();
On relationships
$user->posts()->restore();
36. DEFINE ONE TO ONE
class User extends Eloquent {
public function phone() {
return $this->hasOne('Phone');
}
}
Use
$phone = User::find(1)->phone;
37. INVERSE OF A RELATION
class Phone extends Eloquent {
public function user() {
return $this->belongsTo('User');
}
}
class Phone extends Eloquent {
public function user() {
return $this->belongsTo('User', 'custom_key');
}
}`
38. class Phone extends Eloquent {
public function user() {
return $this->belongsTo('User', 'custom_key');
}
}
39. ONE TO MANY
class Post extends Eloquent {
public function comments() {
return $this->hasMany('Comment');
}
}
return $this->hasMany('Comment',
'custom_key');
$comments = Post::find(1)->comments;
$comments = Post::find(1)->comments()
->where('title', '=', 'foo')->first();
40. INVERSE OF A RELATION
class Comment extends Eloquent {
public function post() {
return $this->belongsTo('Post');
}
}
41. MANY TO MANY
class User extends Eloquent {
public function roles() {
return $this->belongsToMany('Role');
}
}
return $this->belongsToMany('Role',
'user_roles');
return $this->belongsToMany('Role',
'user_roles', 'user_id', 'foo_id');
$roles = User::find(1)->roles;
42. INVERSE OF A RELATION
class Role extends Eloquent {
public function users() {
return $this->belongsToMany('User');
}
}
44. class Photo extends Eloquent { public
function imageable() { return $this-
>morphTo(); } } class Staff extends Eloquent
{ public function photos() { return $this-
>morphMany('Photo', 'imageable'); } } class
Order extends Eloquent { public function
photos() { return $this->morphMany('Photo',
'imageable'); } }
47. DYNAMIC PROPERTIES
Eloquent allows you to access your relations
via dynamic properties. Eloquent will
automatically load the relationship for you,
and is even smart enough to know whether
to call the get (for one-to-many relationships)
or first (for one-to-one relationships) method
48. EAGER LOADING
Will hit db 26 times for 25 books
foreach (Book::all() as $book) { echo $book-
>author->name; }
Is reduced to two queries
foreach (Book::with('author')->get() as $book) {
echo $book->author->name; }
select * from books select * from authors where
id in (1, 2, 3, 4, 5, ...)