Laravel 5.8 – From The Ground Up - Laravel 5.8 Tutorial From Scratch – e12 – Eloquent Where Clause

Education, Programming

Laravel 5.8 – From The Ground Up

52 Lessons

Laravel 5.8 Tutorial From Scratch – e12 – Eloquent Where Clause

now that we have a way of adding customers I want to be able to filter by active and inactive customers another popular thing to do with a customer list

in this episode we're going to be refactoring our code to accept an active column that way we can select if a customer is active or inactive and for now we're gonna do that on creation but

as with any project you can always add that later to an edit menu if you wanted to change the status of a customer but we are just gonna keep it simple so here's what I'm thinking I want to add a

third row here and it's gonna be a drop-down menu and then I want to have two lists down here my left list is gonna be my active customers list and my right list is gonna be my in active

customers list so let's start by modifying our view let's go to phpstorm and let's look at our customers view so here we are in our view right before the add customer let's add a new div with a

form group and let's add a select and the name of our select is going to be active we'll keep ideas active active as well and let's add an option options gonna be

empty and we'll just say disabled let's put a label on this select customer status let's add another option it's gonna be a one for active and let's add inactive and we'll give that a value of

zero alright so this needs a class class a form control let's check out the browser there we go so we have a nice drop-down and we can select active or inactive and let's add a label so label

it's for active will say status but right now this doesn't do anything let's start with our migration let's go to they create customers table migration and we need to add a new column here so

it's not gonna be a string let's use an integer and we'll call it active now that we've modified our migration we need to roll back the database and roll it forward again so let's do that now

PHP artisan migrate roll back so that deletes the customers table let's go ahead run PHP artisan migrate so now we have our customers table again with that of course we lost all of our customers

but at this point if we tried to add a new customer it would not save the customer status and that is because we haven't changed our controller let's go to our controller customers controller

and right down here so we are assigning a name and email and we need to assign a status so say active and that will come from our requests which means that we need to make that also required

we'll say active is required all right let's give this a run let's see what happens so our name would be test name our email will be something dot-com and we'll give it a status of active add

customer all right so our customer got added let's add an inactive customer now who said that to inactive add customer right now we are fetching all of our customers so they all get put in this

left list but like I said I actually wanted two columns I want to call them for active customers and I want to call them for inactive customers so let's go back to my customers blade and scroll

down and here's the section where we are echoing out each of our customers so one thing you'll notice is we have this customers variable here in our view and if you recall we are fetching that from

our database right here in the list menu we have this customers variable and we are simply fetching all of our customers but that's no longer going to work because we need to fetch our

inactive customers and we need to fetch our active customers so let's modify our controller to do that now so we'll call them active customers equals customer and the way you do this in eloquent is

we need to add a where clause so our active customers are where the active column is set to one and then get me all of the results does that make sense

so to fetch our active customers we're reaching into our customers table and saying where the active column is set to one give me all the results so let's do the inverse now inactive customers come

from customer where the active column is set to zero and get me all of the results for that just to show you I'm gonna die and dump the active customers list let's go back to the browser hit

refresh so there we are so we have one entry make sense we just have our entry that was the test name entry and notice that the active column is set to one let's die and dump the inactive

customers hit refresh we have one as well and we dive into the attributes we see that the active is equal to zero now we have two variables we have active customers and inactive customers so

let's get rid of this customer all call because that no longer applies and now we need to send both of these variables to our view let's do it in the long form way so active customers equals active

customers and then in active customers we'll set that equal to inactive customers there we go so now we need to modify our view let's change our column to six so that way we can have two

columns equal size so we'll say for each active customers as active customer let me go ahead and modify this as well

active customer so for each of those we'll go ahead and echo out the same we've been doing and let me duplicate this same thing and we're going to modify it from active to inactive so we

have our first iteration here through active customers and then we do the same exact thing through our inactive customers finally let's just add a label here

we'll say h3 active customers and we'll add another h3 here in active customers let's check out what we have and there we are so now we have a nice list of active customers and we have a list of

inactive customers let's add a new active customer here just to prove that it's working we'll say another person with a random email he's an active customer so that gets put in the active

column and let's add an inactive customer inactive add customer and there we go so we are properly adding active customers and inactive customers using that new column that we added to the

database one last thing I want to show you I want to show you a shorthand notation for passing data into views and you will probably end up using this most of the time there is a function called

compact and what compact will do is this exact same operation notice that active customers is referencing an active customers variable and then we have inactive customers and that references

