Skip to content

Commit ef86a82

Browse files
committed
Init blud
0 parents  commit ef86a82

26 files changed

+488
-0
lines changed

.vuepress/components/ItemList.vue

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<template>
2+
<div>
3+
<router-link
4+
v-for="item in items"
5+
:key="item.key"
6+
:to="item.path"
7+
>
8+
<h2>{{ item.title }}</h2>
9+
</router-link>
10+
</div>
11+
</template>
12+
13+
<script>
14+
export default {
15+
computed: {
16+
items() {
17+
const folder = this.$page.path
18+
19+
return this.$site.pages
20+
.filter(page => page.path.match(new RegExp(`(${folder})(?=.*html)`)))
21+
.sort((a, b) => new Date(b.frontmatter.date) - new Date(a.frontmatter.date))
22+
}
23+
}
24+
}
25+
</script>

.vuepress/components/RandomItems.vue

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<template>
2+
<div>
3+
<router-link
4+
v-for="item in items"
5+
:key="item.key"
6+
:to="item.path"
7+
>
8+
<h2>{{ item.title}}</h2>
9+
</router-link>
10+
</div>
11+
</template>
12+
13+
<script>
14+
export default {
15+
props: [
16+
'count',
17+
],
18+
19+
computed: {
20+
21+
numberOfItems() {
22+
const numberOfItems = this.count || 5
23+
const numberOfPages = this.$site.pages.filter(page => page.path.match(new RegExp(`(?=.*html)`))).length
24+
25+
return numberOfPages < numberOfItems ? numberOfPages : numberOfItems
26+
},
27+
28+
items() {
29+
return this.getRandom(this.$site.pages
30+
.filter(page => page.path.match(new RegExp(`(?=.*html)`))), this.numberOfItems)
31+
}
32+
33+
},
34+
35+
methods: {
36+
/**
37+
* Credit: https://stackoverflow.com/a/19270021
38+
*/
39+
getRandom(arr, n) {
40+
let result = new Array(n),
41+
len = arr.length,
42+
taken = new Array(len);
43+
if (n > len)
44+
throw new RangeError("getRandom: more elements taken than available");
45+
while (n--) {
46+
var x = Math.floor(Math.random() * len);
47+
result[n] = arr[x in taken ? taken[x] : x];
48+
taken[x] = --len in taken ? taken[len] : len;
49+
}
50+
return result;
51+
}
52+
},
53+
}
54+
</script>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<template>
2+
<div>
3+
<slot></slot>
4+
</div>
5+
</template>

.vuepress/config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module.exports = {
2+
title: 'Planet Laravel Code Hub',
3+
description: 'Useful, interesting and fun code snippets for the Laravel PHP Framework',
4+
themeConfig: {
5+
nav: [
6+
{ text: 'Planet Laravel', link: 'https://www.planetlaravel.com' },
7+
],
8+
sidebar: [
9+
'/random/',
10+
'/generic/',
11+
'/authentication/',
12+
'/database/',
13+
'/interfaces/',
14+
'/models/',
15+
'/tinker/',
16+
'/testing/',
17+
],
18+
lastUpdated: 'Last Updated',
19+
serviceWorker: {
20+
updatePopup: true
21+
}
22+
}
23+
}

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
home: true
3+
actionText: Get Started →
4+
actionLink: /random/
5+
features:
6+
- title: Open-source
7+
details: Got some code to share? Just open a Pull Request on this repository. Anyone is welcome to contribute.
8+
- title: Searchable
9+
details: Start by entering a command or area of interest to see what hidden-gems you can uncover.
10+
- title: Give yourself some credit
11+
details: Wether you are sharing your own or somebody else's code, be sure to include everyone in the credits.
12+
footer: MIT Licensed | Copyright © 2018-present Planet Laravel
13+
---

authentication/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Authentication and Authorization
2+
3+
<ItemList />

