mardi 26 novembre 2019

【Laravel】I wanna make custom function in Model and format Data

What I Want to do

I wanna convert URL to a tag like this

あいうえおかきくけこhttp://localhostさしすせそたちつてと

あいうえおかきくけこ<a href="http://localhost">http://localhost</a>さしすせそたちつてと

Then, I wanna use custom Function in Model to do it.

What I did

I made it in Model.

class hogehoge extend Model {

    public function convertUrlToLink($value)
    {
        // URLをaタグで囲む
        $pattern = '/((?:https?|ftp):\/\/[-_.!~*\'()a-zA-Z0-9;\/?:@&=+$,%#]+)/';
        $replace = '<a href="$1">$1</a>';
        $text = preg_replace($pattern, $replace, $value);
        return $text;
    }
}

and call it like this.

Hogehoge::first(1)->convertUrlToLink()

This result is .

Call to a member function convertUrlToLink() on string (View: /app/resources/views/club/show.blade.php)

How I make custom Function in Model and use it ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire