Return and display a single record from a database in laravel

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

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

This function works, and all records are successfully retrieved in my view.

The problem is with the second method upload, the purpose of which is to display data for a single download when you click on a name from the list of downloads.

Currently, I only have this:

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

And I'm not sure how to perform this function.

My view is as follows:

 @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 was not sure what to add web.php, so my routes look like this:

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

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

- ? , . ?

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

Laurvels 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');
+2

, -

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 .

0

You did not specify Routes to display a single entry in your web.php.

Web.php

so add the following route to your web.php

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

Modify the upload function with the following function inside your UploadController: This function retrieves the upload entry and returns this entry 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 a click file called view_upload.blade.php to show this highlighted 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
0
source

Source: https://habr.com/ru/post/1690197/


All Articles