Я пытался создать кнопку редактирования / обновления в моей таблице данных, но кажется, что кнопка не работает должным образом, я пробовал много решений из inte rnet, но я не смог найти ничего, что помогло.
Вот мой взгляд
<body>
<header>
<div class="menu-wrap">
<input type="checkbox" class="toggler">
<div class="hamburger"><div></div></div>
<div class="menu">
<div>
<div>
<ul>
<li><a href="/create_item">Input Menu</a></li>
<li><a href="/country_input">Country</a></li>
<li><a href="/state_input">State</a></li>
<li><a href="#">City</a></li>
</ul>
</div>
</div>
</div>
</div>
</header>
<br />
<div class="container box">
<h3 align="center">Input Data</h3><br />
<form action="{{url('/create_item')}}" method="post">
{{ csrf_field() }}
<div class="form-group">
Country :
<input type="text" name="country" required>
</div>
<div class="form-group">
State :
<input type="text" name="state" required>
</div>
<div class="form-group">
City :
<input type="text" name="city" required>
</div>
<div class="flex-center position-ref">
<div class="content">
<div class="button">
<div class="links">
<input type="submit" name="submit" value="Add">
<a href="http://127.0.0.1:8000/">Back</a>
</div>
</div>
</div>
</div>
</form>
</div>
@if (session('alert'))
<div class="alert alert-success">
{{ session('alert') }}
</div>
@endif
<table id="datas">
<tr>
<th>id</th>
<th>Country</th>
<th>State</th>
<th>City</th>
<th>Manage</th>
</tr>
@foreach($data as $value)
<tr>
<td>{{ $value-> id }}</td>
<td>{{ $value-> country }}</td>
<td>{{ $value-> state }}</td>
<td>{{ $value-> city }}</td>
<td>
<button data-id="{{$value->id}}" data-ecountry="{{$value->country}}" data-estate="{{$value->state}}" data-ecity="{{$value->city}}" data-toggle="modal" data-target="#myModal" style="border:none">Edit</button>
<a href="/deleteCreateItem/{{ $value->id }}"><button>Delete</button></a>
</td>
</tr>
@endforeach
</table>
<!-- Button trigger modal -->
<!-- <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
Launch demo modal
</button> -->
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content" style="background-color:#E8E8E8">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h2 class="modal-title" id="myModalLabel">Edit Data</h2>
</div>
<div class="modal-body">
<form action="{{url('/create_item',$value->id)}}" method="post">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH" />
<div class="form-group">
Country :
<input type="text" name="country" id="country" required>
</div>
<div class="form-group">
State :
<input type="text" name="state" id="state" required>
</div>
<div class="form-group">
City :
<input type="text" name="city" id="city" required>
</div>
</form>
</div>
<div class="modal-footer">
<!-- <a href="/updateItem/{{ $value->id }}"><button>Save Changes</button></a> -->
<div class="flex-center position-ref">
<div class="content">
<div class="button">
<div class="links">
<button type="button" data-dismiss="modal">Close</button>
<input type="submit" name="submit" value="Save Changes">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
$('#myModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget) // Button that triggered the modal
var ecountry = button.data('ecountry') // Extract info from data-* attributes
var estate = button.data('estate') // Extract info from data-* attributes
var ecity = button.data('ecity') // Extract info from data-* attributes
// If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
// Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
var modal = $(this)
modal.find('.modal-body #country').val(ecountry)
modal.find('.modal-body #state').val(estate)
modal.find('.modal-body #city').val(ecity)
})
</script>
Я использовал модал для кнопки редактирования
Вот мой маршрутизатор
Route::get('/', function () {
return view('welcome');
});
Route::get('/create_item', function() {
return view('create_item');
});
Route::post('/create_item','CreateItem@insert');
Route::get('/create_item','CreateItem@getData');
Route::post('/create_item/{id}','CreateItem@update');
Route::get('/deleteCreateItem/{id}','CreateItem@delete');
Вот мой Контроллер
class CreateItem extends Controller
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
function insert(Request $req)
{
$country = $req->input('country');
$state = $req->input('state');
$city = $req->input('city');
$data = array('country'=>$country,"state"=>$state,"city"=>$city);
DB::table('country_state_city')->insert($data);
return back()->with('alert','Data inputed successfully!');
}
function update(Request $req, $id)
{
DB::table('country_state_city')->where('id',$req->id)->update([
'country' => $req->country,
'state' => $req->state,
'city' => $req->city
]);
return back()->with('alert','Data updated successfully!');
}
function getData()
{
$data['data'] = DB::table('country_state_city')->get();
if(count($data) > 0)
{
return view('create_item',$data);
}
else
{
return view('create_item');
}
}
function delete($id)
{
DB::table('country_state_city')->where('id',$id)->delete();
return redirect('/create_item')->with('alert','Data Deleted!');
}
}
Я все еще не понимал, какую часть маршрутизатора можно получить и отправить / получить