Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Products Create: Select2 and CKEditor

Premium
11 min read

In this last lesson for the products, we will create a Livewire component to create and edit products. In this form, we will use Select2 for dropdowns and CKEditor for textarea.

finished form

Before starting, let's add Select2 to our app...

The Full Lesson is Only for Premium Members

Want to access all of our courses? (29 h 14 min)

You also get:

54 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Comments & Discussion

RB
Rajesh Budhathoki ✓ Link copied!

Image is missing after following phrase:

After visiting create or edit product page, you should see similar view to below

PK
Povilas Korop ✓ Link copied!

Thanks for reporting! Fixed now.

A
AlbertLens ✓ Link copied!

In Http\Livewire\ProductForm.php protected function initListsForFields(): void { $this->listsForFields['countries'] = Country::pluck('name', 'id')->toArray();

    // THIS IS EXPLAINED BUT IT FAILS IN MY CASE:
			// $this->listsForFields['categories'] =** Category::active()**->pluck('name', 'id')->toArray();
    
			// CHANGED FOR the following and it now works
			$this->listsForFields['categories'] = **Category::where('is_active', true)**->pluck('name', 'id')->toArray();
} 
PK
Povilas Korop ✓ Link copied!

Sorry, as Emil pointed in another comment, I forgot to add scopeActive() to the tutorial, will fix it now. But you've done the same thing, just without the scope, all good.

EE
Emil Enevoldsen ✓ Link copied!

You are using the scope Active() on Model Category, but the scope is not made anywhere in the tutorial. Inside App/Models/Category add

public function scopeActive($query)
{
    $query->where('is_active', 1);
}
PK
Povilas Korop ✓ Link copied!

Thanks for flagging, Albert in another comment also had this issue. Will add the info about the scope now.

EE
Emil Enevoldsen ✓ Link copied!

@Povilas, in the code where validation is added, the eloquent is without the scope again. Guess that's why it didnt fail when testing the repo. Love this tutorial, was working on an order-system with services, so this was spot on, right when I needed it!

PK
Povilas Korop ✓ Link copied!

Great to hear that it helped you!

A
AlexSteele ✓ Link copied!

i get NULL when I place : dd($this->initListsForFields()); into the mount

shouldn't i have a full list of categories and countries at this point? The reason I am trying to see what is happening right there (and) the main issue I am having is that I get this error:

trim(): Argument #1 ($string) must be of type string, array given its an error on the select2/blade.php file, line 6 - its the attributes

A
AlexSteele ✓ Link copied!

i decided to back up the code i wrote along with this, and then test. i am getting the same exact error from the code in the repository, so i took a screenshot for clarity. Then realized cant attach the screenshot, so I will copy and paste... I don't understand the cause of the error, there is an array here, but it must be an array, because it was specified to be from the livewire component, right?

TypeError PHP 8.1.7 9.51.0 trim(): Argument #1 ($string) must be of type string, array given

the only thing i see that raises an eyebrow is this:

SELECT name, id FROM categories WHERE is_active = ?

PK
Povilas Korop ✓ Link copied!

Hard to answer without debugging. Could you put your current code on GitHub, and invite me (username PovilasKorop) and I will try to find time to debug it for you next week.

A
AlexSteele ✓ Link copied!

The code that fails - it is an exact duplicate of what is in the repository. i downloaded the respository Wednesday evening, and replaced my code with that downloaded version last evening when I had tried for several hours to solve and could not. I am running on a linux server, so all my commands are to run build and not dev. i mean literal file replacement, not copy and paste. so there could be no differences, no errors on my part.

PK
Povilas Korop ✓ Link copied!

Not sure I understand the part of "replaced my code with that downloaded version", are you sure that nothing could be broken there?

So, anyway, how can I reproduce it to debug? You're saying I should download the repository and there will be an error already?

Otherwise, I can't really debug the situation or guess what you did wrong. Or maybe indeed the bug is somewhere in the repository, please help me to identify it.

A
AlexSteele ✓ Link copied!

Thank you for responding! I was in deadlines all day yesterday because of internet outages here I couldn't get any work done and now I am behind schedule for work. as soon as possible I will respond to this with a better explanation. thank you again!

A
AlexSteele ✓ Link copied!

I finally was able to get internet back up. and I pulled the entire repository and replaced your code with mine - some things must have been different. ** I am happy to report that the issue was not in your code. ** I will go line by line through the code you have vs the code I wrote along with the training - until I spot the issue. Thank you for responding and trying to help. I am very excited to discover where I made the error, because I know it was definitely in my code!

