I
I
Ivan2020-09-09 11:14:45
Laravel
Ivan, 2020-09-09 11:14:45

How to make a mutator so that if there is a link in the text, the model will give it as an html link?

The idea is this. There is a text field in the table, and if there is a link among the text, it should display this link as in the html link tags. For example, if this field contains

Some text bla bla http://mysite/bla/bla дальше рыба текст
so that the model gives it as
Some text bla bla  <a href="http://mysite/bla/bla">http://mysite/bla/bla</a>дальше рыба текст
. How to do it in a mutator? Or not in the mutator.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin B., 2020-09-09
@Djonson86

public function getTextFormattedAttribute()
{
  $text  = $this->attributes['text'];

  // Тут делаете что хотите с вашим тестом. Находите ссылки и т.д., удаляете или подсвечиваете их

  return $text;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question