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

vincej's avatar
Level 15

Suggestions for Converting Laravel Collective

Years ago I moved from CodeIgniter to Laravel at version 4.2. Back then the be-and-end-all was using Laravel Collective. Jeffrey recommended it and so too did loads of people, and so dutifully I complied. It's been great, but now it is dead, and so my fear is one day Taylor will do something which breaks Collective.

So - question, in a perfect world I would prefer to convert the thousands of lines of Collective into raw HTML. That is future proofed. Is there some magical way I can so this without spending forever manually converting each page? Even with PHPStorm Replace functions, I will certainly break something and end up having to go through each page manually. anyone have any experience or advice?

Yes, could use Shift and convert to Spatie HTML, but how future proof is that? they could get bored with the package and give up just like the boys and girls behind collective. But yet, I accept it is the easier way to go.

OR: Am I panicking over nothing, and Taylor is unlikely to ever break Collective?

0 likes
23 replies
LaryAI's avatar
Level 58

Converting from Laravel Collective to raw HTML or another form helper package can indeed be a daunting task, especially if you have a large codebase. Here are a few suggestions that might help you with the transition:

  1. Manual Conversion: This is the most reliable method but also the most time-consuming. You would need to go through each form and replace the Laravel Collective syntax with raw HTML. To minimize errors, you could do this incrementally, testing each form as you go.

  2. Regular Expressions: You could use regular expressions to find and replace Laravel Collective syntax with raw HTML. This can be done using an IDE like PHPStorm or a command-line tool like sed. However, this approach requires careful crafting of regex patterns and thorough testing to ensure nothing breaks.

  3. Custom Command: Write a custom Artisan command that scans your views for Laravel Collective syntax and attempts to replace it with raw HTML. This would be a more controlled approach than using regex globally and allows you to handle edge cases more gracefully.

  4. Third-Party Packages: While you've mentioned concerns about the longevity of other packages, using a well-maintained package like Spatie's BladeX or another form package might be a good middle ground. These packages are often kept up to date with the latest Laravel versions.

  5. Automated Tools: There might be automated tools or services that can help with the conversion, but their reliability can vary, and they might not cover all edge cases.

Regarding the future-proofing concern, it's true that no package can be guaranteed to be maintained forever. However, Laravel's philosophy tends to be about not breaking packages without a good reason and providing long deprecation periods. That said, it's always a risk to rely on third-party packages.

Here's a simple example of how you might use regex to replace a Laravel Collective Form::text with raw HTML:

// Example regex pattern for converting Form::text to an input field
$pattern = '/Form::text\([\'"](.+?)[\'"],\s*[\'"]?(.*?)[\'"]?,\s*(.*?)\)/';
$replacement = '<input type="text" name="" value="" >';

$content = preg_replace($pattern, $replacement, $viewContent);

Please note that this is a very simplistic example and real-world usage would require a more complex pattern to handle all possible cases.

In conclusion, there is no magical way to convert Laravel Collective forms to raw HTML without some effort. The best approach depends on your specific situation, the complexity of your forms, and your comfort level with various tools and techniques. Regardless of the method you choose, make sure to thoroughly test your application after making changes to ensure that everything still works as expected.

gych's avatar

Laravel Collective doesn't work with Laravel 11 anymore so you will have to convert it to HTML or Spatie HTML when you're planning to upgrade to Laravel 11.

If you want to migrate everything quickly you could indeed use Laravel Shift to convert it to Spatie HTML. Otherwise migrating everyting manually is also an option but like you mentioned it will take a lot more time. Using search and replace will speed it up but a mistake can be quickly made.

vincej's avatar
Level 15

Thanks guys, I'm still on L10, and haven't upgraded. Good thing as I was not aware that L11 already breaks Collective. I had a look at Spatie/HTML and I can see that there are have been 1.5 million downloads so far and it has been around for at least 7 years. Who would have known? :o)

Any opinions on how well it is received, reviewed?

Snapey's avatar

If you want futureproofing, use native html

vincej's avatar
Level 15

I think I will take a single blade view and try converting it over manually to HTML using PHPStorm replace feature. If it goes well I will try a second then a third, and ultimately use 'Replace All' across my app in PHPStorm. if it's a disaster then my fallback will be to go to Spatie – HTML.

vincej's avatar
Level 15

Moreover, it is not a simple case of just replacing Collective syntax with HTML syntax, a person also has to ensure that all the laravel variables are included in the correct place as well. Oh Boy .... fun ... not.

Snapey's avatar

