mercredi 21 juillet 2021

how to access an array of images in a laravel colloction

i have uploaded images stored in an array in my MySql database. but I don't know how to access the contents of the array in the laravel collection. all the code I tried are giving me error that the array is a string.

this is the query of the posts from the database below.

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

this is the blade template showing the 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/"><img src="/storage/images/" 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/"></a>
                    </h3>
                     by </small> --}}
                    @if ($each_post->price > 0)
                        <small class=" text-green-500  text-xs md:text-base">   </small>
                    @else
                    <small class=" text-green-500 text-xs md:text-base"> free  </small>
                        
                    @endif
                    
                </div>
            </div>
    
            
        @endforeach
       
    </div>
        
    @else
        <p>No Posts</p>
    @endif

The $each_post->images is where the problem lies.

a dd($each_post) command displays this

 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 [▶]
}

please any suggestion?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3eCBNx2
via IFTTT

Aucun commentaire:

Enregistrer un commentaire