[英]Ajax get is not working - Internal Server Error
I have a homepage where there is a menu with some categories and below there are the latest 10 posts. 我有一个主页,上面有一个带有某些类别的菜单,下面是最新的10个帖子。
A post can have many categories and one category can belong to many posts so there are 2 models and a pivot table "category_post" with 2 columns: id and name. 一个帖子可以有多个类别,一个类别可以属于许多帖子,因此有2个模型和一个带有2列的数据透视表“ category_post”:id和name。
So in the homepage, there is a menu with some categories and below the posts: 因此,在主页中,在帖子下方有一个带有一些类别的菜单:
<ul class="Categories__Menu">
@foreach($categories->take(6) as $category)
<li class="ative">
<a href="" name="category" id="{{$category->id}}">{{$category->name}}</a>
</li>
@endforeach
</ul>
<div class="row" id="posts">
@foreach($posts as $post)
<div class="col-12 col-sm-6 col-lg-4 col-xl-3 mb-4">
<div class="card">
<img class="card-img-top" src="{{$post->image}}" alt="Card image cap">
<h5 class="card-title">{{$post->name}}</h5>
<div class="card-footer d-flex justify-content-between align-items-center">
<a href="{{route('posts.show', ['id' => $post->id, 'slug' => $post->slug])}}" class="btn btn-primary text-white">More</a>
</div>
</div>
</div>
@endforeach
</div>
I want that when each category is clicked to show only the posts of that category in the homepage, but in the same homepage, not in a specific category page. 我希望当单击每个类别以仅在首页中显示该类别的帖子,而在同一首页中而不是特定类别页面中显示该帖子。 So maybe the best approach is using AJAX.
因此,也许最好的方法是使用AJAX。
But Im getting an error accessing " http://proj.test/posts/where/category/5 " 但是我访问“ http://proj.test/posts/where/category/5 ”时出错
ReflectionException (-1)
Class PostController does not exist
Or when I click in a category in the menu it appears in the console: 或者,当我单击菜单中的类别时,它会出现在控制台中:
GET http://proj.test/posts/where/category/5 500 (Internal Server Error)
Do you know where is the issue? 您知道问题出在哪里吗?
The code: 编码:
In the FrontController I already pass the categories and posts to the homepage view: 在FrontController中,我已经将类别和帖子传递到了主页视图:
FrontController: FrontController:
class FrontController extends Controller
public function index(){
return view('home')
->with('categories', Category::orderBy('created_at', 'desc')->get())
->with('posts', Post::orderBy('created_at','desc')->take(10)->get());
}
}
Post and Category models: 职位和类别模型:
class Post extends Model
{
public function categories(){
return $this->belongsToMany('App\Category');
}
}
class Category extends Model
{
public function posts(){
return $this->belongsToMany('App\Post');
}
}
PostController: PostController:
public function WhereHasCategory(Request $request)
{
$posts = Post::whereHas('categories', function ($categories) use (&$request) {
$categories->where('id',$request->id);
})->get();
return response()->json($posts);
}
Route to the homepage: 转到首页:
Route::get('/', [
'uses' => 'FrontController@index',
'as' =>'index'
]);
Route to the ajax part: 路由到ajax部分:
Route::get('posts/where/category/{id}','\PostController@WhereHasCategory')->name('category.posts');
Ajax: 阿贾克斯:
$(function() {
$("a[name='category']").on('click', function(){
var category_id = $(this).attr("id");
$.ajax({
url: '{{ route('category.posts',null) }}/' + category_id,
type: 'GET',
success:function(result){
$('#posts').empty();
$.each(result,function(index, postObj){
$('#posts').append("<p>"+postObj.title+"</p>");
});
console.log(result);
},
error: function(error) {
console.log(error.status)
}
});
});
});
You've defined the class wrong in the ajax route. 您在ajax路由中定义了错误的类。
Route::get(
'posts/where/category/{id}',
'\PostController@WhereHasCategory'
)->name('category.posts');
You've put the controller as \\PostController
when I imagine it's App\\Http\\Controllers\\PostController
. 你已经把控制器
\\PostController
,当我想象它的App\\Http\\Controllers\\PostController
。 When you prefix the class with \\
it tells it that it's in the global scope, with no namespace. 当您在类的前面加上
\\
它告诉它在全局范围内,没有名称空间。 Remove the \\
. 删除
\\
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.