Share value between function in same controller

Published 4 weeks ago by zettz

Hi there, I need help on how to share result between function on the same controller.

For example: search function results can be reuse at another function in the same controller.

Current controller:

public function searchProduct ()
{
    $search = Product::find(\Request::get('barcode'));
}
public function show ()
{
    $user = User::All();
    $result = $this->searchProduct(); // this line always return null
    return view('products.result', compact('user', 'result'));
}

So, how can I get the searchProduct() result on the show() function?

Thank you.

Best Answer (As Selected By zettz)
zettz

After some trial and error, I only need to load the data after ajax success inside the modal with no change at at controller/route/view at all.

Snapey
Snapey
4 weeks ago (1,036,605 XP)

reference it with the $this prefix

$result = $this->searchProduct();
zettz
zettz
4 weeks ago (1,980 XP)

@Snapey , done with it, but it always return null at blade view. When I check the console log, I get the result based on inputted barcode number.

Vilfago

Are you sure that searchProduct() stand alone return something ?

Could we see the view if the problem come from here ?

zettz
zettz
4 weeks ago (1,980 XP)

@Vilfago , yes, I get the result base on input if I make it into separate page. But when I share the result at other function, it always return null.

tykus
tykus
4 weeks ago (690,060 XP)

Return something from searchProduct if you intend to assign a variable wherever it is consumed

Snapey
Snapey
4 weeks ago (1,036,605 XP)

and also, is the primary key for Product the 'barcode' field?

more likely

public function searchProduct ()
{
     return Product::where('barcode', request('barcode'))->first();
}
zettz
zettz
4 weeks ago (1,980 XP)

@tykus , can you give an example? I'm really confused.

zettz
zettz
4 weeks ago (1,980 XP)

@Snapey , yes, barcode is the primary key. I already tried the

return Product::where('barcode', request('barcode'));

but same, return null too.

tykus
tykus
4 weeks ago (690,060 XP)

Assuming your query is correct:

public function searchProduct ()
{
    return Product::find(\Request::get('barcode'));
}
Snapey
Snapey
4 weeks ago (1,036,605 XP)

When using where you have to get the results. If you only want the single product then use find() or first()

zettz
zettz
4 weeks ago (1,980 XP)

@tykus and @Snapey , I get the result now, but when at blade, if I click the show button after I inputted the barcode number, I got error "Trying to get property 'product_name' of non-object" Example:

// The line below still return nothing
{{ $result->product_name }}
Snapey
Snapey
4 weeks ago (1,036,605 XP)

Well that one search function is not going to give you a working view on its own. Therefore you need to show the controller, the view file and your routes

zettz
zettz
4 weeks ago (1,980 XP)

@Snapey , For controller, just like above except a little change:

public function searchProduct ()
{
    Return Product::find(\Request::get('user_id'));
}
public function show ()
{
    //$user = User::All();
    $werehouse = Werehouse::all();
    $result = $this->searchProduct();
    return view('products.result', compact('werehouse', 'result'));
}

Route:

Route::post('/product/search', '[email protected]')->name('product.src');
Route::get('/product/result', '[email protected]')->name('product.show');

View:

@section('content')
<section class="content">
{{ Form::open([ 'method' => 'POST', 'style' => 'display: inline', 'id' => 'form-src', 'data-url' => route('product.src') ]) }}          
    {{ Form::text('barcode', old('barcode'), array('placeholder' => 'Barcode Number') }}
    <button type="submit" class="btn btn-xs btn-warning" id="src">
        <i class="fa fa-search fa-fw"></i>
    </button>
{{ Form::close() }}

@foreach ($werehouse as $wh)
    // werehouse data
@endforeach
</section>

<div class="modal fade" id="show" tabindex="-1" data-backdrop="static" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true"><i class="fa fa-times"></i></span>
            </button>
        </div>
        <div class="modal-body">
            {{ $result->product_name }}
        </div>
        </div>
    </div>
</div

@endsection

and ajax:

$(document).on('click', 'button#src', function (e) {
    e.preventDefault();
    var self = $(this);
    $.ajax({
        url : $('form#form-src').data('url'),
        type : "POST",
    data : $('form#form-src').serialize(),
        success : function(data){
            console.log(data);
            $('#show').modal('show');
        },
        error : function(data){
            console.log(data);
        }
    });
});
zettz
zettz
3 weeks ago (1,980 XP)

After some trial and error, I only need to load the data after ajax success inside the modal with no change at at controller/route/view at all.

Snapey
Snapey
3 weeks ago (1,036,605 XP)

no change in the controller

are you sure you didnt need to return the value... or did you forget that bit

Please sign in or create an account to participate in this conversation.