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

programmer13's avatar

how to pass a variable into a json post request

case 4:
                    $res=Http::post('localhost:5000/recommend', [
                    "customer"=>-1,
                    "criterion"=>"description",
                    "product_id"=>22017,
                    ])->json();
    
                    $this->products = collect($res);
                    dd($res);

the product id will be a select

0 likes
33 replies
Snapey's avatar

the product id will be a select

what does that mean?

programmer13's avatar

@Snapey i choose the product with a select i make the product id a variable inside the json

Sinnbeck's avatar

@programmer13 What does the http request return? A json object?

$res = Http::post('localhost:5000/recommend', [
                    "customer"=>-1,
                    "criterion"=>"description",
                    "product_id"=>22017,
                    ])->object();

$res->project_id = $id;
dd($res);
programmer13's avatar

@Sinnbeck yes

^ array:45 [▼
  1 => array:13 [▶]
  2 => array:13 [▶]
  3 => array:13 [▶]
  4 => array:13 [▶]
  5 => array:13 [▶]
  6 => array:13 [▶]
  7 => array:13 [▶]
  8 => array:13 [▶]
  9 => array:13 [▶]
  10 => array:13 [▶]
  11 => array:13 [▶]
  12 => array:13 [▶]
  13 => array:13 [▶]
  14 => array:13 [▶]
  15 => array:13 [▶]
  16 => array:13 [▶]
  17 => array:13 [▶]
  18 => array:13 [▶]
  19 => array:13 [▶]
  20 => array:13 [▶]
  21 => array:13 [▶]
  22 => array:13 [▶]
  23 => array:13 [▶]
  24 => array:13 [▶]
  25 => array:13 [▶]
  26 => array:13 [▶]
  27 => array:13 [▶]
  28 => array:13 [▶]
  29 => array:13 [▶]
  30 => array:13 [▶]
  31 => array:13 [▶]
  32 => array:13 [▶]
  33 => array:13 [▶]
  34 => array:13 [▶]
  35 => array:13 [▶]
  36 => array:13 [▶]
  37 => array:13 [▶]
  38 => array:13 [▶]
  39 => array:13 [▶]
  40 => array:13 [▶]
  41 => array:13 [▶]
  42 => array:13 [▶]
  43 => array:13 [▶]
  44 => array:13 [▶]
  45 => array:13 [▶]
]
Sinnbeck's avatar

@programmer13 That is an array not an object. Can you show and example of one item, and how you expect it to look?

programmer13's avatar

@Sinnbeck

