Laravel From Scratch [Part 7] - Forms & Saving Data
ฝัง
- เผยแพร่เมื่อ 8 ก.พ. 2025
- In this video we will be creating a form using Laravel Collective to submit blog posts to the database.
CODE: Complete Code For This Series
github.com/bra...
10 PROJECT LARAVEL COURSE: Please use affiliate link below
www.eduonix.co...
50% OFF: Use special code "traversy"
SUPPORT: We spend massive amounts of time creating these free videos, please donate to show your support:
www.paypal.me/t...
/ traversymedia
FOLLOW TRAVERSY MEDIA:
/ traversymedia
/ traversymedia
/ bradtraversy
I love the fact that he makes silly mistakes sometimes. It just proves to me that even the most experienced can make mistakes and it doesn't mean anything else as long as we can figure it out. And the way he handles it makes me laugh. xD "WTH did I do? " Hats off to you man!
@L K LOL, u must be dr. evil
I've studied laravel a few months ago via laracasts and i barely made it through. But I'm really glad I tried learning that before this because now I know to do things long-handedly and following this tutorial is a piece of cake for me. I love how consistently simple laravel can be made. Thanks for this tutorial, it will take me a few weeks probably to master everything as you do but thanks! really!
So true. Brad Traversy's youtubes and Jeffrey Way's Laracasts are both helpful. They very different in style. I watch both.
If you get an "InvalidArgumentException Action PostsController@store not defined" in 6:13, use App\Http\Controllers\PostsController@store instead.
thanks man!
This worked. Thanks
thx you this is working
Had this issue and not a single answer was close from what i could find on google. This worked though
This is the best course I have sen on the web, thank you! I went through the whole course from Part 1.
In this part (7) I think that getting third party software to create a HTML form is a bit of overkill.
From my experience a HTML Form is one of the easiest tasks for a web developer.
For all you trying to get CKEditor to work with Laravel 6 - just use
">
CKEDITOR.replace( 'article-ckeditor' );
in the app.blade template and move on. :-)
Thnx! That did the trick perfectly!
The best solution! Thanks
Thanks man. I almost gave up
got a weird issue when trying to install via composer ... loads of errors. How did you resolve that ?
@@d-one-and-only are you a saint or what...thanks man.
Make sure that you are writing this line right:
{!! Form::open(["action" => "PostsController@store", "method" => "POST", "class" => "form"]) !!}
The class is optional of course, but all of the key-values inside of the same set of brackets. I had some sad issues because i had it wrong. But it is working now! : D
Thank you so much for this I was stuck for long time.
I am happy that you were able to solve it!
i have been trying to add a new data to my post an i see this error;
"SQLSTATE[HY000]: General error: 1364 Field 'body' doesn't have a default value (SQL: insert into `posts` (`title`, `updated_at`, `created_at`) values (This is post three, 2018-07-17 07:31:59, 2018-07-17 07:31:59)) "
pls i need help
Hello Patrick!
I am sorry, i did not continue following to this series. However, does Traversy provide the full code of the course? You could compare your code with his.
why i can't submitting?
I can't stop learning from you , and also can't thank you enough ! Good series for Laravel ,maybe the bests!
thanks a lot Brad! Thought learning laravel would be really difficult. your videos really helped a lot. :)
No problem, glad you liked them
Traversy Media 🙌
Thanks a lot. This is the kind of tutorial that makes me wanted to learn Laravel, clear and concise. You turned it something that's really interesting, making me want to learn more. Apart from extended project courses on Eduonix, I pray that you create more Laravel videos that noobs like me can learn more and stick with Laravel. More power to you!
Hi ! for those who have problem with the editor not showing install laravel-filemanager (the link is on the bottom of the page where you got the editor, follow the installation instructions.) After you installed that run
php artisan vendor:publish --tag=ckeditor one more time . That fixed it for me .
thanks a lot!
I did what u said but still editor is not showing
tldr; make sure you saved /config/app.php before running php artisan vendor:publish --tag=ckeditor
Thanks for the suggestion, but may I offer a different solution.
For me, after running php artisan vendor:publish --tag=ckeditor, the terminal output was only "Publishing complete". It didn't output any of the copied files. I checked the path /vendor/unisharp/laravel-ckeditor/ and did find the directory full of files including the ckeditor.js file, but the browser's server response was a 404 not found for that file.
I may have not saved the /config/app.php file straight away before running the artisan command, because I tried the php artisan vendor:publish --tag=ckeditor again, and this time it did return that all the files had been copied, just like in Brad's terminal output.
Thanks!
if you still have the issue, check in developer tools> network if the ckedit.js is loaded. If not, try this in your layout
CKEDITOR.replace( 'article-ckeditor' );
To move Create Post button to the right, use class="nav navbar-nav ml-auto".
Thank You!
It makes no sense to use navbar classes for buttons, especially because you will lose all the styles. Just add the float-right class:
"class" => "btn btn-primary float-right"
Using Laravel 6:
I could not get Unisharp Laravel-CKeditor to work at all
Apparently, I'm not the only one. I've been googling for 3 hours and keep finding a lot of "Unisharp CKEditor not working with Laravel 6"
Every time I tried to issue command: "composer require ..."
I got this error: "Installation request for laravel/framework (locked at v6.1.0, required as ^6.0)"
I tried to change "laravel/framework" from "^6.0" to "6.1.0"
that didn't seem to work.
Maybe I didn't do something right
But I was able to just use the CDN directly from the CKEditor website. (Using CKEditor Version 5 is a little different than 4 and earlier)
However, I would recommend using TinyMCE CDN instead.
Just add this inside the tag (in layouts/app.blade.php)
tinymce.init({
selector: '#editor'
});
Where #editor is the name of your
NOTE: You'll need to setup an account and generate an API Key on their website (takes about 20 seconds). This will unlock more features for your editor. Works in localhost and yourwebsite.com
Anyway, hope that helps someone
Thanks for the recommendation, I also have a problem with CKeditor that was presented on the video.
1. npm install --save @ckeditor/ckeditor5-build-classic
2. copy this file node_modules\@ckeditor\ckeditor5-build-classic\build\ckeditor.js
3. paste it to public\js
4. at app.blade.php, put inside head tag
5. put before closing your body tag
ClassicEditor
.create( document.querySelector( '#editor' ) )
.catch( error => {
console.error( error );
} );
source: ckeditor.com/docs/ckeditor5/latest/builds/guides/integration/installation.html
@@emisamad302 I followed your instructions, but noting happend after that. What are the next steps?
@@teddysotirov933
just add these two lines to your head tag:
tinymce.init({selector:'textarea'});
You are an amazing human being. we save hours and hours because of people like you. hats off! (laravel 2020)
My simple solution for those who have problem with the editor not showing:
CKEDITOR.replace( 'article-ckeditor' );
Change src to {{asset('/vendor/unisharp/laravel-ckeditor/ckeditor.js')}}.
Be sure that your files were copied like in Brad's terminal
it's works thanks bro
Worked for me too, thanks
@@bryanloh8617 hi, i have trouble with this too, I was wondering how did you solved this!
so how did you solve this!
Thanks for help!
I love how you keep your mistakes in! Great work by the way! :)
Brad you rock! Being a Symfony 3/4 developer I find Laravel is easier to use and has less steps then Symfony. I am excited to be learning Laravel!
laravelCollective is removed. So you can create the simple form
@csrf
Title
Body
Submit
Laravel Collective is not removed they're just remaking their website. You can find their documentation over here github.com/LaravelCollective/docs/blob/5.6/html.md
Thanks Zunaira !
I don't understand why would I want to use this library if we can simply use html and avoid overprocessing. What is the matter with learning html basics¿
@@nac9880 The same reason we use Laravel and over process vendors when we can _simply_ use plain php.. Laziness, my little summer child.
Thanks baby.. I come from Indonesia, I am very happy to be able to share knowledge with all of you
Greetings from Panama bro! I'm trying to be a selflearner and I love your videos. I really like how you explain were things come from! kudos to you
The best series I have ever come across. And it's totally free!!!
Laravel is terrific. I have old JavaScript routines I wrote to do validation, pagination and present error messages that I can get rid of now. Thanks for teaching us about Laravel.
UPDATE: if you use 'composer require laravelcollective/html' in terminal, you don't have to add those lines in config/app.php (providers & aliases)
Thank you very much! It helps a lot
if your getting not defined error
add Route::get('/create', [PostsController::class, 'create']);
to web.php file.
and try both App\Http\Controllers\PostsController@store or PostsController@store by itself
Mine worked on PostsController@store without the full path.
Using Laravel version 8 +
you can check the store route exists by using php artisan route:list, and if it does, I changed the action in the form open to be: 'action' => 'App\Http\Controllers\PostsController@store'
ok guys, if you get stuck with this error: "Action App\Http\Controllers\PostsController@store not defined. (View: C:\xampp\htdocs\laravel\HAR
esources\views\posts\create.blade.php)" you have to check this line: {!! Form::open(['action' => 'PostsController@store', 'method' => 'POST']) !!}. The function name changed in someplace between Brad's video and 2019.
And what is it that changed? Did you find the solution?
@@istandaloneroronoazoro5188 {!! Form::open(['action' => 'App\Http\Controllers\PostsController@store','method'=>'POST']) !!}
This series is brilliant Brad, everything is so clear, probably one of your best! Thanks!
AWSOME STUFF!!! Thanks, Brad!!!! You make learning Laravel easy and enjoyable!
One of the best series on the laravel.Thank you so much for creating such a wonderful series...
hey can you help me please how dashboard is created?
Just download ckeditor from website
ckeditor.com/ckeditor-4/download/?null-addons=
then put these files in ckeditor named folder and put it in public folder
in app.blade.php your main template
in header put the js link
and below before the end of body tag add
$(document).ready(function () {
$('.ckeditor').ckeditor();
});
From the script it could be seen that any where u want to use ckeditor apply class=“ckeditor” there
As I put in my text area
{{Form::textarea('body','', ['class' => 'ckeditor form-control', 'placeholder' => 'Body Text'])}}
But you will see that it’s not parsing the html code
blockquote> This is post Seven lets see if I can bold it.
Now in show.blade.php
{{$post->body}}
You have to change its syntax to let it parse html
{!! $post->body !!}
And yup it works if you want to show parsed in list view too then also add this setting to it in index.blade.php in post folder
Thank you
thank you :D
Thanks a lot
for y'all using laravel 6, and were having issue where install unisharp/laravel-ckeditor, you can use original ckeditor instead,
place this on the head tag after css :
then place this on end of body tag :
ClassicEditor
.create(document.querySelector('#editor'))
.catch(error => {
console.error(error);
});
then add 'id' => 'editor' to form::textarea, will look like this :
{{ Form::textarea('body','', [ 'id' => 'editor', 'class' => 'form-control', 'placeholder' => 'Body Text'] ) }}
which css file exactly? and body tag?
@@AmandaOwinya in the file app.blade.php inside the head after the link tag
The CK editor is the bomb.
Security-related comment:
For all the comments saying to prevent users from using the tag and that laravel is so badass that can handle all security by itself, I want to note one thing.
This is vulnerable and should not be taken lightly:
{!! $data !!}
*Example:
data =
This will trigger an alert...
Just make sure to create apps with care and do not assume that attacks won't work...
Laravel is doing a really good job but its your responsibility to use it wisely and make a secure site.
Never trust user input!!!!
Maybe a bit late but, I wanted to thank you for this great tutorial and I wanted to ask if there will be a new series or a course for laravel?
Thank you for making these!
It might be helpful to add a video annotation about sanitation though, not just validation.
Just finished the series and came back to this video for the comment, haha. Thank you again!
Hi, first of all I would like to say that I really like your tutorials as I am learning a lot here so thank you for sharing them! :D Now I would need a little help... I have installed Laravel 5.6 and since there is no html form package available for it I am using a regular html and it goes like this:
Create Post
Title
Body
Submit
That shows the form on my browser (/posts.create). In PostsController.php I have:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
return 123;
}
When i click a Submit button it does not stop me if my form is empty and it also does not show '123' when the form is not empty. Instead of that it takes me to a page: isapp.test/posts/PostsController@store and it shows "Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException" error. I don't understand what I did wrong?
I have found the solution. If you are using a regular html (without a form helper) you should make your form like that:
{{ csrf_field() }}
Title
Body
Submit
Than it should work fine :)
Does it worked>.....????
Yup it does work! But after that i had problems with the update part and decided to go back to using the form helper and follow along this tutorial
@joseph did you add {{ method_field('PUT') }} inside the form after csrf_field? PUT method is required if you are updating.
thank you!!
love the tutorials, but i am having serious problems with downloading the laravelcollective/html package maybe because i am using a newer version(laravel 11) and any solution here will help alot
According to the Laravel Collective web site, they are re-evaluating support for their components. I don't know when they'll return or what the component will look like. Check the project's Github page for documentation. In the meantime, I think we have three choices:
1) Continue using Laravel Collective's component but be ready for possible changes after their relaunch,
2) Find an alternative component like Laravel Form Builder, or
3) Use good old HTML.
Although using HTML will require more work (using {{!! csrf_field() !!}}, marking selected values in dropdowns, etc.) but you'll get more stability. I'm choosing HTML to avoid these types of disruptions in the future.
BTW you can use middle mouse button to close tabs!
that's awesome trick !
This is what I was looking for, excellent Tutorial.
Finally managed to get passed by first 3-4 videos and I definitely love this series. Cmon, seriously, is that easy?
This is really helpful. Thank you for this free video Sir! Salute to you
For anyone having problem with ckeditor try this:
Use the direct CDN & set the text-area id to editor
CKEDITOR.replace( 'editor' );
Thanks guy Brad, I really proud of you for your videos to learning laravel tutorial.
LaravelCollective Ckeditor was not showing, then i changed
TO
I deleted 'defer' and it works now,
this is for those who struggle like me to fix the same issue... :)
my solution for ckeditor not showing is Changing to .
Hoe it help, happy coding =)
it worked, thanks!
Thanks !!!
Thank you. :D
Thank you
Not all heroes wear capes. you deserve a beer. Thanks a lot
In Laravel 8 (and possibly some of the others like 7, not sure) use the following for validation instead at 8:00 mark.
$validated = $request->validate([
'title' => 'required',
'body' => 'required'
]);
Both methods seem to work for me on Laravel 8. Do you know what the difference is between them?
5 more to go !!
Hey Dev, just want to ask, how rewarding is Laravel, how comfortable are you with it after a year?
@@iffipifi1 same question to you
@@Luk3Stein 😂
same question to you
@@simonatacha6129 haha its only been a month for me but loving the framework so far. Easy to pick up, great documentation and flexible. Right now making a blog in it.
@@Luk3Stein a little help please!!!!
{{Form::label('summary-ckeditor','Body')}}
CKEDITOR.replace( 'summary-ckeditor' );
in body we are suppose to use 'id' but thats not working for me so i did this,
can you help me to do this as shown in the video
Superb!!! Planning to take your course after I am done with this.
If someone experienced not aligning second ul, use "justify-content-between" on div that encompasses them both.
Really Nice series.
Enjoying it. Would like more projects with new features.
Hi Brad.
Question:
Why don't you make another @section('scripts') to place the scripts used only in some pages?
Probably performance related.
Another great tutorial. So straightforward. Very helpful.
Excelents videos for learning Laravel 101
I'm in love with Traversy Media, from Bangladesh
If somebody still have problem with action in form try this (laravel version 5.7.*):
----------> Rest of the form is just basic html as shown on video
Sorry. I futzed around trying to get the ckeditor to work, variously trying suggestions found in the comments. I will instead have to use a different editor. I feel I have invested too much time in this series to abandon it. Plus, this is what happens in the course of professional development when you hit a roadblock, you find a solution that works and then carry on. Hopefully I will be able to continue with the next part of the series by tomorrow.
2:30 That's already included in modern Laravel, though.
Man I like your videos. Has made learning Laravel so easy. Big up
This is a great tutorial. Very easy to follow.
Am gonna monetize this skill you offered for free, in return for your it i will see to it i reward your kindness but before that may you be blessed. You are a hero...👊🏾👊🏾👊🏾
Amazing so far so Good Thank you Brad. I just got these errors when trying to add the [CKEDITOR Installation failed, reverting ./composer.json and ./composer.lock to their original content]
Problem 1
- Root composer.json requires unisharp/laravel-ckeditor ^4.7 -> satisfiable by unisharp/laravel-ckeditor[4.7.2].
- unisharp/laravel-ckeditor 4.7.2 requires illuminate/support ~5.0 -> found illuminate/support[v5.0.0, ..., 5.8.x-dev] but these were not loaded, likely because it conflicts with another require.
NEED HELP PLEASE??
Thank you! I'm watching 2nd time at least every episode now :). And this time I'm coding as well. So, there are a few bits and bobs after 1.5 years which I had to overcome, but it was worth a while. And only I had to quit Xampp, too many problems with it, I've switched to Laragon and no problems since then.
Part 7 with CK Editor is outdated by the way. In Laravel 6.12 starting, you can't install it the way it's shown.
If you go to "Source" and type alert("test") and submit it right away, it will work. This is a major security issue.
best laravel tutorial ever. thanks man.
Your videos are really great . It really helps me lot
Thank you, sir,
you are the best teacher
Very useful series bro. Thanks for your effort!
Never forget to prevent users from using the tag!
Tobias Wimmer laravel does it for you
does Laravel 7 got new "Forms" Code or is it okay to use the old Collective methods? and besides, thanks to this great series. i enjoy your stuff since a year now which lead to a better knowledge and doing an apprentice ship in computer science (application development) at the age of 38 :D
Migrating from MERN stack, this feels like heaven
I think it's better to refer to "/posts/create" with "{{ route('posts.create') }}" instead.. so you won't have to edit all the links to the route named 'posts.create' if you decided to change the route uri, as well as referring to "/posts/" with "{{ route('posts.index') }}" for the same reason
Hi Brad and thanks for this awesome tutorial amongst all of your other ones!
I'm still learning web programming and have actually managed to setup an almost complete addon-function in form of a simple task manager (with controller, model, views etc).
However, got a hopefully quick question:
If I have this list of tasks, how can I best update a single row if i.e click on my checkmark (glyphicon) without return/render full view?
I'm guessing this should be quite simple, just not familiar with it yet :)
Thanks!
Is it safe generally to use {!! !!} ? Because its possible to inject also for instance, but I am not sure if with form validation this kind of tags are escaped ?
hi
why ckEditor doesn't work?
anybody has same problem?
because u need to copy the resources first
php artisan vendor:publish --tag=ckeditor
@@daniils.6868 Done that but still, it works not.
@Mark M Thank you! You are a life saver!
You are BEST teacher!!!
Nice tutorial as always.. but I have a question,
Why do you use this type of forms instead of direct bootstrap?
I think we are more familiar with bootstrap compared to this type of forms
They are trying to use laravel features
Loving this tutorial. But I'm kinda bummed at this part. I don't want to learn antiquated stuff, I wanna keep it fresh. So I wish you used the current way. Otherwise great jorb!
To be fair, the current method is so close to the old LaravelCollective way, that I carried on with the method he taught here, but also had the new Laravel Forms & HTML documentation open at the same time. A lot of it is interchangeable so I would really recommend it.
laravel.com/docs/4.2/html#opening-a-form
Why the hell would I ever consider writing "{{!! Form::open(array('url' => 'foo/bar', 'method' => 'put')) !!}}" instead of "" ? And then "{{!! Form::close() !!}}" instead of just "" ? Makes no sense for me.
The form builder to look for with Laravel 5.x seems to be github.com/kristijanhusak/laravel-form-builder
Yeah, I do like the series so much. Ty Man!
Thanks for the tutorial,
I would like to know why I should use this form syntax instead of the html form syntax ?
This series is so good
Very informative..Helped a lot.
I have a confusion of 18:10 to 18:50, why tag was not showing correctly on html page and why does it needed to change on laravel code while it will parse by html what does {!! !!} Do ?
Hello guys, for those who is facing problems with ckeditor I´m using laravel 7.x and for me I just need to add CDN or download (choose the more confortable to you) add the script on your base blade file and you are set! u just need to apply the css class on your textarea
if you get memory error when installing laravelcollective, just add COMPOSER_MEMORY_LIMIT=-1 before the command like this:
COMPOSER_MEMORY_LIMIT=-1 composer require laravelcollective/html
Very nice, Thanks Brad, great tutorial.
Error :
Action App\Http\Controllers\PostsController@store not defined. (View: C:\wamp64\www\blog
esources\views\posts\create.blade.php)
I tried everything listed here to get the 'php artisan vendor:publish --tag=ckeditor' to publish my files. It would always respond with 'Publishing complete.' The way I got it to work was by running these two commands: 'php artisan config:clear' and 'php artisan cache:clear'. Then run the vendor: publish command again. It should return the copied files at the command line and the WYSIWYG editor should now display.
thanks i didnt knew it had been the problem i tried replacing it with asset and everything but only this worked!
Hello Traversy, Do you use Laravelcollective form in real project or pure laravel form?
Hello Brad, I got a problem with my laravel ckeditor installation on Laravel version 7.30.4. Could you please tell me how to install this via composer since 5.4 has no issues but the 5.5 and above sure does make a lot of issues especially with the ckeditor
Same
you're literally amazing
if some has error after composer require "laravelcollective/html":"^5.4.0"
[UnexpectedValueException]
Could not parse version constraint :5.4.*: Invalid version string ":5.4.*"
try
composer require "laravelcollective/html ^5.4.0"
Installation failed, reverting ./composer.json to its original content.
iam getting this in the terminal when iam trying to install it
Just wanted to ask why dont you use
!empty($var), and use count()>0.
Are there any down side to using the !empty? Thanks
We Could also Use
if($var)
I been having issues with the messages IF statement, Laravel was trowing an error on since it was trying to convert $error as an int.
If you are having this same issue, change if statement for the following code:
@if ($errors->any())
@foreach ($errors->all() as $error)
{{ $error }}
@endforeach
@endif
tnx
{!! Form::open(['action' => 'App\Http\Controllers\PostsController@store', 'method' => 'POST']) !!}
to fix the error on the create page
Hey Brad. Thanks sooo much for the videos. I'm a newbie and these are excellent! Quick question: I installed ckeditor as you showed, but html tags are now appearing in the post. Is this related to an Apache setting (conf file ???)? Any help would be appreciated. Thanks again!
If you haven't figured it out. He goes over that {{"some_call_here"}} doesn't parse html. Change it to {!!"some_call_here"!!}
few more to go, man your'e the best!
Just wondering if anyone can help, i'm using Laravel 7+. The form worked fine before but once i add the text editor the controller only recieves Null from that field. Any ideas?
Thanks Brad, I love this video and you are doing an amazing job, could you please make a video of file uploading with fineUploader and Laravel? Thanks in advance.
can you please advise me why my layout is in plain HTML text when I enter posts/create where the other pages displays the correct layout format ? I do have @extends('layout.app') and @section('content') @endsection in my create.blade.php file , the navbar is also in plain HTML text
Thank you! and just for reference you guys be careful of XSS with that cool editor :)
Is there a security risk to enabling HTML parsing in the Show template?