0

i am inserting data of which event subscribed button is clicked , for that i need event id here i am passing event id but each time i am getting last event id

i have tried this for loop and passing specific event id

   <form action="{{route('postSubscribed')}}" method="POST">
          @csrf
          <input type="text" name="search" placeholder="Search Event...">
      </div>
      
      <div class="row">
        @foreach($data as $index => $event)
        <div class="col-sm-6 col-md-4 col-lg-3">
          <div class="box">
           
              <div class="img-box">
                <img src="{{ asset('storage/' . $event->image) }}" alt="">
              </div>
              <input type="hidden" name="eventId" value="{{ $event->id }}">
              <div class="detail-box">
                <h6>
                  {{$event->eventName}}
                </h6>
                <h6>
                  Price
                  <span>
                    {{$event->price}}
                  </span>
                </h6>
              </div>
              <div class="detail-box">
                <p class="mute-text">
                    {{$event->desc}}
                  </p>
              </div>
              <div class="location-box">
                <span style="font-size: 24px;">&#x1F4CD;</span>
                <p class="location-text">
                  {{$event->location}}
                </p>
              </div>
              <div class="btn-box">
                <button name="submit" type="submit">
                 Subcribe
                </button>
              </div>
            
          </div>
        </div>
@endforeach

and in controller i am printing that

  public function postSubscribed(Request $request)
    {
        $eventId = $request->input('eventId');
        dd($eventId);
    }
Ruby
  • 69
  • 5
  • You have multiple elements with the same `name="eventId"`. Last one wins. Use `name="eventId[]"` to make those fields submit as an array. Not Laravel related btw. https://www.php.net/manual/en/faq.html.php#faq.html.arrays (Also, that first `` is invalid) – brombeer Aug 03 '23 at 07:38
  • but i want only when id in request as i want to insert that data only which event button is clicked @brombeer – Ruby Aug 03 '23 at 07:43
  • 1
    Oh, ok. Then place your `
    ` _inside_ of your loop so you get as many forms as you have items/ids.
    – brombeer Aug 03 '23 at 07:45
  • not working that is passing id=3 everytime – Ruby Aug 03 '23 at 07:47
  • Then you did something wrong. Why `id=3`? You don't submit an `id` field – brombeer Aug 03 '23 at 07:49
  • i also don't know – Ruby Aug 03 '23 at 07:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254774/discussion-between-miral-jogi-and-brombeer). – Ruby Aug 03 '23 at 07:56
  • What do you need the form for anyway? Why not simply use a link with `?eventId={{ $event->id }}`? – brombeer Aug 03 '23 at 07:56
  • is it a good way using anchor tag? – Ruby Aug 03 '23 at 08:02

0 Answers0