Skip to content

Commit

Permalink
Update laravel note
Browse files Browse the repository at this point in the history
Add php storm note
Update ubuntu note
  • Loading branch information
dev-graduan committed Feb 25, 2019
1 parent d9943f1 commit 8415ff9
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 26 deletions.
96 changes: 96 additions & 0 deletions Laravel/Query/order_by_relationship.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Order By Relationship

## Order by timestamp of relationship

```php

// raw method
// in this example we try sort posts using version
$jobs = DB::table('posts')
->join('versions', 'versions.post_id', '=', 'posts.id')
->select('posts.*', 'versions.post_id', 'versions.created_at')
->orderBy('versions.created_at', 'desc')
->take(5)
->get();

// or use eloquent
$posts = Post::join('posts', 'versions.post_id', '=', 'posts.id')
->addSelect('posts.*', 'versions.post_id', 'versions.created_at')
->orderBy('versions.created_at', 'desc')
->paginate(20);

```

## Order by sum of relationship

Assume we have Model Comment and CommentVote

Setup Comment Model first

```php

// App/Comment.php

/**
* Orders a comment by the sum of the votes that it has in the comment_votes table.
* Comment votes can have a position of 1, 0 and -1 so the sum of all these entries works as a rank.
*
* @param Builder $query Query builder instance
* @param string $order asc/desc
*
* @return Builder instance
*/
public function scopeOrderByCommentRank($query, $order = 'desc')
{
return $query->leftJoin('comment_votes', 'comment_votes.comment_id', '=', 'comments.id')
->groupBy('comments.id')
->addSelect(['*', \DB::raw('sum(position) as commentRank')])
->orderBy('commentRank', $order);
}


/**
* An alternative relationship with comment votes for DB count queries
*/
public function commentRankRelation()
{
return $this->hasOne('App\CommentVote')
->selectRaw('comment_id, sum(position) as rank')
->groupBy('comment_id');
}


/**
* @return int the mutator to get the sum of all the comment votes
*/
public function getCommentRankAttribute()
{
// if relation is not loaded already, let's do it first
if (!array_key_exists('commentRankRelation', $this->relations)) {
$this->load('commentRankRelation');
}

$related = $this->getRelation('commentRankRelation');

// then return the count directly
return ($related) ? (int) $related->rank : 0;
}

```

Sorted comment by sum of comment votes

```php

$comments = Comment::orderByCommentRank('desc')->get();

// Print all the comments in Descending order
foreach ($comments as $comment) {
echo $comment->text." ($comment->commentRank)";
}

```

## Reference

[Order By Related Field Sum](https://laracasts.com/discuss/channels/eloquent/order-by-related-field-sum)
23 changes: 1 addition & 22 deletions Laravel/cheat_sheet.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,11 @@ find empty column

`$query->where('profile', '<>', "");`

### Order By another table

```php

// raw method
// in this example we try sort posts using version
$jobs = DB::table('posts')
->join('versions', 'versions.post_id', '=', 'posts.id')
->select('posts.*', 'versions.post_id', 'versions.created_at')
->orderBy('versions.created_at', 'desc')
->take(5)
->get();

// or use eloquent
$posts = Post::join('posts', 'versions.post_id', '=', 'posts.id')
->addSelect('posts.*', 'versions.post_id', 'versions.created_at')
->orderBy('versions.created_at', 'desc')
->paginate(20);

```

## Listener

Listen when user login and logout

[https://infylife.wordpress.com/2016/08/10/storing-logging-login-activity-in-laravel-application/](https://infylife.wordpress.com/2016/08/10/storing-logging-login-activity-in-laravel-application/)
[Storing / Logging Login Activity in Laravel Application](https://infylife.wordpress.com/2016/08/10/storing-logging-login-activity-in-laravel-application/)

## Request

Expand Down
16 changes: 16 additions & 0 deletions PhpStorm/laravel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Laravel

## Magic Method

```php

**
* @property string type
*/
class User {

}

```

to remove magic method warning add doc property inside class you're having issue with.
9 changes: 5 additions & 4 deletions Ubuntu/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ Clean and remove old and unused package

## Reference

* [https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-14-04](https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-14-04)
* [https://www.digitalocean.com/community/tutorials/how-to-create-a-sudo-user-on-ubuntu-quickstart](https://www.digitalocean.com/community/tutorials/how-to-create-a-sudo-user-on-ubuntu-quickstart)
* [https://www.digitalocean.com/community/tutorials/how-to-setup-a-firewall-with-ufw-on-an-ubuntu-and-debian-cloud-server](https://www.digitalocean.com/community/tutorials/how-to-setup-a-firewall-with-ufw-on-an-ubuntu-and-debian-cloud-server)
* [https://askubuntu.com/questions/410244/a-command-to-list-all-users-and-how-to-add-delete-modify-users](https://askubuntu.com/questions/410244/a-command-to-list-all-users-and-how-to-add-delete-modify-users)
* [ Initial Server Setup with Ubuntu 14.04
](https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-14-04)
* [How To Create a Sudo User on Ubuntu [Quickstart]](https://www.digitalocean.com/community/tutorials/how-to-create-a-sudo-user-on-ubuntu-quickstart)
* [How To Setup a Firewall with UFW on an Ubuntu and Debian Cloud Server](https://www.digitalocean.com/community/tutorials/how-to-setup-a-firewall-with-ufw-on-an-ubuntu-and-debian-cloud-server)
* [A command to list all users? And how to add, delete, modify users?](https://askubuntu.com/questions/410244/a-command-to-list-all-users-and-how-to-add-delete-modify-users)

0 comments on commit 8415ff9

Please sign in to comment.