Search code examples
phplaravelweb-crawlergoutte

Trying to extract Goutte data to view


I have been working for hours trying to figure out how to extract some data to my view in Laravel.

my controller is as follows:

  public function practice(Request $request) 
    {

        $client = new Client();
        $crawler = $client->request('GET', 'https://www.polkadotpassport.com/');

$crawler->filter('.post-header')->each(function($node){
   $title = $node->filter('h2 > a');
    return view('flights', compact('title'));
});


    }

and then my view is:

@extends('layouts.app')

@section('content')

@foreach($title as $titles)
<h1>{{$titles}}</h1>
@endforeach


@endsection

any help would be appreciated!


Solution

  • Assuming the crawler is returning a collection, try something like:

    public function practice(Request $request)
    {
            $client = new Client();
            $crawler = $client->request(
                    'GET',
                    'https://www.polkadotpassport.com/
            ');
    
            $titles = $crawler->filter('.post-header')->map(function($node){
                    return $node->filter('h2 > a');
            });
    
            return view('flights', ['titles' => $titles]);
    }