Laravel Dusk Dashboard

Laravel dusk is very powerful for browser test and easy to use, but we want to see “step by step” running test cases, beyondcode/dusk-dashboard provide it to make simple and easy.

Read more
Fetching tags, mention and links to render in html

For example, we have a content is:

Lorem ipsum dolor sit amet #Yish, consectetur adipiscing elit. Nunc pharetra tortor a dui pellentesque, eget mattis turpis laoreet. Maecenas consequat maximus aliquam. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean sodales diam et turpis gravida facilisis. Duis sed sem quam. Pellentesque ornare, turpis vitae consectetur sodales, orci sapien venenatis leo, non porttitor mauris dolor id sapien. https://google.com Aliquam dignissim lectus nulla, ut ornare nisl volutpat eget. Pellentesque augue nibh, bibendum non condimentum in, porta id tortor. Curabitur mattis justo vitae nulla facilisis #gravida. Quisque feugiat eget arcu vitae commodo. Interdum et malesuada fames ac ante ipsum primis in faucibus.

https://laravel.com

#hello

#world

#this-is

Read more
Fetching tags from content with PHP

For example, the content is:

Hello, world.
This is the one of tech place: #laravel.

#tech #coding #ceil

I want to fetch start with ‘#’ as tags and insert to tags table, so how to do this?

class Tag
{
public static function fetchesResolved($value)
{
$matches = [];
if (! empty($value)) {
preg_match_all("/(#\w+)/u", $value, $matches);
}
if (empty($matches)) {
return;
}
$hashtagsArray = array_count_values($matches[0]);
$hashtags = array_keys($hashtagsArray);
return preg_replace('/#([\w-]+)/u', '$1', $hashtags);
}
}

Finally, you can call Tag::fetchesResolved($content) and get the array of tags like this:

[
'laravel',
'tech',
'coding',
'ceil',
]


CARTALYST/TAGS set slug generator for others language

CARTALYST/TAGS is powerful taggable package for laravel, it’s so nice and easy to use, but having some problem with languages except english.

The problem is, by default, slug was be set Illuminate\Support\Str::slug and we don’t pass any properties ref, by the way, Illuminate\Support\Str::slug supported multiple languages ref, but need to pass option properties, if not, . For now, the package provide setSlugGenerator method, you can do belong and set customize slug parser.

public function slugify()
{
$this->setSlugGenerator(function($name) {
return base64_encode($name); // set what you want.
});

return $this;
}

For better, you can extract it and compose Cartalyst\Tags\TaggableTrait, like this:

namespace App\Foundation;
use Cartalyst\Tags\TaggableTrait;
trait Slugify
{
use TaggableTrait;
public function slugify()
{
$this->setSlugGenerator(function($name) {
return base64_encode($name);
});
return $this;
}
}


Extracting scope status with Eloquent

Uniform the status scoping and do better setting.

In project, your posts, articles and others, the status are uniform for initial design, but you distributed on each model like this:

// Post.php
public function scopeStatus($query, $status)
{
return $query->where('status', $status);
}
public function scopePrivate($query)
{
return $query->status(static::$private);
}
public function scopePublic($query)
{
return $query->status(static::$public);
}
public function scopeProtected($query)
{
return $query->status(static::$protected);
}

and each model owns it, it’s wired, so how’s best practice?
you can put in App\Eloquent\Concerns\HasStatus

namespace App\Eloquent\Concerns;
trait HasStatus
{
public static $private = 0;
public static $public = 1;
public static $protected = 2;
public function scopeStatus($query, $status)
{
return $query->where('status', $status);
}
public function scopePrivate($query)
{
return $query->status(static::$private);
}
public function scopePublic($query)
{
return $query->status(static::$public);
}
public function scopeProtected($query)
{
return $query->status(static::$protected);
}
}

Next, if you need to use it, you can use the trait to specific eloquent:
Notice: if you need to set readable and extendable, you can do contract.

//App\Eloquent\Contracts\Statusable.php
interface Statusable
{
public function scopeStatus($query, $status);
}

Finally, implement and use trait.

class Post extends Model implements Statusable
{
use HasStatus,


Laravel notification mocking

For example, you may create a notification for project like belong:

// notification
class PostCreated extends Notification
{
....

public function via($notifiable)
{
return ['database']; // for this example, I used database notifiable.
}

public function toArray($notifiable)
{
return [
'post_id' => $this->post->id, //constructor object.
];
}
}

Next, I added the notification to service flow.

auth()->user()->notify(new PostCreated($post));

But here is a problem, how do I test it?

// PostTest.php
public function it_should_create_by_auth()
{
// enabled fake
Notification::fake();

$this->be($this->user);

// checking the flow is correct...

Notification::assertSentTo(
auth()->user(),
PostCreated::class,
function ($notification, $channels) use ($post) {
return $notification->post->id === $post->id;
}
);
}

For now, you have tested notification and work now, it’s useful.

see documentation.


Retrieving the dictionary keywords with console integration

Sometime I need to find some keyword via terminal, usual I use my editor and find it in general, but no.
So I need to retrieve via terminal, do the integration and echo it, horizontal line.

for i in $(find . -type f) ; 
do grep 'keywords' $i ; //typing what you need.
echo '== '$i' ==' ;
done

Diving Laravel

The collaborator of laravel @themsaid created blog Diving Laravel, it’s took serveral behind the scenes of laravel, you can recognize deeper about laravel how does it works.


Cool backgrounds - nice generator

If you’re looking for nice cover or hero cover, and no image, just purely colour, Cool backgrounds is a the best of selecting, it provide serveral template and plates, and you can customize colour.


Flat icon - a beautiful icons platform

A rich of icons platform flaticon, it can find nice icons for project, also you can pay to use premium package.