我有两个方法show和index我在索引方法上有一个项目列表,当用户点击它时,会将她带到包含属于该id的一些数据的另一个页面。我没有这样做,而是想使用jquery在同一页面上加载数据。我在index.blade.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);
}
// }
答案 0 :(得分:0)
使用引导模式或弹出窗口。
根据您的需求设计category.show
视图。
调用ajax并将返回的视图响应附加到该模态体并显示它,以便它与您想要的视图完全相同。
示例代码用于替换,而ajax用于
这是替换模态
<!-- Show Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Show data</h4>
</div>
<div class="modal-body replace">
</div>
</div>
这将是你的ajax方法
$('.showModalButton').on('click',function(e){
var id = $(this).data('id'); // get id of resource you want to show
$('.replace').empty(); //
$.ajax({
url: 'your route/'+id,
type: "GET",
success:function(data) {
$('.replace').html(data);
},
error:function(jQXHR, textStatus, errorThrown) {
console.log(jQXHR);
console.log(textStatus);
console.log(errorThrown);
}
}); //end Ajax call
});
这将是您在控制器中的show方法
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);
}
设计category.show但是你要显示你的数据。