Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kikter's avatar

link preview not showing

in my livewire post show blade I did this if I create a post with a link the is not previewing in my show view page , I have tried to different URL still not working

public function mount (Post $post)
{
     $this->post = $post;
   
    preg_match_all('#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $post->body, $matches);
    $this->urlDetails = collect($matches[0])->map(function ($url) {
       $embed = new Embed();
       $info = $embed->get($url);
		return ['info' => $info, 'title' => $info->title];  
    })->all();
}

getting the URL like this in my show blade view

{{ $urlDetails['title'] ?? '' }}
0 likes
16 replies
Snapey's avatar

are we supposed to just guess what you are trying to do?

Snapey's avatar

try saving to array

are you iterating over the collection of urlDetails ?

kikter's avatar

@Snapey how do I save to Array and not iterating over the urlDetails

Snapey's avatar
Snapey
Best Answer
Level 122

@kikter toArray()

    $this->urlDetails = collect($matches[0])->map(function ($url) {
       $embed = new Embed();
       $info = $embed->get($url);
		return ['info' => $info, 'title' => $info->title];  
    })->toArray();

but you need to dump this so that you can see the structure. Since you ask for matches on the body, what you will get is an array response, even if there is only one.

You then map over this array, again, creating an array.

So without iterating, you at least need to;

{{ $urlDetails[0]['title'] ?? '' }}

But as I say. Inspect your data.

kikter's avatar

@Snapey i embed a twitter url in my post got this after dd ($this->urlDetails)

 array:1 [▼
  0 => array:2 [▼
    "info" => Embed\Adapters\Twitter\Extractor {#2002 ▼
      -request: GuzzleHttp\Psr7\Request {#2009 ▼
        -method: "GET"
        -requestTarget: null
        -uri: GuzzleHttp\Psr7\Uri {#2008 ▶}
        -headers: array:3 [▶]
        -headerNames: array:3 [▶]
        -protocol: "1.1"
        -stream: null
      }
      -response: GuzzleHttp\Psr7\Response {#2003 ▼
        -reasonPhrase: "OK"
        -statusCode: 200
        -headers: array:20 [▶]
        -headerNames: array:20 [▶]
        -protocol: "1.1"
        -stream: GuzzleHttp\Psr7\Stream {#2005 ▶}
      }
      -uri: GuzzleHttp\Psr7\Uri {#2004 ▼
        -scheme: "https"
        -userInfo: ""
        -host: "twitter.com"
        -port: null
        -path: "/izzlax/status/1513167772364251140"
        -query: "s=20&t=jOExnZVVf-t19fTmlH0FIA"
        -fragment: ""
        -composedComponents: "https://twitter.com/izzlax/status/1513167772364251140?s=20&t=jOExnZVVf-t19fTmlH0FIA"
      }
      -crawler: Embed\Http\Crawler {#1562 ▼
        -requestFactory: GuzzleHttp\Psr7\HttpFactory {#2013}
        -uriFactory: GuzzleHttp\Psr7\HttpFactory {#1997}
        -client: Embed\Http\CurlClient {#2014 ▶}
        -defaultHeaders: array:2 [▶]
      }
      #document: Embed\Document {#2001 ▶}
      #oembed: Embed\OEmbed {#1999 ▶}
      #linkedData: Embed\LinkedData {#1996 ▶}
      #metas: Embed\Metas {#1993 ▶}
      -settings: []
      -customDetectors: array:7 [▶]
      #authorName: Embed\Detectors\AuthorName {#1994 ▶}
      #authorUrl: Embed\Detectors\AuthorUrl {#1992 ▶}
      #cms: Embed\Detectors\Cms {#1991 ▶}
      #code: Embed\Detectors\Code {#1990 ▶}
      #description: Embed\Detectors\Description {#1989 ▶}
      #favicon: Embed\Detectors\Favicon {#1988 ▶}
      #feeds: Embed\Detectors\Feeds {#1987 ▶}
      #icon: Embed\Detectors\Icon {#1986 ▶}
      #image: Embed\Detectors\Image {#1985 ▶}
      #keywords: Embed\Detectors\Keywords {#1984 ▶}
      #language: Embed\Detectors\Language {#1983 ▶}
      #languages: Embed\Detectors\Languages {#1982 ▶}
      #license: Embed\Detectors\License {#1981 ▶}
      #providerName: Embed\Detectors\ProviderName {#1980 ▶}
      #providerUrl: Embed\Detectors\ProviderUrl {#1979 ▶}
      #publishedTime: Embed\Detectors\PublishedTime {#1978 ▶}
      #redirect: Embed\Detectors\Redirect {#1977 ▶}
      #title: Embed\Detectors\Title {#1976 ▶}
      #url: Embed\Detectors\Url {#1975 ▶}
      -api: Embed\Adapters\Twitter\Api {#1974 ▶}
    }
    "title" => null
Snapey's avatar

yeah, as expected the info you are after is in an array. See my previous reply

kikter's avatar

@Snapey sorry for this silly question how would I map into the second Array or in my post view I could do it in a foreach

@foreach($urlDetails as $Details)
  {{ $details['title'] }}
@endforeach
Snapey's avatar

@kikter do you expect to find more than one url in the post body?

otherwise, yes, loop over all the urls

kikter's avatar

@Snapey yes I think nso because a user can insert more than one url

kikter's avatar

@snapey after trying the foreach loop I dump it and I till got null

@foreach ($urlDetails as $details)
@dd($details['title'])
@endforeach 
Snapey's avatar

You will see in the earlier dump, title is null.

kikter's avatar

@Snapey please how can I get the title now and probably the image

Snapey's avatar

@kikter sorry, that's down to the package. Try another url that you know has a title

kikter's avatar

@snapey i was able to get the title with instagram link , but the of the image isnt working

when i dd again


  "info" => Embed\Adapters\Instagram\Extractor {#2001 ▶}
  "title" => "Instagram on Instagram: ““If I could describe Gen Z in three words, they would be radical, inclusive and unapologetic.” Anya Dillard (@iamajdillard) is the foun ▶"
  "image" => GuzzleHttp\Psr7\Uri {#1943 ▼
    -scheme: "https"
    -userInfo: ""
    -host: "instagram.flos1-1.fna.fbcdn.net"
    -port: null
    -path: "/v/t51.2885-15/278181615_407640024524677_1513490039406748060_n.jpg"
    -query: "stp=dst-jpg_e35_p1080x1080&_nc_ht=instagram.flos1-1.fna.fbcdn.net&_nc_cat=1&_nc_ohc=bjdIOXiQ-jEAX_jcODy&edm=AABBvjUBAAAA&ccb=7-4&oh=00_AT_w-DMTwgn0XkET-73qDtOKc ▶"
   @foreach($urlDetails as $details)
                {{ $details['title'] }}
               <img src="{{  $details['image'] }}" alt="" class="w-full h-44">
@endforeach 

Please or to participate in this conversation.