2

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

public function newCall(){

   $new_calls = new_calls::all();     //fetches everything in the new_calls table in the database
    return view('calls.newCall')->with('new_calls', $new_calls);     //passes it to the view
}

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

The issue is with the second method viewCall whose aim is to show data for a single call when its name is clicked from the list of newCalls.

Currently, I have just this:

public function viewCall(){
    return view('calls.viewCall');
}

I plan on formatting the data from the database (for a particular call) with some static text while displaying it in my viewCall.blade.php.

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

Route::get('/calls/newCall', 'CallsController@newCall');
Route::get('/calls/viewCall/{id}', 'CallsController@viewCall')->name('viewCall');

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 a call's name. What do I need to add in routes and in my view?

This is my newCall.blade.php

<div class="table-responsive">
    <table class="table no-margin table-striped">
        <thead>
        <tr>
            <th>Terminal ID</th>
            <th>Terminal Name</th>
            {{-- <th>Fault Description</th> --}}
            <th>Call Logged On</th>
            <th>ATM Variant</th>
        </tr>
        </thead>
        <tbody>
        @if(count($new_calls) > 0)
        @foreach($new_calls as $calls)
        <tr>
            <td><a href="/calls/viewCall/{{$calls->id}}" title="view more details">{{$calls->terminal_id}}</a></td>
            <td>{{$calls->terminal_name}}</td>
            {{-- <td style="width:350px">{{$calls->fault_description}}</td> --}}
            <td>{{$calls->created_at}}</td>
            <td>{{$calls->atm_variant}}</td>
        </tr>
        @endforeach
        @else
        <h1>No new Calls</h1>
        @endif
        </tbody>
    </table>
</div>

This is my viewCall.blade.php

<address>
    <strong>{{$new_call->client_name}}</strong><br>
    {{$new_call->address}}<br>
    <b>Phone: {{$new_call->phone}}</b> <br>
    <b>Email: {{$new_call->email}}</b>
</address>

nb: I have also tried using {{$calls->email}} but it's returning undefined variable: calls and new_calls in each case

4
  • Check my answer below and tell me if it works for you or not. Commented Nov 10, 2018 at 10:56
  • Add your newcall.blade.php and viewcall.blade.php file and I will look at it. Commented Nov 11, 2018 at 11:54
  • Did you updated your viewCall function as shown in my answer below? Commented Nov 11, 2018 at 16:48
  • yes...please view the updated version of my post..I included my foreach statement.. am still getting the undefined variable calls error Commented Nov 12, 2018 at 8:52

3 Answers 3

2

You can do something like this:

public function viewCall($id)
{
    return view('calls.viewCall', ['call' => new_calls::findOrFail($id)]);
}

This will get the id from the route and call a function to find the object with the given id.

If you have problems to deal with the variable in the view. Try to check the object using dd. Try this:

public function viewCall($id)
{
    $new_call = new_calls::find($id);
    dd($new_call);
}

Ref: https://laravel.com/docs/5.7/controllers#basic-controllers

Sign up to request clarification or add additional context in comments.

5 Comments

nothing is there except HTML and CSS codes but they are all imported as the headers and the footer
I was hoping to view the data in its raw state ie in array so i can customize the view
The variable that is going to be sent to viewCall is $call. In the view you can get attributes from this variable. I edited my answer with a debug code. Try to check if $new_call is printed correctly.
it returned the following new_calls {#234 ▼ #fillable: array:6 [▶] #table: "new_calls" #connection: "mysql" #primaryKey: "id" #keyType: "int" +incrementing: true #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:10 [▶] #original: array:10 [▶] #changes: [] #casts: [] #dates: [] #dateFormat: null #appends: [] #dispatchesEvents: [] #observables: [] ...and others } but i want it to return the information of the call I clicked in an array
thanks...I made some errors in my variable names and I have corrected them..my code now workes....thanks soo much... i really appreciate
1

What you need to do is get the new_calls model by the ID. Which you can do like so:

routes/web.php

Route::get('/calls/viewCall/{id}', 'CallsController@viewCall');

CallsController.php

public function viewCall($id) // the id in the url
{
    $new_call = new_calls::find($id);

    return view('calls.viewCall')->with('new_call', $new_call);
}

In your view (I assume you're looping through all the calls to display them) create a link.

// your existing foreach
@foreach($x as $y)
    <a href="/calls/viewCall/{{$y->id}}">View Call</a>
@endforeach

8 Comments

That's what return view('calls.viewCall')->with('new_call', $new_call); does. In your view, you can access the call like so: $new_call with any properties.
Just add the view that you want the link to the call page in and I'll look at it.
I tried <small class="pull-right">Call logged on: {{$new_call->created_at}} </small> and I got the undefined variable new_call error
You should use {{$new_call->created_at}}. Because the properties get passed into the view as $new_call
Please add all relevant views to your post: viewcall.blade.php and the one that you have all the calls in.
|
0

I recommend too looking at naming convetions and route model binding and type hinting in the controller.
Then you'd have something like this:

    public function viewCall(NewCall $newcall)
{
    return view('calls.viewCall')->with($newcall);
}

and route would be like this:

Route::get('/calls/viewCall/{newcall}', 'CallsController@viewCall')->name('viewCall');

4 Comments

thanks for your response but it says "Class App\Http\Controllers\NewCall does not exist"
So where you did: $new_call = new_calls::find($id); new_calls is your class. That is poor naming convention. Your model is new_calls and should be singular. Think of a model as 1 row in the DB. new_call::allI() = all rows. new_fall::find($id) == 1 row. I would change my model to NewCall but since you did it like that already you can do: Public function viewCall(new_calls, $newcall) { return view('calls.viewCall')->with($newcall); } . Now what you are type hinting at is $newcall is an instance of new_call. It is an eloquent.
thanks...I made some errors in my variable names and I have corrected them..my code now workes....thanks soo much... i really appreciate
@Austin . Really read those laravel docs. Route model binding and type hinting are so important and help keep code clean/readable, which will go a long way. Good luck with your project.

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.