andrews-quest's avatar

andrews-quest liked a comment+100 XP

2mos ago

I don't use React, so I'm not sure about your situation. However, I pass parameters in the Route() method, using named routes. I also use inertia's useForm().

So my call might look something like:

my_form.put( route( 'calls.edit', { 'param1': value1, 'param2': value2 } ) );

Inertia's useForm() is pretty helpful and route() makes the code cleaner. I don't use the method or action attributes in forms anymore. For more info, see the 'Form Helper' section in the docs:

Inertia Forms - Form Helper

Note the Inertia route() is slightly different than the Laravel version, because it passes the params as an object rather than an array.

Also, fwiw, I only use post, put or delete on forms.

Inertia's useForm() makes form handling fairly straightforward for most situations, and you can fall back to axios (included), if it's needed.

HTH,

andrews-quest's avatar

andrews-quest wrote a reply+100 XP

2mos ago

Thank you for your answer! Do you have an advice on what's the best practice to develop a search on the site? Would you rather do it with forms or otherwise?

andrews-quest's avatar

andrews-quest started a new conversation+100 XP

2mos ago

I'm trying to use Inertia <Form> to pass some parameters to a URL request, along some other ones that are stored as variables in React.

For that I'm trying to use object merging like:

<Form
method='GET'
action='/calls'
transform={data => ({...queryParams, ...data})}
>

But that flops because, for some reason, the form's URL parameters always get overwritten by whatever other parameters I have stored in queryParams.

Another problem would be that all the Parameters gets put into the URL, even if they are empty.

Is there an elegant solution? Is my approach beneficial at all?

andrews-quest's avatar

andrews-quest wrote a reply+100 XP

5mos ago

Alright, I've figured it out.

There was a syntax error in the code of one of my Controllers.

I realised that when I ran php artisan wayfinder:generate --with-form separately - it had printed the stack trace then.

andrews-quest's avatar

andrews-quest started a new conversation+100 XP

5mos ago

Hi, when building with Vite I get the following message:

error during build: [@laravel/vite-plugin-wayfinder] [plugin @laravel/vite-plugin-wayfinder] Error generating types: Error: Command failed: php artisan wayfinder:generate --with-form

And that's only at the dev server. When I try the same on the release, everything runs smooth.

I've tried comparing the composer.lock files on the remote and the dev, but they are identical as expected.

andrews-quest's avatar

andrews-quest wrote a reply+100 XP

6mos ago

Yes, that's the solution I meant. That's how it works in my app for now.

But you mentioning the sorting directions made me ponder, maybe I should really switch to Spatie as soon as possible.

Thanks a lot!

andrews-quest's avatar

andrews-quest wrote a reply+100 XP

6mos ago

Oh, I see. I'll stick with the out-of-the-box solution for now, but will definetely look further into this approach on latter stages.

andrews-quest's avatar

andrews-quest liked a comment+100 XP

6mos ago

Hi, I have a bunch of calls, which I need to return sorted by different columns (/calls/datetime, /calls/operator etc.).

What'd be the correct approach in this scenario? Should the index() function of the CallController capture the URL parameter and base its output based on that? Should it be split into multiple functions?

@andrews-quest You would use a query string parameter here to control how you want records sorting, i.e.

  • /calls?sort=datetime
  • /calls?sort=operator

You can use Spatie’s Laravel Query Builder package as mentioned by @glukinho to implement sorting (https://spatie.be/docs/laravel-query-builder/v6/features/sorting):

class CallController extends Controller
{
    public function index()
    {
        $calls = QueryBuilder::for(Call::query())
            ->allowedSorts([
                'datetime',
                'operator',
                // Any other column you want to be able to sort by...
            ])
            ->paginate();

        return view('calls.index', compact('calls'));
    }
}
andrews-quest's avatar

andrews-quest wrote a reply+100 XP

6mos ago

It seems to be a clever approach, thanks. Maybe, if you have an example of such class or a whole architecture that uses it, it would be helpfull to take a look.

andrews-quest's avatar

andrews-quest wrote a reply+100 XP

6mos ago

Well, I've ended up pulling them out in the Controller like

class CallController extends Controller 
{
public function index (Request $request) {
	// ...
	datetime = $request->query('datetime');
	// ...
}
	// ...
}

Thank you for your advice!