0

如果这是一个愚蠢的问题,我仍在学习,请原谅我,但我自己似乎无法找到解决方法。

我在获取 JSON 的“title”属性和我得到的错误时遇到了问题

“试图获取非对象页面/results.blade.php:28 的属性‘标签’”

我知道 $tags 是一个数组而不是一个对象。

我用 $tags 做了一个 foreach

foreach ($result->tags as $tag)

我也知道 $tag 包含我需要的数据,因为当我死并转储 $tag 时,我得到了这个

在此处输入图像描述

我的问题是如何访问数组内的未命名 JSON?

title 属性位于“tags”数组中,浏览器给我一个错误,我无法访问它,因为它是一个非对象。

你能帮我写代码吗?

这是我的控制器

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use GuzzleHttp\Client;

class PageController extends Controller
{

    // public function __construct() {
    //     $this->middleware('auth');
    // }
        

    public function index() {
        $user = Auth::user();
        return view('pages/home', compact('user'));
    }

    public function result(Request $request) {
        // https://api.unsplash.com/search/photos?query=philippines&client_id=hb-UQIJ2DMaPckaJOO5nxrC90uYnaVRGTMz3S8WHzJY
        $input = $request->input('query');
        $client = new Client();
        // var_dump($input); die;
        // dd($input);
        // var_dump($request->input('query')); die;
        $res = $client->request('GET', "https://api.unsplash.com/search/photos", [
            "query" => [
                "query" => $input,
                "client_id" =>"hb-UQIJ2DMaPckaJOO5nxrC90uYnaVRGTMz3S8WHzJY",
                "per_page" => 100
                ]
            ]);

        $data = $res->getBody();
        
        $data = json_decode($data);
        
        $filteredData = [];
        
        // return $data->results;

        foreach($data->results as $result) {
            $urls = $result->urls;

            array_push($filteredData,$result);

                foreach ($result->tags as $tag) {
                    $tags = $result->tags;
                    array_push($filteredData, $tag);
                    // dd($tag);
                    foreach($tag as $key => $value) {
                            if($key === 'title') {
                                array_push($filteredData,$value);
                                array_push($filteredData,$key);
                                // dd($filteredData);
                            }
                        }
                    
                }

        $user = Auth::user();
        return view('pages/results', compact('user', 'filteredData', 'input'));
    }
}

这是我的刀片

    @extends('layouts/main')

@section('title')
    Design Storm - Inspiration for Developers
@endsection

@section('content')
  <div id="site-section">
    <div class="container">
      <div id="results">
        <div>
          <div class="search-container">
            <form action="/results" method="POST">
              @csrf
              <input class="search" type="text" value="{{$input}}" placeholder="Search" name="query">
            </form>
          </div>
          <div class="boxes">
            <div class="row">
              @foreach ($filteredData as $result)
              <div class="col-md-3">
                <div class="box">
              

    <div style="position: relative; background: url('{{$result->urls->small}}') no-repeat center center;-webkit-background-size: cover;-moz-background-size: cover;-o-background-size: cover;background-size: cover; height: 200px;">
                    <div class="add-btn "><i class="fa fa-check" aria-hidden="true"></i></div>
                  </div>
                  @foreach ($filteredData as $tag)
                    <h4>
                      {{$filteredData->tag->title}}
                    </h4>
                  @endforeach
                </div>
              </div>
              @endforeach
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
@endsection

"

4

0 回答 0