1

These are my controllers

<?php

public static function getAccessToken()
{
    $url = 'http://api.tech/oauth/authenticate';
    $query = [
        'grant_type' => 'client_credentials',
        'client_id' => 'E3PuC',
        'client_secret' => 'IhvkpkvMdAL7gqpL',
        'scope' => 'bookings.read,images.create,images.read,images.update,locations.read,rates.read,rates.update,reports.read,reviews.read,rooms.create,rooms.delete,properties.read',
    ];

    $client = new Client();
    $response = $client->get($url, ['query' => $query]);
    $content = json_decode($response->getBody()->getContents());

    if ($content) {
        return $content->access_token;
    } else {
        return null;
    }
}

public function getReviews()
{
    $client = new Client();
    $access_token = $this->getAccessToken();
    $url = 'http://api.tech/hotels/88244/reviews';
    $query = [
        'access_token' => $access_token,
    ];

    $response = $client->get($url, ['query' => $query]);
    $content = json_decode($response->getBody()->getContents());

    if ($content->status == 'success') {
        // return $content->access_token;
        return $content->data;
        //    return $response;
    } else {
        return null;
    }

}

public function index()
{
    $content = $this->getReviews();
    return view('channel.channel', [
        'content' => $content
    ]);
}

When i try to output the content in my blade as a link, it says ---

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

and this is my blade file

<a href="{{$content}}">This</a>

It also throws an error when i try to output it like thus

{{$content}}

Please How can i solve the error My question isn't a duplicate cause once i dd it shows an array and i want a link to show the array on a different page

12
  • 1
    Possible duplicate of Laravel - htmlspecialchars() expects parameter 1 to be string, object given Commented May 31, 2018 at 13:46
  • stackoverflow.com/questions/40045920/… try {{dd($content)}} it's probably a formating issue Commented May 31, 2018 at 13:47
  • 1
    your $content is an array. do {{dd($content)}} and see Commented May 31, 2018 at 13:47
  • I believe the best practise is to use: $content = $client->get($url, ['query' => $query])->json()); Otherwise, just use $content = json_decode($response->getBody()); Commented May 31, 2018 at 13:58
  • @Indra when i do dd it shows an array, but u want to show the variable as a javascript object on a different page when clicked on the link Commented May 31, 2018 at 13:59

1 Answer 1

0

Try Using: {! $content !} Or Use: @json($content)

Sign up to request clarification or add additional context in comments.

1 Comment

Please add some explanation to your answer such that others can learn from it. Wouldn't it look pretty strange to have a json encoded string within the href attribute?

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.