【问题标题】:Route [news.shownewspublic] not defined路线 [news.shownewspublic] 未定义
【发布时间】:2022-01-16 04:57:31
【问题描述】:

我想查看一些数据,但我收到此错误 Route [news.shownewspublic] not defined。 (查看:C:\xampp\htdocs\ContentBaseApp - 1.0.2\resources\views\news\indexnewspublic.blade.php) 这是我的资源\视图\新闻文件夹 这是 resources\views\news\indexnewspublic.blade.php

@foreach ($news as $news)
  <a class="btn btn-outline-info" href="{{route('news.shownewspublic'),$news->id}}">
     <img src="/image/{{ $news->image }}" width="100px" class="col-2">
     <div class="col-2" style="color: #1d2124">
     <strong>App Name:- {{ $news->name }}</strong>
     </div>
 </a>
@endforeach

这是我的 NewsController.php

public function indexnewspublic()
{
  $news = News::latest()->paginate(5);
  return view('news.indexnewspublic',compact('news'))
     ->with('i', (request()->input('page', 1) - 1) * 5);
}
public function shownewspublic(News $news)
{
  return view('news.shownewspublic',compact('news'));
}

这是 web.php

Route::resource('news', NewsController::class);
Route::get('/indexnewspublic', [NewsController::class, 'indexnewspublic']);
Route::get('/shownewspublic', [NewsController::class, 'shownewspublic']);

【问题讨论】:

    标签: laravel-8


    【解决方案1】:

    在 indexnewspublic.php 中解决 @foreach ($news as $news) &lt;a class="btn btn-outline-info" href="{{ url('shownewspublic/' . $news-&gt;id) }}"&gt; {{ $news-&gt;name }} &lt;img src="/image/{{ $news-&gt;image }}" width="100px" class="col-6"&gt; &lt;/a&gt; @endforeach 这是web.php

    Route::get('indexnewspublic/{id}', [NewsController::class, 'indexnewspublic'])->name('indexnewspublic');
    

    这是 NewsController.php

    public function indexnewspublic()
    {
    $news = News::latest()->paginate(5);
    return view('news.indexnewspublic',compact('news'))
      ->with('i', (request()->input('page', 1) - 1) * 5);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-30
      • 2016-08-26
      • 2021-11-12
      • 2018-10-15
      • 2020-09-13
      • 2017-12-25
      • 2023-03-29
      相关资源
      最近更新 更多