an inactive customers variable so this is very redundant so we can get rid of all of this and we can say compact and all you're gonna pass in is just the name of the variable so active customers

and that's the second argument in active customers much cleaner way so this is doing the exact same thing that we were doing before we hit refresh and so we see that we

the exact same thing let's recap the entire episode so the first thing we did was we went into our view and we added a status column we did that using a select which is just a drop down menu and we

have active which is a value of 1 and inactive which is a value of 0 so then we went to our migration remember the migration describes the database so whenever we're going to do any changes

to our database we need to go ahead and change a migration just as a recap on that migrations only roll forward so if you have some code in production you would have made a new migration which

modified the customers table and added the integer active however we are still in development and during the development process you can change your migrations as many times as you want do

know that when you do and you roll back you're going to lose any test data that is in your database we will look at database seders later on which is a very convenient way of seating test data into

a database so for now you will simply lose your data you'll have to add your data back in just keep that in mind as a side note we added our integer column with a name of active so in our

controller in our store method remember this is the method that we hit whenever we hit this button right here we are targeting this method right here the first thing we did was make active

required that makes sense without having anything for active it doesn't quite work our query would blow up because it requires an active column so after doing that then we are setting our active

column in our customer to be that request active so we return back which means that we go to our list and what I wanted to do was have two columns I wanted to separate my active customers

from my inactive customers that way we can show that we were actually storing that variable so what I did was I split up into two variables now we have this active customers which is fetching into

our customer and saying we're active that Colin that we just talked about equals to one meet all of the results and save them to active customers and then we do the

inverse we say inactive customers equals go into my customers and wherever the active column is set to zero get me all of those results and then we're gonna return our view and we're passing our

data in this new compact way the compact function does the exact same thing we've been doing of assigning active customers equal to active customers as an array and inactive customers equal to inactive

customers the difference is this is just a shorthand notation for doing that so you don't have all of that repetition but great job so far if you've been following along go ahead and make these

changes to your project now

In this episode, we are adding a column for our customers list to make them active and inactive. Follow along as we add a where clause to our eloquent call to make this functionality work.

For the best experience, follow along in our interactive school at https://www.coderstape.com

Resources
Course Source Code
https://github.com/coderstape/laravel-58-from-scratch

