【问题标题】:Laravel search for topic by title and generate linkLaravel 按标题搜索主题并生成链接
【发布时间】:2015-08-04 17:25:54
【问题描述】:

我是 laravel 新手。我有这样的事情:

现在我不想在单击第一个主题时重定向到该主题网址。为此,我需要使用 DB:: 函数。我不想按给定的标题搜索,然后获取 id 等。例如我有帖子:

table name 与上图中的主题标题相同,因此我需要使用 DB::select 或 ::table 并按该名称搜索,然后打印结果。怎么做?

布局:

@extends('layouts.main')
@section('content')
<div class="panel panel-default">
  <div class="panel-heading">Forum</div>
  <div class="panel-body">
    @forelse($forums as $forum)
    @forelse($topics as $topic)
    <a href="HERE MUST BE CODE TO POST FOR EXAMPLE http://example.com/topic/pirmas-atnaujinimas/64">{{ $topic->title }}</a><br>
    @empty
    <div class="alert alert-danger">Apgailėstaujame tačiau šis forumas yra tuščias!</div>
    @endforelse
    @empty
       <div class="alert alert-danger">Apgailėstaujame tačiau forumas nerastas</div>
    @endforelse
  </div>
  </div>
  {!! $topics->render() !!}
@stop

提前致谢;(

更新 视图论坛控制器:

<?php 

namespace App\Http\Controllers;
use DB;
use View;

class viewForum extends Controller 
{
    public function showForum($fname, $fid)
    {
        $forums = DB::table('forums')
            ->where('id', $fid)
            ->where('seo_name', $fname)
            ->select()
            ->get();

        $topics = DB::table('topics')
            ->where('forum_id', $fid)
            ->select()
            ->paginate(1);

        return View::make('forum', compact('forums', 'topics'));
    }
}

【问题讨论】:

  • 请同时贴出相关控制器的代码。

标签: php laravel


【解决方案1】:

在您的主题 href 中执行以下操作:

 <a href="http://example.com/topic/{{ $topic->seo_name }}/{{ $topic->pid }}">{{ $topic->title }}</a><br>   

在你的 routes.php 中添加这个

 Route::get('topic/{fname}/{fid}', 'viewForum@showForum');

【讨论】:

    猜你喜欢
    • 2014-08-08
    • 2015-04-10
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2014-12-17
    • 1970-01-01
    • 2020-05-04
    相关资源
    最近更新 更多