nafeeur10's avatar

Automatically filled up with the selected id

I have some input fields.

-----------------------------------------------------------------------------------
Product Name | Product Description | Product Unit Cost
----------------------------------------------------------------------------------

Product Name is a Select-Option dropdown. While I will Select a Product, it's description and Cost will automatically filled based on the ID of selected product. I have all products in blade.

What will be the approach?

0 likes
16 replies
Nakov's avatar

@nafeeur10 Select the product ID, make a post request to an endpoint using the product ID, using Route model binding you will have the product with its full details:

Route::post('product/{product}', 'ProductsController@store')->name('create.product');

In the controller:

public function store(Product $product)
{
    dd($product); // description and cost within the product.
}
Nakov's avatar

@nafeeur10 You can use AJAX for that, but might be more complicated, then just do the following:

Route::post('product/create', 'ProductsController@store')->name('create.product');

In the view:

<form action="{{ route('create.product') }}" method="post">
    @csrf

    <select name="product_id">
        <option value="1">First Product</option>
        ...
    </select>
</form>

Then in the controller:

public function store(Request $request)
{
    $product = Product::findOrFail($request->product_id);
    dd($product); // description and cost within the product.
}
Nakov's avatar

@nafeeur10 on the select element you can add this:

onchange="this.form.submit()"

This will submit the form on the change event.

1 like
nafeeur10's avatar

@nakov , Thank you for your valuable answer.

I am submitting this form with a ID (like edit page)

http://localhost:8000/client/create/1

Web.php

Route::get('/invoice/create/{user}', 'InvoiceController@create')->name('invoice-index');

In this page, I passed a parameter {user}

With your procedure, I am getting the result, but when I want to back to this page, it is demanding a parameter.

Missing required parameters for [Route: invoice-index] [URI: client/invoice/create/{user}].

How will I back to my calling page with parameter?

public function select(Request $request)
{
        $productindividual = Product::findOrFail($request->product_id);
        return redirect()->route('invoice-index');
}

I think you got my point.

Nakov's avatar

@nafeeur10 well, yeah, you must pass the required parameter, try this:

return redirect()->route('invoice-index', auth()->id());

So this will use the authenticated user id, but you might not need that one, and use the User id instead that you want.

Nakov's avatar

@nafeeur10 that's why I said use the user that you want.. Do you have a User on your product?

$productindividual = Product::findOrFail($request->product_id);
return redirect()->route('invoice-index', $productindividual->user_id);

So simply said, if you want to redirect to that route you MUST pass a user, otherwise use a page that does not requires the user.

nafeeur10's avatar

@nakov ,

Solved this problem with giving parameter that user to the post url, but when it is back to the page, I am not getting the selected item. :-( as SELECTED....

Nakov's avatar

@nafeeur10 to do that you should put some logic as well. If you are talking about the product id in the select option, you can use something like this:

<option value="1" @if(old('product_id') === 1) selected @endif>First Product</option>

Keep in mind that 1 should be changed with the real ID of the product.

nafeeur10's avatar

@nakov,

Okay. It's not a major problem I think,

While I will post with the selected id, then I am redirecting to this page again with the value of selected id..... It's fine.

But before selecting how will this page will recognise the value of that. After posting it will know about that.

For example:

<input type="text" class="form-control" name="itemDescription" value={{ $pi->productDescription }}>
public function select(Request $request, User $user)
{
        $productindividual = Product::findOrFail($request->product_id);
        //dd($user);
        return redirect()->route('invoice-index', $user)->with(['pi' => $productindividual] );
}

So, before selecting he will not know about $pi.

Nakov's avatar

@nafeeur10 then what needs to be selected I don't understand? If the pi is not known before selecting how and what do you expect to be pre-selected?

nafeeur10's avatar

@nakov ,

They will empty before selecting.

After select an product, form will give me others data of that id and then they will fill up automatically with those data.

Nakov's avatar

@nafeeur10 I am sorry but I don't understand the flow. You will have to play around with the logic that I've shown you above.. so use:

{{ old('product_id', $pi->id) === 1 ? 'selected' : '' }}

Something along those lines on the <option> element.. That's how you pre-select. Make sure that you change $pi->id with whatever is the ID that you need.

anoshiri's avatar

If I understand you correctly, you will have to use Javascript to achieve that. Depending on what library you have in your installation. Vue or JQuery will do that for you. Vue binding will be perfect.

Please or to participate in this conversation.