我的控制器中有两种方法。第一个叫做uploads
,它显示数据库表中的所有记录,如下所示:
public function uploads()
{
//return Upload::all();
$uploads = Upload::all();
return view('uploads')->with('uploads',$uploads);
}
此功能正常,我的视图中已成功检索到所有记录。
问题在于第二种方法upload
,其目的是在上传列表中点击其名称时显示单个上传的数据。
目前,我有这个:
public function upload($id)
{
return Upload::find($id);
}
我不确定如何完成此功能。
我的观点如下:
@extends('layouts.app')
@section('content')
<h1>Uploads</h1>
@if(count($uploads)>0)
@foreach($uploads as $upload)
<div class="well">
<h3><a href="/uploads/{{$upload->id}}">{{$upload->name}}</a> </h3>
<small>Written on {{$upload->created_at}}</small>
</div>
@endforeach
@else
<p>No uploads found</p>
@endif
@endsection
我不确定要放入web.php
的内容,所以我的路线看起来像这样:
Route::get('/uploads', function () {
return view('uploads');
});
Auth::routes();
Route::get('/uploads','UploadController@uploads')->name('uploads');
有人能帮助我做这项工作吗?我只想在点击上传的名称时看到关联数组与数据库中的记录。在路线和我看来,我需要添加什么?
答案 0 :(得分:3)
// Controller
public function uploads()
{
$uploads = Upload::all();
return view('uploads')->with('uploads', $uploads);
}
您可以在此处使用Laravels IOC容器,让Laravel在您的控制器操作中传递正确的上传。
public function showUploadDetail(Upload $upload)
{
return view('uploadDetail')->with('upload', $upload);
}
使用示例内容创建名为uploadDetail.blade.php
的新刀片文件:
@extends('layouts.app')
@section('content')
<h1>Upload Detail</h1>
<p>ID: {{ $upload->id }}</p>
@endsection
正如fil已经提到的那样,调整您的路线:
Route::get('/upload/{id}', 'UploadController@upload')->name('upload');
答案 1 :(得分:0)
好的尝试这样的事情
public function upload($id)
{
$uploads = Upload::find($id);
return view('uploads')
->with('uploads', $uploads);
}
并在你的刀片中
@extends('layouts.app')
@section('content')
<h1>Uploads</h1>
@if(is_array($uploads) and count($uploads) > 0)
@foreach($uploads as $upload)
<div class="well">
<h3><a href="/upload/{{$upload->id}}">{{$upload->name}}</a> </h3>
<small>Written on {{$upload->created_at}}</small>
</div>
@endforeach
@else
<div class="well">
<h3><a href="/uploads/{{$uploads->id}}">{{$uploads->name}}</a> </h3>
<small>Written on {{$uploads->created_at}}</small>
</div>
@endif
@endsection
在你的路线
Route::get('/upload/{id}', 'UploadController@upload')->name('upload');
或者您可以创建一个单独的刀片,对于单个视图,说刀片的名称是upload.blade.php而不是uploads.blade.php,以便在文件名和目的方面更加清晰。
答案 2 :(得分:0)
您没有在web.php中指定显示单个记录的路由。
Web.php
所以,只需将以下路线添加到您的web.php
即可Route::get('/uploads/{upload_id}','UploadController@upload')->name('single.upload');
使用UploadController中的以下功能更改上传功能: 此函数检索上载记录并将该记录返回到视图:
控制器
public function upload($upload_id)
{
$upload= Upload::find($upload_id);
//var_dump($upload); //Un-comment this line to show full Returned Data
return view('view_upload',compact('upload'));
}
创建名为view_upload.blade.php的刀片文件,以显示所选的单个上传内容。
@extends('layouts.app')
@section('content')
<h1>Upload Detail</h1>
<p>ID: {{ $upload->id }}</p>
//Following Line Display all the Record.
// var_dump($upload);
@endsection