[英]Problem in show posts in Laravel category
my post's category table like this 1,3,4,8 this numbers is category's ID我的帖子的类别表像这样 1,3,4,8 这个数字是类别的 ID
for example post X have 4 category like this (2,7,13,24) in database例如帖子 X 在数据库中有 4 个这样的类别 (2,7,13,24)
ok now i show that my category with this code好的,现在我用这段代码显示我的类别
Controller CODE Controller 代码
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class categoryController extends Controller
{
public function index($category_name)
{
$category_name_exist = DB::table("category")->where('cat_name', $category_name)->exists();
if ($category_name_exist) {
$category_name = DB::table("category")->where('cat_name', $category_name)->first();
return view('category/index', ['category' => $category_name]);
} else {
return view('404');
}
}
}
Route CODE路线代码
Route::get('category/{category_name}','categoryController@index');
View CODE查看代码
@extends('layout/main')
@section('title') Category @stop
@section('content')
<section class="page-title-section section-padding">
<div class="container">
<div class="row">
<div class="col-md-12 ">
<h2 class="page-tagline text-center">Now view the category domains</h2>
<h1 class="page-title text-center xl">{{$category->cat_name}}</h1>
</div>
</div>
</div>
</section>
@stop
but i dont know how must show the posts of category in page但我不知道如何在页面中显示类别的帖子
i before Learn Laravel use this code for show posts of each category but now i don't know how must use this code in laravel我在学习 Laravel 之前使用此代码显示每个类别的帖子,但现在我不知道如何在 laravel 中使用此代码
Please help me.请帮我。 Thanks谢谢
$get_domain_cat_id = $_GET['id'];
$query_cat = $conn->query("select * from category WHERE cat_id='$get_domain_cat_id'");
if ($query_cat->rowCount() == 0) {
header("location:$url");
}
$fetchall = $query_cat->fetchObject();
$query_cats2 = $conn->query("select * from domains WHERE FIND_IN_SET('$get_domain_cat_id',category)");
$fetchalls2 = $query_cats2->fetchall();
You should try this one in the controller你应该在 controller 中试试这个
$category = DB::table("category")->where('cat_name', $category_name)->first();
return view('category/index', compact('category'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.