orim
orim

Reputation: 143

Laravel, get data from controller for display purposes

I'd like to display data calculated in controller in a blade view based on data provided by a user in form . For now I'm doing this by:

//View xyz.blade.php
<div class="card">
    <div class="card-header">Add link</div>
    <div class="card-block">
        {{ Form::open(array('url' => 'getSimilar', 'method' => 'GET')) }}
        {{ Form::token() }}
        <div class="form-group">
            {{ Form::label('url', 'url:') }}
            {{ Form::text('url', '') }}
        </div>
        {{ Form::submit('Get') }}
        {{ Form::close() }}
    </div>

    @if( ! empty($similar))
        <div class="card-block">
            @foreach ($similar as $item)
                {{$item->title}} <br/>
            @endforeach
        </div>
    @endif
</div>

//Controller
public function getSimilar(){
    ..
    return View('xyz', ['similar' => $found]);
} 

The above code works as expected. I can provide a url and after clicking "Get" I can see a list of found items below the form. However, something tells me that this is not the right way to do this since the entire page will refresh. Am I right (and so the above code is bad)? If so, is there any build in feature to display fetched data without refreshing? I searched on the official Laravel-form page but I did not find anything.

Upvotes: 0

Views: 120

Answers (1)

Adriano Rosa
Adriano Rosa

Reputation: 8771

As far as I could understand your question.

The code seems to be ok, but the logic you've created may not be possible without a page refresh, because the user interaction depends the server response which requires a new reload.

You can craft another interactive action without a page refresh using an AJAX so when the user clicks the button he gets the result from the server you then display the results in page. Because the AJAX Request/Response happens behind the scenes for the user it gives an impression the page didn't reload which may be what you want.

Upvotes: 2

Related Questions