@vincej of course, but for inputs its only name, value, type and class

vincej's avatar
Level 15

@Snapey agreed, however, I had hoped that I could just use the 'replace' feature in Storm across a whole file. I don't think that will work if I have to manually be changing the variables into the correct places. Unless I am mistaken this looks like a painstaking manual job, no?

vincej's avatar
Level 15

@Snapey I just had a look at the spatie syntax and it looks awful. All of a sudden, raw HTML looks better.

vincej's avatar
Level 15

@Tray2 You are right. I was drawn into doing more interesting things.

Now I am facing the wall! :o(

vincej's avatar
Level 15

@Tray2 For clarity, those more interesting things were all JS driven, so Laravel work took a bit of a back seat, so yeah, I lost focus.

Tray2's avatar

@vincej Shit happens, I would not move to spatie myself, don't like the syntax. I would probably create a component for each type of form element, and use that, then it would be a pretty simple task to reuse.

<x-form action="{{ route('books.store') }}" method="POST">
	<x-form-text-input name="name" />
	<x-form-submit />
</x-form>
vincej's avatar
Level 15

@Tray2 Great idea, thanks for that. I''l give it a try. It looks like it will be a manual job. Yeah, I hate the syntax as well.

Since my last post I ran a bash command to count up the number of blade views I have and it is 223. Each letter sized view is full with collective and a few bits of HTML code. Looks like I will be busy for a while :o)

vincej's avatar
vincej
OP
Best Answer
Level 15

I have realised the perfect answer to the problem I own of converting 223 Laravel Collection files.

I use PHPstorm IDE. PHPstorm has available a dedicated AI assistant which I subscribed to already. All I am doing is grabbing those sections of files which contain Collective and sticking them into the AI assistant with the instruction to convert it to raw HTML. Abracadabra, the page done. No need for Laravale components or foR conversion to Spatie HTML.

I hope this helps someone else.

5 likes
SupaMonkey's avatar

@vincej Hi Vince, In the same position as you and was greatly intrigued by your solution! Not being familiar with PHPStorms AI solution though, curious about more details on how you achieved this.

Did you have to, for example, search for every use of 'Form::open' and copy paste it into the AI assistant and manually copy/paste the solution for each laravelcollective line of code back into your project?

Or was it more general like 'search entire project for X [each type of laravellcollective type, or code that starts with Form::open] and replace that line of code with html equivalent?

vincej's avatar
Level 15

@SupaMonkey Hi! I don't know what VS code offers, but you could use Git CoPilot, I am sure that would work. In my page views all the Collective Form::Open, Form::text etc etc was grouped together on the page. I would ignore any native HTML or Laravel includes or sections that might be at the top or bottom. Then I just highlighted the Collective section and copy and pasted it into the AI assistant. Hit the button and Voila. Then copy and paste back into your page. It can be a little slow, but it is WAY faster than doing it manually or using components etc. NB! When I have a very long page with lots to convert, then I will break it up into 2 or 3 sections as my AI will skip repetitive rows for example when you feed it a a large long page. Good luck !

1 like
casc-or's avatar

@vincej Just a big thank you for suggesting this - a huge timesaver!

SupaMonkey's avatar

Thanks, did try it but wasnt happy. Code generated would sometimes be optimal, other times would be less so. Since I had a bunch of LaravelCollective sprinkled in the blade templates in hundreds of files, having to select each 'sprinkle' and then do the manual work was less than ideal.

I've decided to rather use Shift to convert to spatie/laravel-html

GodziLaravel's avatar

@vincej Hello and thanks for your post . I'm actually switching from Laravel 10 to Laravel 11 and I have a problem to install laravelcollective/html I'm wondering what alternative to use and if possible to give me the steps you did to solve this

thanks

vincej's avatar
Level 15

@GodziLaravel HI there, Laravel / collective does not work in L11. So, use HTML, or use Spatie HTML.

I have not yet installed L11, and so, I am converting my collective slowly when I have time.

All I do is I use an AI tool like what is available on Laracasts or I use the one inside my IDE. I simply copy the page view containing all the Collective. I then tell the AI to convert from collective to HTML, I then paste the collective into the AI, and push the button. BINGO. Then copy the result and paste into my IDE. I then check in a browser that everything is good.

Yes, it is slow to do one page at a time, but it is faster than every other method mentioned here. It take me about 30 seconds or LESS to do each page.

If speed is your only interest, then Spatie/HTML is your best bet. However a lot of people do not like the syntax Spatie gives you.

Please or to participate in this conversation.