MattB
2 months ago

Pulling ID into form within modal

Posted 2 months ago by MattB

I have an edit form in a modal that's set to use the ID number from a table. For some reason, regardless of what entry is clicked, it will always pull in the same ID for some reason. Here are the table and the modal/form:

@extends('layouts.admin')
@section('content')
  <h1>Games <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#addNew">Add New</button></h1>
  @if($games)
<table class="table table-hover table-bordered" id="usersTable">
    <thead>
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Image</th>
        <th>Price</th>
        <th>Category</th>
        <th>Sold?</th>
        <th>Promote?</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
      @foreach($games as $game)
      <tr>
        <td>{{$game->id}}</td>
        <td>{{$game->title}}</td>
        <td><img src="{{$game->image}}" alt="" width="50"></td>
        <td>£{{$game->price}}</td>
        <td>{{$game->category->name}}</td>
        <td>{{$game->sold}}</td>
        <td>{{$game->promote}}</td>
        <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#editGame">Edit</button></td>
      </tr>
      @endforeach
    </tbody>
  </table>

@endif
<div class="modal fade" id="addNew" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
  <div class="modal-content">
    <div class="modal-header">
      <h5 class="modal-title">Add Game</h5>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      {!! Form::open(['method' =>'POST', 'action'=> '[email protected]', 'files'=>true, 'enctype'=>'multipart/form-data']) !!}
      @include('includes.admin.gameForm')
      <div class="form-group">
        {!! Form::submit('Add Game', ['class'=>'btn btn-primary']) !!}
      </div>
      {!! Form::close() !!}
    </div>
  </div>
</div>
</div>

<!--EDIT GAME SECTION-->
<div class="modal fade" id="editGame" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
  <div class="modal-content">
    <div class="modal-header">
      <h5 class="modal-title">Edit Game</h5>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      {!! Form::model($game, ['method' =>'PATCH', 'action'=> ['[email protected]', $game->id], 'files'=>true, 'enctype'=>'multipart/form-data']) !!}
      @include('includes.admin.gameForm')
      <div class="form-group">
        {!! Form::submit('Update Game', ['class'=>'btn btn-primary']) !!}
      </div>
      {!! Form::close() !!}
    </div>
  </div>
</div>
</div>
@stop

It's using this from the controller:

$games = Game::all();
        $categories = Category::pluck('name', 'id')->all();
        return view('admin.games.index', compact('games', 'categories'));

Also on a follow-up, is this the right way to go about updating the database based off the form input?

$gameSearch = Game::findOrFail($id);

          $file = $request->file('image');
          if($file){
            $name = $file->getClientOriginalName();
            if($file->move('images/games/', $name)){
              $games->image = '/images/games/' . $name;
              $games->title = $request->title;
              $games->price = $request->price;
              $games->category_id = $request->category_id;
              $games->promote = $request->promote;
              $games->sold = 0;
              $gameSearch->update($games);
              return redirect()->route('/admin');
            };
          };

Please sign in or create an account to participate in this conversation.