Everything about laravel users

Everything about laravel users

Most of issues with laravel developers or who tries to work with it (not developer yet!) is in users section, how to sign them, login them, assign roles, get their info, show them something etc.

Today we will look at some of these issues and I'll give you short and painless answers so you can process your work without requireing to read a long article.

issues we will talk about:

  1. Auth (Register/Login)
  2. Show parts of website only to registered users
  3. Getting list of all users
  4. Getting list of users with posts (only)
  5. Show users info in profile page

 

Auth (Register/Login)

In order to make authentication for your laravel application if you working with v. 5+ all you need to do is to type/copy this code in your CMD/Terminal while you are in your project folder

php artisan make:auth
PHP

this code will install a layout view, registration and login views, as well as routes for all authentication end-points. A HomeController will also be generated to handle post-login requests to your application's dashboard. more info

 

Show parts of website only to registered users

In laravel 5.5 all you need to do to achieve this is to place your code between

@auth

// Your code here

@endauth
Markup

to show thing to guests only

@guest
    Welcome Guest!
@endguest
Markup

while in laravel 5.4 and before you have to do something like

@if(auth()->check())

// Your code here

@endif
Markup

and to show to guest only in laravel 5.4 and before

@if(auth()->guest())

// Your code here

@endif
Markup

more info

Getting list of all users

If you want to have a list of all users you can do it like this

public function index() {
  $users = User::all();
  return view('index', compact("users"));
}
PHP

 

Getting list of users with posts (only)

If you want to get list of users with posts only for example you need to make authors page, you can do it like this

public function authors() {
  $users = User::has('posts')->get();
  return view('authors', compact("users"));
}
PHP

Remember has method comes with plural posts and not singular post

Show users info in profile page

If you would like to have profile page for your users in this case I will make it for users with posts only.

purpose: imagine you have list of authors in your website and you want to show their info and posts in their own page.

Step 1:

controller:

public function author($username) {
  $user = User::where("username", $username)->with("posts")->first();
  return view('author', compact('user'));
}
PHP

I try to link their profile page with username you can do it by id or any other element you like.

Step 2:

Blade

//Show author personal info
{{ $user->name }} 

{{ $user->username }}


// Show their posts
@foreach ($user->posts as $post)
   {{ $post->title }}
@endforeach
PHP

Here I have two different element at the same page, first I get user info as his/her name and username then at the bottom I got his/her posts.

note: you might have question why for  getting author info I did not use foreach, well it's because first in our config file we used singular $user = second because we are in author page base on his/her username laravel already knows which user info should be return so will not be any contradiction in here.

Step 3:

Route

Route::get('/author/{username}', '[email protected]')->name('author');
PHP

That's all you need to make author profile page.

- Last updated 1 year ago

Be the first to leave a comment.

You must login to leave a comment