authentication/ajax-login.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Using Ajax for user authentication
2+
3+
If you are building an Ajax based login form you can use the `authenticated` method to send an ajax response instead of the default Laravel redirect.
4+
5+
```php
6+
// LoginController.php
7+
8+
...
9+
/**
10+
* The user has been authenticated.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @param mixed $user
14+
* @return mixed
15+
*/
16+
protected function authenticated(Request $request, $user)
17+
{
18+
// Send required response here
19+
}
20+
...
21+
```
22+
23+
|Added by|Found via|
24+
|--------|--------|
25+
|[Marco Mark](https://twitter.com/m2de_io)|[Medium](https://codeburst.io/using-ajax-user-login-in-laravel-5-3-5-4-5-5-5-6-d4e30b47985f)|

database/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Eloquent Databases
2+
3+
<ItemList />

database/array-where-conditions.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
snippet: |
3+
User::where([
4+
'confirmed' => true,
5+
'active' => true,
6+
]);
7+
---
8+
9+
# You can pass an array of constraints to the "where" method
10+
11+
Did you know that you can pass an array of constraints to the "where" method on Laravel's query builder?
12+
13+
```php
14+
User::where([
15+
'confirmed' => true,
16+
'active' => true,
17+
]);
18+
```
19+
20+
You can even include operators ...
21+
22+
```php
23+
User::where([
24+
['confirmed_at', '!=', null],
25+
'active' => true,
26+
]);
27+
```
28+
29+
|Added by|Found via|
30+
|--------|--------|
31+
|[Marco Mark](https://twitter.com/m2de_io)|[Joseph Silber](https://mobile.twitter.com/joseph_silber/status/1017394114185388032)|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Subqueries made easy with `selectSub`
2+
3+
Subqueries allow us to select extra columns (attributes) right in our primary database query.
4+
5+
```php
6+
$lastLogin = Login::select('created_at')
7+
->whereColumn('user_id', 'users.id')
8+
->latest()
9+
->limit(1)
10+
->getQuery();
11+
12+
$users = User::select('users.*')
13+
->selectSub($lastLogin, 'last_login_at')
14+
->get();
15+
```
16+
17+
```php
18+
@foreach ($users as $user)
19+
<tr>
20+
<td>{{ $user->name }}</td>
21+
<td>{{ $user->email }}</td>
22+
<td>
23+
@if ($user->last_login_at)
24+
{{ $user->last_login_at->format('M j, Y \a\t g:i a') }}
25+
@else
26+
Never
27+
@endif
28+
</td>
29+
</tr>
30+
@endforeach
31+
```
32+
33+
|Added by|Found via|
34+
|--------|--------|
35+
|[Marco Mark](https://twitter.com/m2de_io)|[Jonathan Reinink](https://reinink.ca/articles/dynamic-relationships-in-laravel-using-subqueries)|

generic/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Generic tips and tricks
2+
3+
<ItemList />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Consider static factory methods instead of constructors
2+
3+
Unlike constructors, static factory methods can have names making the code easier to read - the DateTime class in PHP is a good example.
4+
5+
This is much more expressive ...
6+
7+
```php
8+
$format = 'Y-m-d H:i:s';
9+
10+
$date = DateTime::createFromFormat($format, '2018-07-28 13:37:00');
11+
```
12+
13+
than this ...
14+
15+
```php
16+
$format = 'Y-m-d H:i:s';
17+
18+
$date = new DateTime($format, '2018-07-28 13:37:00');
19+
```
20+
21+
|Added by|Found via|
22+
|--------|--------|
23+
|[Marco Mark](https://twitter.com/m2de_io)|[Nuno Maduro](https://mobile.twitter.com/enunomaduro/status/1077879686695448576)|

generic/throw_if_throw_unless.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Get rid of unnecessary conditionals with `throw_if` and `throw_unless`
2+
3+
Make use of more expressive code using the "throw_if" and "throw_unless" helper functions in Laravel PHP
4+
5+
```php
6+
// throw an exception if the condition is true
7+
throw_if($condition = true, new SomeException);
8+
9+
// throw an exception unless the condition is true
10+
throw_unless($condition = true, new SomeException);
11+
```
12+
13+
|Added by|Found via|
14+
|--------|--------|
15+
|[Marco Mark](https://twitter.com/m2de_io)|[Marcel Pociot](https://mobile.twitter.com/marcelpociot/status/1075744056679038976)|

interfaces/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Interfaces and its use cases
2+
3+
<ItemList />
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Using the responsible interface to build dedicated view objects
2+
3+
Dedicated view objects are a great use case for Laravel 5.5's new `Responsable` interface.
4+
5+
Let's take this pseudo controller which actually returns a rendered view with specific parameters.
6+
7+
```php
8+
class ProductSalesController
9+
{
10+
public function show(Product $product)
11+
{
12+
return new SalesReport($product);
13+
}
14+
}
15+
```
16+
17+
The implementation is a simple class which implements the Responsible interface.
18+
19+
```php
20+
class SalesReport implements Responsible
21+
{
22+
...
23+
24+
public function totalRevenue()
25+
{
26+
return $this->product->sales->sum('amount');
27+
}
28+
29+
public function toResponse()
30+
{
31+
return view('product-sales.show')->with('total_revenue', $this->totalRevenue());
32+
}
33+
}
34+
```
35+
36+
|Added by|Found via|
37+
|--------|--------|
38+
|[Marco Mark](https://twitter.com/m2de_io)|[Adam Wathan](https://mobile.twitter.com/adamwathan/status/897836363885797376)|

models/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Things you didn't know you can do with Models
2+
3+
<ItemList />

models/boot-trait.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Hook into model events from a trait
2+
3+
You can hook into a model's boot method from your trait by creating a `boot{TraitName}` method. This is somewhat hidden in the [official documentation](https://laravel.com/docs/5.0/eloquent#global-scopes).
4+
5+
Here's a basic example of how you could use this functionality.
6+
7+
```php
8+
trait NotifyWhenCreated
9+
{
10+
public static function bootNotifyWhenCreated()
11+
{
12+
static::created(function ($item) {
13+
Notification::send($recipients, new ItemCreated($item));
14+
});
15+
}
16+
}
17+
```
18+
19+
Add the trait to your model.
20+
21+
```php
22+
class User extends Eloquent
23+
{
24+
use NotifyWhenCreated;
25+
}
26+
```
27+
28+
Use your new functionality.
29+
30+
```php
31+
User::create([...]); // Your notification will be sent
32+
```
33+
34+
|Added by|Found via|
35+
|--------|--------|
36+
|[Marco Mark](https://twitter.com/m2de_io)|[Laravel Docs](https://laravel.com/docs/5.0/eloquent#global-scopes)|

models/initialize-trait.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Hook into the __construct() method of a model from a trait
2+
3+
Since 5.7, Laravel will automatically look for and execute any `initialize{TraitName}` methods when a model instance is created, givin you full access to the object (e.g. `$this`).
4+
5+
Create a trait with the initialize function and perform whatever logic you need.
6+
7+
```php
8+
trait HasMembership
9+
{
10+
// Use the initialize{TraitName} convention
11+
public function initializeHasMembership
12+
{
13+
$this->casts = array_merge($this->casts, [
14+
'membership_started_at' => 'datetime',
15+
]);
16+
}
17+
}
18+
```
19+
20+
Add the trait to your model.
21+
22+
```php
23+
class User extends Eloquent
24+
{
25+
use HasMembership;
26+
27+
protected $casts = [
28+
'age' => 'int',
29+
];
30+
}
31+
```
32+
33+
Use your new functionality.
34+
35+
```php
36+
User::first()->membership_started_at; // Carbon Instance
37+
```
38+
39+
|Added by|Found via|
40+
|--------|--------|
41+
|[Marco Mark](https://twitter.com/m2de_io)|[Tim MacDonald](https://twitter.com/timacdonald87/status/1076117855911866369)|

0 commit comments

Comments
 (0)