1

I installed a package (https://github.com/kirill-latish/laravel-newsapi).

And after setting everything up and also everything in the controller like so:

public function newsapi(){
  $response = NewsAPI::topHeadlines()->get([
        'country' => 'gb',
        'category'=>'sports'
    ]);
  return view('newsapi', compact('response'));
}

and then added into the view:

{{ $response }}

i'm getting a:

htmlspecialchars() expects parameter 1 to be string, object given

How can i work this? And also besides retrieving all the json, how can i display the things normally in a view? such as the title of the news article and url?

Thanks in advance for the help.

3
  • Can you give the full line where the $response is inserted in the view? Possibly you need to use {!! $response !!} to escape content. Can you also confirm $response is a string? Looking something like '{"key": "value"}' ?? Commented Sep 30, 2018 at 1:47
  • 1
    {{ }} in blade uses to echo(). so to echo() it should be a 'String' Commented Sep 30, 2018 at 1:56
  • Debug it's structure. Put dd($response); before return statement to see how object's structure looks like (and what keys you need from there). Post structure here too if you have further doubts. Commented Sep 30, 2018 at 10:42

1 Answer 1

1

Solved:

In controller:

$response = NewsAPI::topHeadlines()->get([
              'country' => 'us',
              'category'=>'technology'
          ]);
$articles = $response->articles;

In view:

@foreach ($articles as $article)
{{ $article->title }}
@endforeach
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.