Skip to content

Instantly share code, notes, and snippets.

@karkoonzaid
Forked from msurguy/eloquent.md
Last active August 29, 2015 14:16
Show Gist options
  • Save karkoonzaid/a902bf6b3130e5d15d99 to your computer and use it in GitHub Desktop.
Save karkoonzaid/a902bf6b3130e5d15d99 to your computer and use it in GitHub Desktop.

Revisions

  1. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 13 additions and 1 deletion.
    14 changes: 13 additions & 1 deletion eloquent.md
    Original file line number Diff line number Diff line change
    @@ -86,5 +86,17 @@ Getting a sum of the query result:

    ``` Shop::where('orders_cache','>','100')->decrement('orders_cache'); ```


    ## Todo :
    - or, between, whereNull, whereIn
    - inserts
    - updates
    - deletes
    - soft deletes
    - unions
    - raw expressions
    - grouping
    - joins
    - Fillable
    - Guarded
    -

  2. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions eloquent.md
    Original file line number Diff line number Diff line change
    @@ -81,9 +81,10 @@ Getting a sum of the query result:

    # Incrementing and decrementing values of a column:

    ```Shop::where('orders_cache','>','100')->increment('orders_cache');```
    ``` Shop::where('orders_cache','>','100')->increment('orders_cache'); ```

    ```Shop::where('orders_cache','>','100')->decrement('orders_cache');```

    ``` Shop::where('orders_cache','>','100')->decrement('orders_cache'); ```



  3. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions eloquent.md
    Original file line number Diff line number Diff line change
    @@ -82,6 +82,7 @@ Getting a sum of the query result:
    # Incrementing and decrementing values of a column:

    ```Shop::where('orders_cache','>','100')->increment('orders_cache');```

    ```Shop::where('orders_cache','>','100')->decrement('orders_cache');```


  4. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions eloquent.md
    Original file line number Diff line number Diff line change
    @@ -79,4 +79,10 @@ Getting a sum of the query result:

    ```$totalWeight = Product::where('name','Coffee')->sum('weight');```

    # Incrementing and decrementing values of a column:

    ```Shop::where('orders_cache','>','100')->increment('orders_cache');```
    ```Shop::where('orders_cache','>','100')->decrement('orders_cache');```



  5. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion eloquent.md
    Original file line number Diff line number Diff line change
    @@ -77,6 +77,6 @@ Getting an average value of the query result:

    Getting a sum of the query result:

    ```$averageCoffeePrice = Product::where('name','Coffee')->sum('price');```
    ```$totalWeight = Product::where('name','Coffee')->sum('weight');```


  6. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 27 additions and 6 deletions.
    33 changes: 27 additions & 6 deletions eloquent.md
    Original file line number Diff line number Diff line change
    @@ -30,24 +30,24 @@ Retrieving single record that matches a certain column :
    ```$shop = Shop::where('name', 'Starbucks')->first();```

    Retrieving specific columns of the result (by default get() returns all columns):
    ```$shop = Shop::where('name', 'Starbucks')->get('name','address','url');```
    ```$shops = Shop::where('name', 'Starbucks')->get('name','address','url');```

    Retrieving singe column of single row :
    ```$name = Shop::where('name', 'Starbucks')->pluck('address');```
    Retrieving single column of result :
    ```$shopAddresses = Shop::where('name', 'Starbucks')->pluck('address');```

    Retrieving records matching a criteria:

    ```$shops = Shop::where('orders_cache','>','100')->get();```
    ```$profitableShops = Shop::where('orders_cache','>','100')->get();```
    where() takes 3 parameters, name of the column, operator and value to be compared against. The operator can be one of the following:
    ```'=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', 'between', 'ilike'```

    Retrieve only a specified number of records matching a criteria:

    ```$shops = Shop::where('state', 'CA')->take(10)->get();```
    ```$californianShops = Shop::where('state', 'CA')->take(10)->get();```

    Skip a specified number of records:

    ```$shops = Shops::where('name', 'Starbucks')->skip(10)->get();```
    ```$someShops = Shops::where('name', 'Starbucks')->skip(10)->get();```

    Combining "skip" and "take" (useful for making custom paginators):

    @@ -57,5 +57,26 @@ Using forPage() to accomplish the above statement:

    ```$shops = Shops::where('name', 'Starbucks')->forPage(2,25)->get();```

    # Aggregates

    Counting the number of rows of the query results:

    ```$countStarbucks = Shop::where('name','Starbucks')->count();```

    Getting a maximum value of the query result:

    ```$maxCoffeePrice = Product::where('name','Coffee')->max('price');```

    Getting a minimum value of the query result:

    ```$minCoffeePrice = Product::where('name','Coffee')->min('price');```

    Getting an average value of the query result:

    ```$averageCoffeePrice = Product::where('name','Coffee')->avg('price');```

    Getting a sum of the query result:

    ```$averageCoffeePrice = Product::where('name','Coffee')->sum('price');```


  7. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 48 additions and 1 deletion.
    49 changes: 48 additions & 1 deletion eloquent.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Conventions:

    Defining Eloquent model (will assume that DB table named is set as plural of class name and primary key named "id"):

    ```class Shop extends Eloquent {}```
    @@ -11,4 +13,49 @@ Using custom primary key (instead of "id"):
    ```protected $primaryKey = 'my_key';```

    Disabling use of "created_at" and "updated_at" columns:
    ```public $timestamps = false;```

    ```public $timestamps = false;```

    # Retrieving records:

    Retrieving all records(rows) of a given model:

    ```$shops = Shop::all();```

    Find and get a record by primary key:

    ```$shop = Shop::find(1);```

    Retrieving single record that matches a certain column :
    ```$shop = Shop::where('name', 'Starbucks')->first();```

    Retrieving specific columns of the result (by default get() returns all columns):
    ```$shop = Shop::where('name', 'Starbucks')->get('name','address','url');```

    Retrieving singe column of single row :
    ```$name = Shop::where('name', 'Starbucks')->pluck('address');```

    Retrieving records matching a criteria:

    ```$shops = Shop::where('orders_cache','>','100')->get();```
    where() takes 3 parameters, name of the column, operator and value to be compared against. The operator can be one of the following:
    ```'=', '<', '>', '<=', '>=', '<>', '!=', 'like', 'not like', 'between', 'ilike'```

    Retrieve only a specified number of records matching a criteria:

    ```$shops = Shop::where('state', 'CA')->take(10)->get();```

    Skip a specified number of records:

    ```$shops = Shops::where('name', 'Starbucks')->skip(10)->get();```

    Combining "skip" and "take" (useful for making custom paginators):

    ```$shops = Shops::where('name', 'Starbucks')->skip(25)->take(25)->get();```

    Using forPage() to accomplish the above statement:

    ```$shops = Shops::where('name', 'Starbucks')->forPage(2,25)->get();```



  8. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions eloquent.md
    Original file line number Diff line number Diff line change
    @@ -2,13 +2,13 @@ Defining Eloquent model (will assume that DB table named is set as plural of cla

    ```class Shop extends Eloquent {}```

    Using custom table name
    Using custom table name

    ```protected $table = 'my_shops';```

    Using custom primary key (instead of "id"):
    Using custom primary key (instead of "id"):

    ```protected $primaryKey = 'my_key';```

    Disabling use of "created t" and "updated_at" columns:
    Disabling use of "created_at" and "updated_at" columns:
    ```public $timestamps = false;```
  9. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion eloquent.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,14 @@
    Defining Eloquent model (will assume that DB table named is set as plural of class name and primary key named "id"):

    ```class Shop extends Eloquent {}```

    Using custom table name

    ```protected $table = 'my_shops';```

    Using custom primary key (instead of "id"):

    ```protected $primaryKey = 'my_key';```

    Disabling use of "created t" and "updated_at" columns:
    public $timestamps = false;
    ```public $timestamps = false;```
  10. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions eloquent.md
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    Defining Eloquent model (will assume that DB table named is set as plural of class name and primary key named "id"):
    class Shop extends Eloquent {}
    ```class Shop extends Eloquent {}```

    Using custom table name
    protected $table = 'my_shops';
    ```protected $table = 'my_shops';```

    Using custom primary key (instead of "id"):
    protected $primaryKey = 'my_key';
    ```protected $primaryKey = 'my_key';```

    Disabling use of "created t" and "updated_at" columns:
    public $timestamps = false;
  11. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion eloquent.md
    Original file line number Diff line number Diff line change
    @@ -7,5 +7,5 @@ Using custom table name
    Using custom primary key (instead of "id"):
    protected $primaryKey = 'my_key';

    Disabling use of "created_at" and "updated_at" columns:
    Disabling use of "created t" and "updated_at" columns:
    public $timestamps = false;
  12. @msurguy msurguy revised this gist May 27, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions eloquent.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    Defining Eloquent model (will assume that DB table named is set as plural of class name and primary key named "id"):
    class Shop extends Eloquent {}
    class Shop extends Eloquent {}

    Using custom table name
    protected $table = 'my_shops';
    protected $table = 'my_shops';

    Using custom primary key (instead of "id"):
    protected $primaryKey = 'my_key';
  13. @msurguy msurguy created this gist May 27, 2013.
    11 changes: 11 additions & 0 deletions eloquent.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    Defining Eloquent model (will assume that DB table named is set as plural of class name and primary key named "id"):
    class Shop extends Eloquent {}

    Using custom table name
    protected $table = 'my_shops';

    Using custom primary key (instead of "id"):
    protected $primaryKey = 'my_key';

    Disabling use of "created_at" and "updated_at" columns:
    public $timestamps = false;