5

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?

4
  • So what's the target? Display only one on upload() method? Commented Dec 3, 2017 at 15:18
  • yes, display the one with the respective id Commented Dec 3, 2017 at 15:19
  • On the same filename upload.blade.php? Commented Dec 3, 2017 at 15:20
  • I have written it above : the upload function aims to show a data for a single upload when its name is clicked from the list of uploads shown by the uploads function. Commented Dec 3, 2017 at 15:22

3 Answers 3

3
// 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');
Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

thanks for the answer. i tried it but it gives me this error : (2/2) ErrorException Trying to get property of non-object (View: C:\xampp\htdocs\logoss\resources\views\uploads.blade.php)
you can use something like <div class="well">{{ dump($uploads) }} to see what actually is being passed
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.