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 ?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2QTquWe
via IFTTT

Aucun commentaire:

Enregistrer un commentaire