^ array:45 [▼
  1 => array:13 [▼
    "collection" => "collection_hiver"
    "created_at" => "2022-07-29 14:57:57+00:00"
    "description" => "Pyjama en côte, la matière emblématique de petit Bateau. Bords côte en bas de manches et de jambes. Produit labellisé Standard 100 Oeko-Tex garantissant le resp ▶"
    "file" => "/5/2/5234501_15.jpg"
    "id" => "10736"
    "name" => "PYJAMA 14-ANS"
    "price" => "145"
    "simples" => "None"
    "sku" => "3102270327546"
    "status" => "active"
    "type_id" => "simple"
    "updated_at" => "2022-07-29 14:57:57+00:00"
    "visibility" => "1"
  ]
programmer13's avatar

@Sinnbeck

not change that  i consume an api that i give him in post 

 					customer"=>-1,
                    "criterion"=>"description",
                    "product_id"=>22017,
custumor id and criterion and  product id and it will give the product recommend similar to that product id 
Snapey's avatar
$res=Http::post('localhost:5000/recommend', [
                    "customer"=>-1,
                    "criterion"=>"description",
                    "product_id"=>$request->input('mySelectFieldName'),
                    ])->json();

Is that what you meant?

programmer13's avatar

@Snapey

i give him a select with product name that select one and give the product similar to that product based on customer id and criterion and product id i make
programmer13's avatar

@Snapey

  $res=Http::post('localhost:5000/recommend', [
                    "customer"=>-1,
                    "criterion"=>"description",
                    "product_id"=>$request->input('recommendation1'),
                    ])->json();
    
                    $this->products = collect($res);
                     dd($res);
undefined variable request
programmer13's avatar

@Sinnbeck @Sinnbeck

 <select id="case4" wire:model="recommendation1" name="recommendation1"
                            class="form-select form-select-lg mb-3  appearance-none block w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding bg-no-repeat border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
                            aria-label=".form-select-lg example">
                            <option selected>Open this select menu</option>
                            @foreach ($products as $product)
                            <option value="{{$product->id ?? $product['id']}}">{{$product->name ?? $product['name']}}
                            </option>
                            @endforeach

                        </select>
Sinnbeck's avatar

@programmer13 So I would assume that the product id is bound to $this->recommendation1

$res=Http::post('localhost:5000/recommend', [
                    "customer"=>-1,
                    "criterion"=>"description",
                    "product_id"=>$this->recommendation1,
                    ])->json();
programmer13's avatar

@Sinnbeck

  <div class="flex justify-center mt-7">
                    <div class="mb-3 xl:w-96">

                        <select id="emplacement1" onchange="trigerselect()" wire:model="recommendation"
                            class="form-select form-select-lg mb-3  appearance-none block w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding bg-no-repeat border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
                            aria-label=".form-select-lg example">
                            <option selected>Open this select menu</option>
                            <option value="1">Best Seller</option>
                            <option value="2">New Products</option>
                            <option value="3">Visiteur Recommendation</option>
                            <option value="4">Visiteur Similar Description</option>


                        </select>
                        @error('recommendation')
                        <div class="font-medium text-red-600">{{ $message }}</div>
                        @enderror
                    </div>
                </div>

                <div class="flex justify-center mt-7">
                    <div class="mb-3 xl:w-96">

                        <select id="case4" wire:model="recommendation1" name="recommendation1"
                            class="form-select form-select-lg mb-3  appearance-none block w-full px-4 py-2 text-xl font-normal text-gray-700 bg-white bg-clip-padding bg-no-repeat border border-solid border-gray-300 rounded transition ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
                            aria-label=".form-select-lg example">
                            <option selected>Open this select menu</option>
                            @foreach ($products as $product)
                            <option value="{{$product->id ?? $product['id']}}">{{$product->name ?? $product['name']}}
                            </option>
                            @endforeach

                        </select>
                        @error('recommendation')
                        <div class="font-medium text-red-600">{{ $message }}</div>
                        @enderror
programmer13's avatar

@Sinnbeck i select first the recommendation and the wheni choose its show me the select of product i select from that

programmer13's avatar

@Sinnbeck

class Recommendations extends Component
{
    public $section = 1;
    public $recommendation;
    public $recommendation1;
    public $name;
    public $type = 1;
    public $currentColor = 'red-700';
    public $fontsize = 'text-2xl';

    public $products;
    protected $rules = [
        'recommendation' => ['required'],
        'name' => ['required', 'min:3'],
        'type' => ['required'],
        'currentColor' => ['required'],
        'fontsize' => ['required'],
    ];
    public function updated($propertyName)
    {
        $this->validateOnly($propertyName);
    }

    public function mount()
    {
        $this->products = Product::orderBy('type_id')->take(10)->get();
    }

    public function hydrate()
    {
        $this->dispatchBrowserEvent('splide');
    }

    public function updatedRecommendation($value)
    {
        switch ($value) {
            case 1:
                $this->products =  Product::withCount('orderItem')->orderBy('order_item_count', 'desc')->take(10)->get();
                break;
            case 2:
                $this->products =  Product::orderBy('created_at', 'desc')->take(10)->get();
                break;
            case 3:
                $res=Http::post('localhost:5000/recommend', [
                "customer" => -1,
                "criterion" => "",
                ])->json();

                $this->products = collect($res);
                
                // $arrayOfIds = $collection->flatten()->pluck('id')->all();
                // $this->products=Product::whereIn('id', $arrayOfIds)->get();


                // $this->products=Product::where('id',$res->id)->get();
                break;
                case 4:
                  
                    $res=Http::post('localhost:5000/recommend', [
                    "customer"=>-1,
                    "criterion"=>"description",
                    "product_id"=>$this->recommendation1,
                    ])->json();
    
                    $this->products = collect($res);
                     dd($res);
Sinnbeck's avatar

@programmer13 So when you select something in the first select, the second select already have an option selected? If not it will be null

Sinnbeck's avatar

@programmer13 Sorry I dont understand what you mean. Was the idea perhaps that it should run when you select something in the second select, and not the first?

Snapey's avatar

@programmer13 perhaps this

    public function updatedRecommendation($value)

should be

    public function updatedRecommendation1($value)

Please or to participate in this conversation.