Hit us up on Twitter with any questions or comments @codertape (https://twitter.com/CodersTape)

About This Course

Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world’s most popular PHP framework. Let’s get started!

    add to database laravel admin laravel artisan Artisan Command artisan commend create artisan console artisan laravel command authentication in laravel belongsto belongsto laravel example coding tutorials create artisan command create command artisan create new command artisan Create your own crud laravel crud laravel tutorial crud resource laravel crud with file upload custom artisan commands custom artisan commands laravel custom laravel database configuration laravel database relationship digital ocean dotenv eager loading eager loading in laravel eager loading vs lazy loading laravel eloquent eloquent accessors eloquent mutators eloquent orm feature testing laravel fetching data in laravel filesystem laravel form requests full stack vue gate and policy laravel google optimize hasmany hasmany laravel hasmany laravel example hasmany relationship in laravel hasone hasone relationship in laravel How to create how to queue email how to use queue installing laravel intervention image laravel laravel 5 laravel 5 auth laravel 5.4 queue laravel 5.5 queue laravel 5.8 laravel 5.8 artisan command laravel 5.8 artisan console laravel 5.8 auth tutorial laravel 5.8 authentication laravel 5.8 commands laravel 5.8 crud laravel 5.8 crud example laravel 5.8 custom middleware laravel 5.8 deprecations laravel 5.8 eager loading laravel 5.8 elixir laravel 5.8 event listeners laravel 5.8 events laravel 5.8 feature laravel 5.8 features laravel 5.8 global middleware laravel 5.8 install laravel 5.8 lazy loading laravel 5.8 listeners laravel 5.8 middleware laravel 5.8 multi auth laravel 5.8 named resource routes laravel 5.8 named routes laravel 5.8 new feature laravel 5.8 own artisan command laravel 5.8 queue laravel 5.8 queues laravel 5.8 routing laravel 5.8 telescope laravel 5.8 tutorial laravel 5.8 user auth laravel 5.8 user authentication laravel 5.8 what's new laravel 5.9 laravel admin permissions laravel api laravel artisan command laravel artisan console laravel assets laravel auth laravel auth role laravel authentication laravel authorization laravel axios post example laravel background process laravel background task laravel background worker laravel beginner to master laravel belongsto laravel belongsto vs hasone laravel belongstomany laravel best packages laravel best practices laravel best tutorial laravel bootstrap laravel command laravel command line laravel commands tutorial laravel composer install laravel connect db laravel console command laravel console testing laravel contact form send email laravel controllers laravel create laravel crud laravel crud policy laravel custom command laravel custom middleware laravel database laravel database configuration laravel database relationships laravel database seeder laravel database settings laravel db seed laravel db settings laravel debugging laravel deploy aws laravel deploy digital ocean laravel deploy on digitalocean laravel deploy on server laravel deploy to production laravel deployment laravel digitalocean laravel drag and drop file upload laravel dropzone file upload laravel dropzone image upload laravel e-commerce laravel eager loading laravel eager loading tutorial laravel eager loading with condition laravel elixir laravel eloquent laravel eloquent belongsto laravel eloquent crud laravel eloquent hasmany laravel eloquent where laravel eloquest tutorial laravel events and queue laravel events tutorial laravel factory laravel feature test laravel feature testing laravel fetching data from database laravel file storage laravel fillable example laravel flash message notification laravel for beginners laravel form validation laravel forms laravel forms bootstrap snippets laravel from scratch laravel from the ground up laravel front end laravel frontend tutorial laravel gate and policy tutorial laravel global middleware laravel hasmany laravel hasone laravel i18n laravel installation laravel installation mac laravel intervention image tutorial laravel javascript tutorial laravel jobs queue laravel language laravel language switcher laravel language tutorial laravel languages laravel lazy loading laravel listener event laravel listeners laravel localization laravel login laravel mailable tutorial laravel many to many relationship example laravel markdown email laravel markdown mail laravel mass assignment laravel middleware laravel migrate fresh laravel mix laravel mix 5.8 laravel mix vue laravel model factory tutorial laravel multiple language laravel mysql json laravel named resource routes laravel named routes laravel new features laravel news laravel nginx laravel node modules laravel npm install laravel npm run watch error laravel one to one laravel paginate laravel paginate link laravel pagination laravel pagination 5.8 laravel pagination links laravel pagination tutorial laravel phpunit laravel phpunit testing laravel pivot table example laravel policy laravel preview laravel query optimization laravel queue laravel queue event listener laravel react laravel register user laravel registration laravel registration validation laravel relationships laravel reset password laravel role middleware laravel role permission laravel roles laravel route group middleware laravel routes laravel routing laravel routing with parameters laravel sass laravel save file to database laravel scopes laravel seo tutorial laravel server laravel session data laravel ssl laravel supervisor laravel tailwind css laravel tailwind setup laravel tdd laravel tdd tutorial laravel telescope laravel telescope installation laravel telescope tutorial laravel test driven development laravel test workflow laravel testing laravel testing controllers laravel testing tutorial laravel tools laravel translation laravel tutorial laravel tutorial for beginners laravel ubuntu server laravel ubuntu tutorial laravel unit testing controllers laravel upload file laravel upload image to storage laravel upload multiple files at once laravel upload multiple image to database laravel upload multiple images laravel url slug laravel vue laravel vue js crud laravel vue setup laravel vue tutorial laravel vue.js laravel webpack error laravel webpack tutorial lazy loading lazy loading laravel lazy loading vs eager loading learn laravel learn laravel framework step by step localization in laravel localization laravel many to many laravel mvc tutorial for beginners in php mvc tutorial laravel mysql mysql relational database mysql relationship n + 1 problem n + 1 problem laravel new artisan command npm install npm run dev npm run watch laravel one to many one to one one to one laravel own artisan command pagination pagination bootstrap pagination bootstrap php mysql pagination laravel pagination laravel bootstrap pagination links pagination php php php artisan php carbon immutable php framework php framework 2019 php what's new 2019 phpunit phpunit laravel phpunit testing pivot laravel policy laravel polymorphic relationships queue and events queue email queue for laravel queue jobs queue laravel 5.8 registration form relationship laravel eloquent relationship mysql restful controller role permission in laravel roles laravel route model binding laravel routing in laravel save image to database seo friendly seo friendly content writing seo friendly website seo laravel seo optimization simple pagination laravel slugify sqlite relational database sqlite relationships between tables symfony command console symfony laravel tailwind css tailwind laravel mix telescope laravel tutorial laravel unit testing unit testing laravel upload file laravel upload file php upload image laravel upload image to database php upload images laravel upload multiple files in php url slug laravel url slug php vue vue.js vuejs laravel why use laravel queue why use queue