如何使用jQuery在索引方法上加载Laravel中显示方法的内容

how to load the content of a show method in laravel on the index method using jquery

本文关键字:方法 显示 Laravel jQuery 何使用 索引 加载      更新时间:2023-09-26

我有两个方法 show 和 index我在索引方法上有一个项目列表,当用户单击时,它会将她带到另一个页面,其中包含属于该 ID 的一些数据。 而不是这样做,我想使用 jQuery 来实现这一点,使数据加载在同一页面上。我的索引上有以下内容.php 视图,请问如何在 Laravel 中实现这一目标

       @foreach ($categories as $category)
        <div class="body">
        <h4><a style="text-decoration: none; " href="{{ URL::route('category.show', $category->id) }}">{{$category->name}}</a></h4>
        </div>
       @endforeach
    <?php
     namespace App'Http'Controllers;
     use Illuminate'Http'Request;
     use illuminate'HttpResponse;
     use App'Http'Requests'todolistRequest;
     use App'Http'Requests'CreateCategoryRequest;
     use App'Http'Requests;
     use App'Companylist;
     use App'Category;
     use  Illuminate'Support'Facades'DB;
     class CategoryController extends Controller
     {
public function create(){
    return view('category.create');
}
public function index(){
    $categories=Category::all();
    return view('category.index',compact('categories'));
}
public function store(CreateCategoryRequest $request){
     $category = new Category($request->all());
     $category->save();
      return 'Redirect::route('category.create')->with('message',      'Your list has been created!'); 
}
      public function show($id)
      {
      $category = Category::findOrFail($id)->companylist()->get();
      $cat=Category::findOrFail($id);
    // this my route
    Route::resource('category','CategoryController');
    return view('category.show')->with('category',     $category)->with('cat',$cat);
 }

//
}

要在索引页上显示详细信息数据而不刷新页面,您应该使用 ajax ,在 ajax 中返回数据并将其显示在索引页中(即在每个列表下)

在 ajax 中,您必须传递 id,并根据 id 返回相应的数据。

希望对你有帮助