By select value in a PHP variable: LARAVEL BLADE

Asked

Viewed 212 times

1

I’m trying to get the value of select "Categories" and put in the variable "$article" using Laravel’s Slides, but I’m not finding how to do this. Someone can give me a light?

   <?php
    $categories = DB::table('products')->select('category')->get(); 
    $categories = array_unique($categories, SORT_REGULAR);
    array_splice($categories, 0, 1); //exluindo os produtos que ainda não tem pategorias 
  
    ?>                                     
    <label class="control-label">Filtro:</label>
  <div class="">
    <a href="#" data-open-in="picker" data-picker-close-text="OK" data-back-on-select="true" class="item-link smart-select">
      <select  class="products_qto select-filter-cat"  name="categories" id="filter-cat" >
          <option selected>ARTIGOS</option>
          @foreach ($categories as $cat_item)
               <option value="{{$cat_item->category}}">{{$cat_item->category}}</option>
          @endforeach   
        </select> 
    </a>
  </div> 

  
  <?php 
  $artigo = '????';
   ?>

1 answer

4


Apparently you are a little confused about how PHP works. PHP is a server-side language, that is, interpreted on the server. There is no way for a variable in PHP to receive the value of a select, which is rendered in client-side.

What you can do is generate a form, send the data via POST and receive in the controller. Also, it is not recommended to create variables in the view. What you can do is create in the controller and forward to the view.

Ex:

<form action="{{ route('minharota') }}" method="post">
<!-- minhaview.blade.php -->
@csrf
    <select name="meucampo">
        <option value="1"> Meu valor 1 </option>
        <option value="2"> Meu valor 2 </option>
    </select>
    <button type="submit"> Enviar form </button>
</form>
<h1>Valor: {{ $variavel ?? ' (O formulário ainda não foi enviado) ' }}

// controller

class MeuController 
{

    public function meuMetodo(Request $request)
    {
        $variavel = $request->meucampo;

        return view('minhaview')->with('variavel', $variavel);
    }
}

//routes.php
Route::post('minharota')->uses('MeuController@meuMetodo')->name('minharota');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.