From the course: Building RESTful APIs in Laravel

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Saving a new petition

Saving a new petition

From the course: Building RESTful APIs in Laravel

Start my 1-month free trial

Saving a new petition

- [Instructor] Let's create a new petition. In our PetitionControl.php file, we need to work inside a store method. To create a new petition, we create a new petition variable, use the create method on the petition model, and pass in the fields that we want from the request object. In our case, we want to pass in the title, description, category, author, and signees. Next, let's reset a new petition resource and pass into petition variable. To stop PHP storm complaining, let's also change the return value from response to petition. However, before we can actually save a new petition in the database, there is one more data protection action we have to do. All Laravel Eloquent models are protected against the mass assignment vulnerabilities by default. And by using the create methods, we are sending an array of data to our database. This array could potentially include some malicious content which is why we need to…

Contents