【问题标题】:how to access an array of images in a laravel colloction如何访问 laravel 集合中的图像数组
【发布时间】:2021-09-28 16:04:38
【问题描述】:

我已上传存储在 MySql 数据库中的数组中的图像。 但我不知道如何访问 laravel 集合中数组的内容。 我尝试的所有代码都给了我数组是字符串的错误。

这是对下面数据库中帖子的查询。

public function index()
{
    $posts = Post::orderBy('created_at', 'desc')->paginate(10);
    return view('posts.index')->with('posts', $posts);
}

这是显示帖子的刀片模板

@if (count($posts) > 0)
        @foreach ($posts as $each_post)
            <div class=" shadow-md bg-white pb-3  rounded-md " >
                <div class="  ">
                        <a href="/posts/{{$each_post->slug}}"><img src="/storage/images/{{ $each_post->image}}" class=" w-full  object-fill  rounded-t-md h-44 md:h-48" alt=""></a>
                  
                        
         
                </div>
                <div class="p-2">
                    <h3 class=" text-sm md:text-lg text-gray-800 mb-2">
                        <a href="/posts/{{$each_post->slug}}">{{$each_post->title}}</a>
                    </h3>
                    {{-- <small> Added on: {{$each_post->created_at}} by {{ $each_post->user->name}}</small> --}}
                    @if ($each_post->price > 0)
                        <small class=" text-green-500  text-xs md:text-base"> {{$each_post->price}}  </small>
                    @else
                    <small class=" text-green-500 text-xs md:text-base"> {{'free'}}  </small>
                        
                    @endif
                    
                </div>
            </div>
    
            
        @endforeach
       
    </div>
        {{$posts->links()}}
    @else
        <p>No Posts</p>
    @endif

$each_post-&gt;images 是问题所在。

dd($each_post) 命令显示此内容

 App\Models\Post {#387 
  #connection: "mysql"
  #table: "posts"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:12 [▼
    "id" => 37
    "title" => "happy salah"
    "slug" => "happy-salah-2"
    "description" => "jkjjcjc"
    "price" => "1,500"
    "venue" => "talba junction"
    "contact_info" => "09023234546"
    "created_at" => "2021-07-20 13:49:30"
    "updated_at" => "2021-07-20 13:49:30"
    "user_id" => 1
    "images" => "["downloadfile-2_1626788970.jpeg","downloadfile_1626788970.jpeg"]"
    "author_id" => null
  ]
  #original: array:12 [▼
    "id" => 37
    "title" => "happy salah"
    "slug" => "happy-salah-2"
    "description" => "jkjjcjc"
    "price" => "1,500"
    "venue" => "talba junction"
    "contact_info" => "09023234546"
    "created_at" => "2021-07-20 13:49:30"
    "updated_at" => "2021-07-20 13:49:30"
    "user_id" => 1
    "images" => "["downloadfile-2_1626788970.jpeg","downloadfile_1626788970.jpeg"]"
    "author_id" => null
  ]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
}

有什么建议吗?

【问题讨论】:

    标签: php mysql arrays laravel-5 eloquent


    【解决方案1】:

    我已经找到了解决问题的方法,所以无需提出答案。 谢谢大家。

    【讨论】:

      【解决方案2】:

      模型输出

          Illuminate\Database\Eloquent\Collection {#265 ▼
            #items: array:1 [▼
              0 => App\Models\Post {#266 ▼
                #table: "post"
                #connection: "mysql"
                #primaryKey: "id"
                #keyType: "int"
                +incrementing: true
                #with: []
                #withCount: []
                #perPage: 15
                +exists: true
                +wasRecentlyCreated: false
                #attributes: array:2 [▶]
                #original: array:2 [▼
                  "id" => 1
                  "image" => "["downloadfile-2_1626788970.jpeg","downloadfile_1626788970.jpeg"]"
                ]
                #changes: []
                #casts: []
                #classCastCache: []
                #dates: []
                #dateFormat: null
                #appends: []
                #dispatchesEvents: []
                #observables: []
                #relations: []
                #touches: []
                +timestamps: true
                #hidden: []
                #visible: []
                #fillable: []
                #guarded: array:1 [▶]
              }
            ]
          }
      

      控制器

          $post = Post::all();
          return view('post', ['post' => $post]);
      

      查看

          @foreach($post as $data)
              @foreach(json_decode($data['image']) as $image)
                  {{$image}}
              @endforeach
          @endforeach
      

      输出

          downloadfile-2_1626788970.jpeg downloadfile_1626788970.jpeg
      

      【讨论】:

      • 我试过了,但它给了我一个错误Invalid argument supplied for foreach()
      • 我已经用例子更新了答案,你可以检查一下
      猜你喜欢
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2020-04-14
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多