Building Dynamic Web Apps with Laravel | CS50 – Harvard College

Laravel is a powerful and elegant PHP web development framework for building interactive websites, such as CS50’s own site! Learn about how to leverage this cutting edge development tool to create everything from simple APIs to sophisticated modern web applications.

Laravel Eloquent ORM and Query Builder

Using Query Builder is easy way and complicated writing.

I had thought that I couldn’t use Eloquent ORM for complicated query but I found the solution.

A code snippet:

$bid_products = Bid::with(array(‘Product’ =>
            function($query){$query->orderBy(‘title’, ‘ASC’);}))
->where(‘admin_id’, ‘=’, $admin_id)->orderBy(‘bids.created_at’, ‘desc’)
->get();

Links:

http://stackoverflow.com/questions/15533659/sorting-data-with-eloquent

10 quick tips to get better at Laravel http://tech.knolskape.com/10-quick-tips-to-get-better-at-laravel/

Laravel Eloquent vs Fluent query builder http://blog.sriraman.in/laravel-eloquent-vs-fluent-query-builder/

Laravel’s Eloquent ORM versus Query Builder http://www.rogerpence.com/2013/07/29/laravels-eloquent-orm-versus-query-builder/

Univerzális Entitás Kezelés: http://files.meetup.com/15327232/Perger_Peter_-_Univerzalis_entitas_kezeles-L4R4VELBP.pdf

Laravel : Query Scopes, Accessors and Mutators

Query Scopes

Query scopes are easier way to re-use logic in your model than writing where clauses all over again and again for the same logic.

We will use a user model which has a property “active” a boolean value.

User
    -id [1]
    -username [john]
    -firstname [john]
    -lastname  [doe]
    -password [hashedpassword]
    -active [1]

We need to query for all users who are active (active = 1). To use scopes we prefix the function name with “scope”. Below is example for querying for active users.

class User extends Eloquent {

    public function scopeActive($query){
        return $query->where('active' , '=', 1);
    }
}

You can retrieve the active users as below. Also, you can use multiple scopes on a model.

$users = User::active()->get();

/*multiple scopes - active users - male users 
assuming that scopeMale function is added to the model */

$male_users = User::active()->male()->get();

Mutators

Mutuators can be utitlised to manipulate the data before saving into the database. One of the place where I use mutators are for passwords. Lets consider the same user model, when creating/updating or seeding a user you have to hash the password. So you would do something like

$user->password = Hash::make('password');

Since password has to be hashed everytime before saving into the database it would be nice if we could do it one place and just before saving. We can achieve this using mutators.

class User extends Eloquent {

    public function setPasswordAttribute($value) {
        $this->attributes['password'] = Hash::make($value);        
    }
}

Accessors

Accessors are opposite to mutators, it lets us to manipuldate the data just before returning the data on query. For example, we want to capitalize the first letter in the first name everytime. I am just going to write the function since it is understood that it is inside the user model.

public function getFirstNameAttribute($value){
    return ucfirst($value);
}

We can also use this to get custom property on the model. For example, a property on the user model “full_name” which would concatenate first name and last name. Notice that this would return first name with first letter capitalized since we did that in the previous accessor method.

public function getFullNameAttribute($value){
    return $this->first_name . " " . $this->last_name;
}

$user = User::find(1);
$user->full_name;
//John doe

 

source: http://www.ashwinsureshkumar.com/laravel-query-scopes-accessors-and-mutators/

Sending mail with Laravel

,

Postfix

If you use Postfix on your server you have to change  config > mail.php

// ‘driver’ => ‘smtp’,
‘driver’ => ‘sendmail’,

Install Postfix: http://www.codechewing.com/library/set-up-postfix-on-ubuntu-server/

https://help.ubuntu.com/14.04/serverguide/postfix.html

useful:

https://www.linode.com/docs/email/postfix/email-with-postfix-dovecot-and-mysql

Configure Postfix to Use Gmail SMTP on Ubuntu https://rtcamp.com/tutorials/linux/ubuntu-postfix-gmail-smtp/

 

Smtp

http://www.havetheknowhow.com/Configure-the-server/Install-ssmtp.html

 

[php snippet=1]