mardi 26 novembre 2019

How to use custom function in Model to format Data

What I Want to do

I want to convert URLs to tags like this:

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

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

I want to use a custom Function in Model to do it.

What I did

I added this to my 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 called it like this:

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

But this gives an error:

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

How can I make custom Function in Model and use it in my templates?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire