Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sk8rboi7566's avatar

Query Not Showing results after returning back to the view

I have a listall view which displays all the conversations from a table. I used a controller to pass in the query and display it on the page. I also created a delete controller which will then take the current one selected, match the first conversation found and delete it from the backend, api, and return back to the listall view. But the issue is when i return to the view it displays as empty. If i refresh the page it will return to normal. Do I save the query on top of the page for it to display the conversations regardless of passing the data into the view? Or do i update my delete controller to query the data again and reference the same variable from the listall controller?

0 likes
17 replies
sk8rboi7566's avatar

@jlrdw would it be easier for the delete controller to just use

return back();

and have the view template itself query the database so it gets updated every time?

sk8rboi7566's avatar

i am using

return back()->with('conversations', $conversations);`
jlrdw's avatar
jlrdw
Best Answer
Level 75

I don't what setup you have, ajax, regular blade view, etc. I haven't looked up the back helper in the api, but it should do similar to:

        $areturn = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        echo "<br>";

        $haystack = $areturn;
        $needle = '?';
        $pos = strripos($haystack, $needle);
        if ($pos === false) {  // page 1
            $areturn = $areturn . "?sch=" . Session::get('search') . "&acct=" . Session::get('account') . "&page=1";
            Session::set('areturn', $areturn);
        } else {

            Session::set('areturn', $areturn);
        }

Which is used in an older framework I have. basically it returns where I left off after an edit, otherwise if I was on page 1, it returns there.

Usage in controller:

$vurl = Session::get('areturn');
 return redirect($vurl);

And yes the complete cycle of querying the data takes place.

What is happening when you use:

return back();

If it is not working correctly try to build yourself a custom redirect.

Again above was just example of what needs to happen to get you back where you were. And ajax will be different.

1 like
Snapey's avatar

yes return back without data

more help needs code

sk8rboi7566's avatar

@Snapey Here is my relevant code:

Conversation Controller Listall function:

   public function listAll(){
        $conversations = Conversation::orderBy('updated_at', 'desc')->paginate(2);
        return view('conversations.list')->with('conversations' , $conversations);
    }

Delete function:

public function delete(Request $request){
        //get record in conversation table that matches the channel_id request column value
        DB::table('conversations')->where('Channel_Id', '=', $request->Channel_Id)->delete();
        
		//get updated conversations
        $conversations = Conversation::orderBy('updated_at', 'desc');
		//return back with conversations
        return view('conversations.list')->with("conversations", $conversations);
    }

List Blade

@extends('layouts.app')
@section('content')
    <div class="flex justify-center">
        <div class="w-10/12 lg:w-8/12 bg-white p-6 rounded-lg text-center">
            <h2 class="text-3xl mb-4">All Conversations</h2>
            @if (session('message'))
                <div class="font-black bg-gray-200 p-5 mb-5 text-center">
                    {{ session('message') }}
                </div>
            @endif
                <div class="p-5 block relative h-auto">
                    <table class="table-auto border-b-2 border-gray-500 m-auto">
                        <thead class="border-b-2 border-t-2 border-gray-500">
                        <tr>
                            <th class="border-l-2 border-r-2 p-3 border-gray-500">Conversation Title</th>
                            <th class="border-r-2 p-3 border-gray-500">Channel Id</th>
                            <th class="border-r-2 p-3 border-gray-500">Recipient #</th>
                            <th class="border-r-2 p-3 border-gray-500">Last Updated</th>
                            <th class="border-r-2 p-3 border-gray-500">View</th>
                            <th class="border-r-2 p-3 border-gray-500">Delete</th>
                        </tr>
                        </thead>
                    @isset($conversations)

                    @foreach($conversations as $convo)
                        <tbody>
                                <tr class="border-3 bg-gray-100">
                                    <td class="border-l-2 border-r-2 p-3 border-gray-500">{{ $convo->Friendly_Title }}</td>
                                    <td class="border-r-2 p-3 border-gray-500">{{ Str::of($convo->Channel_Id)->limit(8) }}</td>
                                    <td class="border-r-2 p-3 border-gray-500">{{ $convo->Recipient_Number }}</td>
                                    <td class="border-r-2 p-3 border-gray-500">{{ \Carbon\Carbon::parse($convo->updated_at)->DiffForHumans() }}</td>
                                    <td class="border-r-2 p-3 border-gray-500">
                                        <form action="{{ route('conversations.conversation', $convo->Channel_Id)  }}" method="GET" class="inline p-3">
                                            @csrf
                                            <button type="submit" class="inline-flex items-center"><span class="px-2">View</span>
                                                <span> <svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-3l-4 4z" />
                                                    </svg></span></button>
                                        </form>
                                    </td>
                                    <td class="border-l-2 border-r-2 p-3 border-gray-500">
                                        <form action=" {{ route('conversations.list')}}" method="POST">
                                            @csrf
                                            @method('delete')
                                            <input type="hidden" name="Channel_Id" value="{{$convo->Channel_Id}}">
                                            <button type="submit"><svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                                                </svg></button>
                                        </form>
                                    </td>
                                </tr>
                        </tbody>
                                <div id="pagination">{{ $conversations->links() }}</div>
                     @endforeach
                        @endisset

                    </table>
                </div>
        </div>
    </div>
@endsection
Snapey's avatar

at the end of the delete, you should redirect to the original page. not return the view

1 like
sk8rboi7566's avatar

@Snapey thanks. i will test this with the pagination in place and see if everything works. hate that it is a simple fix.

sk8rboi7566's avatar

@Snapey when i try to delete from a paginated url it sends me back to my homepage for some reason. when there is no pagination it will redirect properly

sk8rboi7566's avatar

@jlrdw i updated my current code from @snapey suggestion

return view('conversations.list')->with("conversations", $conversations);

to

return back();

in the delete function

jlrdw's avatar

@sk8rboi7566 That's what I originally suggested, I gave documentation reference.

But is it working?

sk8rboi7566's avatar

@jlrdw it is working but now i am stuck with another issue. If i am on a paginated url it will no redirect me back to that page. For example:

/conversations/list?page=2

it returns me to my homepage. I understand if the paginated page is no longer there because of the deletion. But that doesnt seem to be the case.

jlrdw's avatar

@sk8rboi7566 then just build your redirect back with session. But the back should work. Also try previous.

https://laravel.com/docs/8.x/helpers#urls

$previous = url()->previous();

I usually just build my own redirect, like I showed above.

Edit:

I just tried both, neither worked with a query string and pagination. But my original works fine:

            $vurl = Session::get('areturn');
            return redirect($vurl);

Right after editing a record it returns me to the very page I was on i.e.,

http://localhost/laravel859/dog/indexadmin?psch=c&aval=y&page=2

Just test data I use.

sk8rboi7566's avatar

@jlrdw i solved it by doing

redirect()->route('conversations.list');

thanks for the help

jlrdw's avatar

@sk8rboi7566 hope it works out fine.

Also just fyi, I just gave you a quick example, the other frame work actually returns the query string part from a method of the paginator:

    public function getQuerystring()
    {
        return $this->params != null ?  '?' . $this->pageName . "=" . $this->getPage() . '&' . http_build_query($this->params) : null;
    }

So there's a lot more to it. My example was just a "quick how to" store the complete url into session so as to redirect back. I was surprised laravel's return back(); did not work. But it probably would if query string was added.

I plan to research this more in the laravel api.

Please or to participate in this conversation.