how to get the value of a parameter in url in the same view file in laravel

Viewed 77

i have a blade file that i submit a value as query string in the view like below :

  @foreach($productImageHelper->getProductImageColor($product) as $item)
    <label class="circle">
    <img class="img-fluid" src="{{$item['small_image_url']}}" alt="">
    <input type="radio" name="color_id" value="{{$item['color_id']}}">
    </label>
   @endforeach

so now when i submit this form my url would be like below :

http://localhost:8000/products/exampleproduct?color_id=1

now in the same view file i want to recive the value of color_id and submit it to another url so the user selected color would go into his cart . how can i recive the color id and put it in some hidden input like below :

<input type="hidden" name="color_id" value="{{somecolorid}}">
2 Answers

If I understood well your issue, you could do:

<input type"=hidden" name="color_id" value="{{ request()->query('color_id') }}>

Notice that this is a quick & dirty way to achieve it, the cleanest way is to retrieve the data from a controller which then gives it to the Blade view. By the way, the view has no idea where the data comes from and it does not matter.

There is the good way and the bad way, the good one is sending it through your controller or your function along side with the view using compact or what ever you use. suppose this is the function that return the view

public function functionName(){
    $color_id = request('color_id') // this will contain the color id
    return view('your_view', compact('color_id'));
}

and in your view you would have something like

<input type="hidden" name="color_id" value="{{$color_id}}">

Now the bad one, is to use request('color_id') directly in your view without passing it using compact.

Related