A
AlexSteele ✓ Link copied!

not sure exactly why this error is gone, all the code matches perfectly to what I had created on my own. two things happened today, I did an npm install, and then a ran npm run build. this was not the first time, I did that many times when it was failing last week, - but i beleve that the npm install command I ran today must have fixed it.

L
Lackson ✓ Link copied!

Create product page display this error see the link Error. Help please

PK
Povilas Korop ✓ Link copied!

Was unable to reproduce this error. Could you push your code to GitHub, invite me (username PovilasKorop) and then I may try to debug next week.

L
Lackson ✓ Link copied!

Thanks. I will again try to debug and in case of any failure, I will do what you recommended.

L
Lackson ✓ Link copied!

I have already invited you. db:seed (all users use same password which is password). Under Product page, click Create Product to find the error I have been facing (trim(): Argument #1 ($string) must be of type string, array given).

PK
Povilas Korop ✓ Link copied!

Thanks, I will try to debug with the team in upcoming days.

N
Nerijus ✓ Link copied!

Adding @props(['options']) to components blade seems to fix this problem, at least for me. Please try.

M
muuucho ✓ Link copied!

Thanks, @Nerijus, that helped me out. I think that the error occours for us who are using PHP 8.

J
johnogle ✓ Link copied!

Great tutorial, loving it. One thing I noted was the first letter lowercase of the component model from this artisan command:

php artisan make:component select2

In my IDE setup (PHPStorm's terminal on Mac Os Ventura) it is case sensitive, so I had to manually change the first letter to uppercase:

"Select2.php" inside of app->View-->Components->Select2.php

not sure of relevancy, but I do note that it is uppercase in the final source code repository.

IC
Ikaro Campos Laborda ✓ Link copied!

In my case (using livewire 3) I had to use an array to store the values on the bindings and only when saving, create a new model instance with fill(), but the select2 component is not working.

M
muuucho ✓ Link copied!

I like to have a flash message when a product is created or updated. Part solution: Install jantinnerezo/livewire-alert and then you can redirect like this:

 $this->flash('success', 'Good job', [], '/products');

This works when you create a new product, but not when you update one. Why is that?

M
muuucho ✓ Link copied!

Part solution: Install jantinnerezo/livewire-alert and then you can redirect like this:

 $this->flash('success', 'Good job', [], '/products');

This works when you create a new product, but not when you update one.

N
Nerijus ✓ Link copied!

Read the docs https://livewire.laravel.com/docs/redirecting#flash-messages Also, instead of @if (session('error')) you should be able to use @session('error')

M
McNab ✓ Link copied!

The final instruction on the save() method in ProductForm is missing the sync and redirect which is in the repo version


$this->product->categories()->sync($this->categories);

$this->redirect(route('products.index'));

A
augusto-dmh ✓ Link copied!

I've done everything it was mentioned in this lesson apparently and the solution works, but when i try to get out of the edit form, go to somewhere else and come back, this error below happens:

Uncaught CKEditorError: ckeditor-duplicated-modules
Read more: https://ckeditor.com/docs/ckeditor5/latest/framework/guides/support/error-codes.html#error-ckeditor-duplicated-modules
    at Object.<anonymous> (version.js:151:8)
    at Object.<anonymous> (ckeditor.js:5:9192)
    at i (bootstrap:19:22)
    at Module.<anonymous> (ckeditor.js:5:122298)
    at i (bootstrap:19:22)
    at bootstrap:83:10
    at ckeditor.js:5:4841
    at universalModuleDefinition:9:27
    at universalModuleDefinition:1:1

The link for my repository with my version of this Povilla's course: https://github.com/augusto-dmh/order-management-livewire-3

A
Alin ✓ Link copied!

You use Livewire.hook('message.processed', (message, component) => { initSelect() }); However, it seems this is not a livewire 3 javascript hook, but a livewire 2. https://laravel-livewire.com/docs/2.x/lifecycle-hooks#js-hooks For me, for the version of livewire i am using, none of these hooks are being triggered. From the code posted in this tutorial, only livewire:init is being triggered.

  1. My question is: am i missing something?
  2. Do you have a specific tutorial on livewire(3, hopefully) javascript lifecycle hooks?
N
Nerijus ✓ Link copied!

Looking at the upgrade guide you might be right https://livewire.laravel.com/docs/3.x/upgrading#new-lifecycle-hooks About all the hooks simply check docs https://livewire.laravel.com/docs/3.x/lifecycle-hooks

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.