تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Unapproved Links Default0:45

that you want to approve these manually until the user becomes a trusted user. And maybe that happens after they've contributed three articles that are valid and appropriate. So from then on, they can submit anything they want. Makes sense? Okay, so that means the first step is, all community links are not approved by default. Let's do this. Let's go back to create community_links table. And you'll remember that we did add an approved column. And if we were to boot up php artisan tinker and view these, community_link::all(), right now, none of them are approved. This is how we set it up. So for example, if we go back to the CommunityLink model, and we go to contribute, here's where we accept an array of attributes, we fill up the model, and we persist it. But don't forget that even though we're not passing an approved column, it's defaulting to zero. So every new record is instantly not approved. So that means, well, we shouldn't be seeing any of these.

Filter Index by Approved1:36

forget that even though we're not passing an approved column, it's defaulting to zero. So every new record is instantly not approved. So that means, well, we shouldn't be seeing any of these here. None of these are approved, so they should not be displayed on the page. All right, it sounds like we need to update our database query. Right up here in the index method, like I said, we're going to have to work on this query quite a bit over the next couple lessons. But to start, we could say communityLink, and why don't we add a new query scope called approved. Or this is basically the same as this, where approved is one. And in fact, maybe keep it like this for now. Okay, so if we come back and refresh, we should see none, which is exactly what we want. Now what you could do here, if you want, we could go to our index view, and then up here maybe say something like, if we have any links, then filter through them. Otherwise, maybe we'll display a list

Now what you could do here, if you want, we could go to our index view, and then up here maybe say something like, if we have any links, then filter through them. Otherwise, maybe we'll display a list item, we'll give it the same class, even though I don't think we're using that CSS class yet. And we'll say no contributions yet. Okay, so indent this, and if we come back, there we go. Better. So that means, yeah, if we, let's bring up php artisan tinker again, and we'll say $communityLink, and we'll just say, give me the first one. And we'll save that to $link. Okay, so why don't we update the approved status to one. $link->approved equals one, or true, and we'll persist it. And now, we should see exactly one record, and we know it's working. Okay, so that's good. But how can we, how can we do this? How can we set it up so that when you contribute a link, if you are a trusted user, the approved status is automatically updated to

Add Trusted User Flag3:14

Okay, so that's good. But how can we, how can we do this? How can we set it up so that when you contribute a link, if you are a trusted user, the approved status is automatically updated to true? How do we do that? Okay, well, why don't, why don't we do this? Why don't we go back to our users table, and we're going to add a new boolean here. So we'll say boolean, and set this to trusted. Is this a trusted user? And once again, we'll set the default equal to 0. Okay, so now, actually, this does bring up a, a good little tip. When you're working in development, and you haven't yet pushed to production, don't immediately create a new migration to add this new column. You really don't need to. Instead, update the original migration, and then refresh them. That way, once you're ready to deploy to production for the first time, you don't end up with 50 different migration files. You don't need it. Just update the

and then refresh them. That way, once you're ready to deploy to production for the first time, you don't end up with 50 different migration files. You don't need it. Just update the original one, and then say, php artisan migrate --refresh. Now, one thing to consider, though, of course, is that it will get rid of any test data you've created. And that's where database factories and database seeding become very useful, so that it doesn't even matter if you get rid of that data, because it was dummy data anyways. So anyways, this should be back to zero, and it is. And in fact, we've lost our User. But that's okay. We can bring that back in just a second. Like I said, you could create a seed file to do this, or, this is fine, App\User::create. And then we'll do a factory for App\CommunityLink::factory. And why don't we create 10 of them. And those will be associated with 10 different Users in the process.

Auto-Approve Trusted Submissions4:42

app User create. And then we'll do a factory for app CommunityLink. And why don't we create 10 of them. And those will be associated with 10 different Users in the process. Okay. So once again, the approved status is set to zero for all of them. But also, if we get our first User, we can see that the trusted status is set to zero as well. So now, let's do this. If we come back to our CommunityLink model, maybe we can add the logic right here. So just a quick recap, this is where, in your store method, you could say a CommunityLink from the given author and contribute using the given data. So we could say from that will create a new instance of CommunityLink, assign the User. But then maybe we could also say, if the supplied User, so the person who is currently signed in, if they are trusted, maybe we'll add that method in just a second. If they're trusted, then let's automatically

if the supplied User, so the person who is currently signed in, if they are trusted, maybe we'll add that method in just a second. If they're trusted, then let's automatically approve the link. And we could do that by either updating the property, so something like this, or we could call a method, which might be useful. So if the User is trusted, then approve the link. Now, if we scroll down, maybe right here. Approve. And I don't know, maybe we can make that public. Okay. So that's where we will set approved equals true, and then return the current instance. Okay. Does that make sense? So when we're building up this new CommunityLink instance, we check to see if the User is considered trusted. And if so, anything they publish should automatically be approved. So we'll set that here. That means we need to create this method. All right. So we used a little macro there to

And if so, anything they publish should automatically be approved. So we'll set that here. That means we need to create this method. All right. So we used a little macro there to build that up on the User model. And all that's going to do is return this trusted. And that's it. And you shouldn't need to, but if you did, you could always cast that directly to a Boolean, and that would be fine. All right. So let's see this in action. Let's find the, maybe the first User we got here. All right. Gina. We're going to make her a trusted User. So let's grab her again, and we'll say $user->trusted equals true, and $user->safe. Okay. So now, Gina is a trusted User, which means anything she tries to publish will automatically be approved. But actually, you know what? I don't know what her password is. So let's reset it to change me and save it, and now log in. Email address and change me. Okay. Great. So now, she will publish a link,

Test Trusted vs Untrusted7:11

you know what? I don't know what her password is. So let's reset it to changeMe and save it, and now log in. Email address and changeMe. Okay. Great. So now, she will publish a link, some article, and this will go to Google. No channel yet. We got to build that up. And this, once again, is where a database/seeder file is really useful. App\Channel::create([ 'title' => PHP, 'slug' => php, 'color' => blue ]). Okay. So if we give that another shot, we'll select that. Contribute the link. She is a trusted User, so it automatically works for her. So that means, though, what happens if she's not a trusted User? So once again, if we track her down, and we'll say $user->trusted = false, and then save it. Okay. So if she were to try another one now, it's not going to work. Some other article. This one will go to Yahoo. Yeah. If she contributes the link,

Flash Messages for Approval8:08

and then save it. Okay. So if she were to try another one now, it's not going to work. Some other article. This one will go to Yahoo. Yeah. If she contributes the link, you're not going to see it represented here, but she needs some feedback to know, even if it's as simple as, hey, somebody needs to approve your article first. So we can tackle that with a simple flash message. For example, in the store method, we're here. Yeah, right now we're just returning back. But it sounds like we need a flash message, and we need two different flash messages. So one should say something along the lines of, thanks for the contribution. But then another one should say, maybe a heading of thanks, and then this contribution will be approved shortly. You know what I mean? So if the user is trusted, yeah, we just say, hey, thanks. It's live on the site. If they're not trusted,

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