【问题标题】:Problem in show posts in Laravel categoryLaravel 类别中显示帖子的问题
【发布时间】:2020-04-23 22:55:20
【问题描述】:

我的帖子的分类表是这样的 1,3,4,8 这个数字是类别的 ID

例如帖子 X 在数据库中有 4 个这样的类别 (2,7,13,24)

好的,现在我用这段代码显示我的类别

控制器代码

    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::get('category/{category_name}','categoryController@index');

查看代码

    @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

但我不知道如何在页面中显示类别的帖子

我在学习 Laravel 之前使用此代码显示每个类别的帖子 但现在我不知道如何在 laravel 中使用此代码

请帮帮我。谢谢

    $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();

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    你应该在控制器中试试这个

     $category = DB::table("category")->where('cat_name', $category_name)->first();       
     return view('category/index', compact('category'));
    

    【讨论】:

      猜你喜欢
      • 2017-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多