У меня есть простая таблица для моих тегов.
Маршрут
route::resource('admin/tags','TagController');
Контроллер:
public function index()
{
$tags=Tag::latest()->paginate(5);
return view('products.tag',compact('tags'));
}
Просмотр:
@extends('admin.index')
@section('content')
...html code...
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">tag name</th>
<th scope="col">tag slug</th>
<th scope="col">description</th>
</tr>
</thead>
<tbody>
@foreach($tags as $tag)
<tr>
<th scope="row">{{$tag->id}}</th>
<td>{{$tag->tag_name}}</td>
<td>{{$tag->tag_slug}}</td>
<td>@{{$tag->tag_description}}</td>
</tr>
@endforeach
</tbody>
</table>
<div class="paging site-pagination">
{{$tags->links()}}
</div>
...html code...
@endsection
Сейчас, этот код работает как талисман, но я хочу получить данные из ajax, а затем перейти к моему представлению.
Мой код ajax:
$(document).ready(() => {
get_data()
function get_data() {
$.ajax({
type: 'GET',
url: '/admin/tags',
beforeSend: function () {
console.log('before')
},
success: function (data , status , xhr){
console.log(data);
console.log(status );
console.log(xhr);
},
complete: function (data) {
console.log('complete')
},
error: function (data) {
console.log('error')
},
});
}
)}
Хорошо. Теперь моя функция успеха - Работа и получениемой HTML.но я просто хочу получить переменную tags
и передать ее для просмотра.
Я изменил свой контроллер следующим образом:
//return view('admin.pages.products.tag',compact('tags'));
return response()->json($tags);
Теперь мой вид тегов:
{
"current_page": 1,
"data": [
{
"id": 46,
"tag_name": "EbTLzDikpS",
"tag_slug": "mKXcpVOF3L",
"tag_description": "05HMeVazYO",
},
...
{
"id": 5,
"tag_name": "T4uvmukgZA",
"tag_slug": "fpWwimnnIk",
"tag_description": "A0nfZ4POHR",
}
],
"first_page_url": "http://127.0.0.1:8000/admin/tags?page=1",
"from": 1,
"last_page": 10,
"last_page_url": "http://127.0.0.1:8000/admin/tags?page=10",
"next_page_url": "http://127.0.0.1:8000/admin/tags?page=2",
"path": "http://127.0.0.1:8000/admin/tags",
"per_page": "5",
"prev_page_url": null,
"to": 5,
"total": 46
}
Теперь, даже если я удалю свою функцию ajax, мой взгляд не изменится.
Так что я снова изменяю свой контроллер:
return view ('admin.pages.products.tag', compact('tags'))->render();
Все еще не работает.