简体   繁体   中英

Retrieving and displaying a single record from database in laravel

I have two methods in my controller. The first is called uploads and it displays all the records from a DB table, it looks like this :

 public function uploads()
    {
        //return Upload::all();
        $uploads = Upload::all();
        return view('uploads')->with('uploads',$uploads);
    }

This function is working and all the records are successfully retrieved in my view.

The issue is with the second method upload which aims to show a data for a single upload when its name is clicked from the list of uploads.

Currently, I have just this :

 public function upload($id)
{
    return Upload::find($id);
}

And I am not sure how to complete this functionality.

My view is looking like this :

 @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

I wasn't really sure what to put in web.php , so my routes look like this :

    Route::get('/uploads', function () {
    return view('uploads');
});

Auth::routes();
Route::get('/uploads','UploadController@uploads')->name('uploads');

Can someone help me make this work? I just want to see the associative array with the record from the database when I click on an upload's name. What do I need to add in routes and in my view?

// Controller
public function uploads()
{
    $uploads = Upload::all();
    return view('uploads')->with('uploads', $uploads);
}

You can use Laravels IOC container here and let Laravel pass the correct Upload in your controller action.

public function showUploadDetail(Upload $upload)
{
    return view('uploadDetail')->with('upload', $upload);
}

Create a new blade file called uploadDetail.blade.php with the sample content:

@extends('layouts.app')

@section('content')
   <h1>Upload Detail</h1>
   <p>ID: {{ $upload->id }}</p>
@endsection

And as already mentioned by fil adjust your route to:

Route::get('/upload/{id}', 'UploadController@upload')->name('upload');

Okay try something like this

public function upload($id)
{ 
  $uploads = Upload::find($id);

  return view('uploads')
    ->with('uploads', $uploads);
}

and in your blade

@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

In your route

Route::get('/upload/{id}', 'UploadController@upload')->name('upload');

Or you can create a separate blade, for single view, say the name of blade is upload.blade.php not uploads.blade.php for more clarity in term of the filename and purpose.

You didn't specify the Routes to show the Single Record inside your web.php.

Web.php

so, Just add the Following Route to your web.php

Route::get('/uploads/{upload_id}','UploadController@upload')->name('single.upload');

Change your upload function with the Following Function inside your UploadController: This function retrieves the Upload Record and return that record to the View:

Controller

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'));
}

And create the blade file with name view_upload.blade.php to show that single selected upload.

@extends('layouts.app')

@section('content')
   <h1>Upload Detail</h1>
   <p>ID: {{ $upload->id }}</p>
   //Following Line Display all the Record.
   // var_dump($upload);
@endsection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM