-1

I am new to laravel. I want to send data as id of existing data. Id comes from products.blade I send via href tag as shown below to gallery page. I have tried to find a way through other sites but it still doesn't work

<a class="btn btn-success" href="/dashboard/galleries/{{ $product->id }}"><i class="ri-image-add-line text-white"></i></a>

then i create a route like this

Route::resource('/dashboard/galleries', DashboardGalleryController::class)->middleware('admin')->shallow();

in the controller gallery, I made like this

public function index($id)
    {
        $gallery = Gallery::where('products_id', $id)->get();
        return view('dashboard.galleries.index', compact('gallery'));
    
    }

then inside the gallery page, I want to display a table to add images based on that id.

dashboard/galleries/index.blade.php

<h1>{{ $gallery->id }}</h1>

should i add data inside foreach or outside?

Rens
  • 3
  • 6
  • `$product = [...] $product->id`? That is never going to work. Should be `$product = Gallery::where('products_id', $id)->get();` (notice `$id` in `index($id)`? Also, `$product` is a terrible name for a Collection of `Gallery` records; should be `$galleries`, or similar (plural, since `->get()` returns Many, and because it's not a `Product`) – Tim Lewis Nov 23 '22 at 15:59
  • hello @TimLewis, thanks for responding to my question. I've tried changing it but still nothing happens. In the gallery view, it only displays blank and no error messages at all – Rens Nov 23 '22 at 17:01
  • You didn't include the code for `dashboard/galleries/index.blade.php`, so until you [edit your question](https://stackoverflow.com/posts/74549637/edit) and include that code, there is no way for us to help you further. – Tim Lewis Nov 23 '22 at 17:03
  • I've added the blade view, the rest of it looks just normal bootstrap like a table – Rens Nov 23 '22 at 17:12
  • Yeah, `$gallery->id` is not going to work; you have **multiple** `Gallery` instances returned by `->get()`, how is your code supposed to know which `->id` to reference? This is why I suggested you use `$galleries`, so you can do `$galleries = Gallery::where('products_id', $id)->get()`, then `return view('dashboard.galleries.index', compact('galleries'));`, then `@foreach($galleries as $gallery)`, then `{{ $gallery->id }}` will work. – Tim Lewis Nov 23 '22 at 17:16

1 Answers1

-1

A restful resource controller sets up some default routes for you and even names them. and ur case the logic should be inside show() function not index()

check this issue it will help u same issue solved here