使用用户名从 Laravel 中的数据库获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26921885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:25:29  来源:igfitidea点击:

getting data from database in laravel using the username

phpmysqllaravel

提问by akimebless

I have a table in database called 'User'. All I want is to get the basic information of the user. I've tried to get data from database but the thing that keep me bugging is that I get all the data inside the table. UserTable

我在数据库中有一个名为“用户”的表。我想要的只是获取用户的基本信息。我试图从数据库中获取数据,但让我烦恼的是我获取了表中的所有数据。 UserTable

|id|username|firstname|lasttname|middlename|gender|age|birthdate|address|contact_number|designation|
 1   admin    lee       chiu      kim        male  47   07-22-87  cebu     0977452445    admin
 2   lrose    rose       loo      mar       female 27   04-02-88  cebu     0977452445    manager
 3   mgray    gray       men      try        male  37   01-22-89  cebu     0977452445    employee

UserProfile.php--> Model

UserProfile.php--> 型号

<?php 
    class UserProfile  extends Eloquent   {
        public $timestamps=false;
        protected $table = 'users';
        protected $fillable=array('firstname','lastname', 'middlename', 'gender', 'age', 'birthdate', 'address', 'contact_number', 'designation');
    }

This is the model of my site.

这是我网站的模型。

UserProfileContoller.php--> Controller

UserProfileContoller.php--> 控制器

<?php
class UserProfileController extends \BaseController {
    public function index($id)
    {
        $userprofile = User::find($id);

        return View::make('userprofile.index', array('userprofile' => $userprofile));

        //return View::make('userprofile.index')->with('UserProfiles',UserProfile::get());
    }

For example I am currently log in to username admin and I want to edit and view my profile. How can I get the data just for admin only. Please Help.

例如,我目前登录到用户名 admin,我想编辑和查看我的个人资料。我怎样才能获得仅供管理员使用的数据。请帮忙。

The situation hers is that. let say they have 3 employees. So in the table 3 username. lets assume that I'm not the admin. I will log in using lrose. and the other one log in as admin and so.on. If the three of us will go to our profiles the only thing to display there is their own profile and can edit it.

她的情况就是这样。假设他们有 3 名员工。所以在表3用户名中。让我们假设我不是管理员。我将使用 lrose 登录。另一个以管理员身份登录,依此类推。如果我们三个人将转到我们的个人资料,则唯一显示的是他们自己的个人资料并且可以对其进行编辑。

回答by Spencer Wieczorek

Assuming you property set-up your database why not just use the DBobject?

假设您对数据库进行了属性设置,为什么不直接使用该DB对象?

 // In the User table find the item with the username 'admin'
 DB::table('User')->where('username', '=', 'admin')->get();

To get the information of some user that is logged in (given you have their username), called something like $userprofile. Then all we need to do is findby id then get the username:

要获取某个已登录用户的信息(假设您有他们的用户名),请调用类似$userprofile. 然后我们需要做的就是find通过 id 然后获取username

 // $id is the ID of the logged in user
 $userprofile = User::find($id)->username; // get username of logged in user

回答by JMc

$userprofile::find($id) will get you the information for admin only, if $id = 1.

如果 $id = 1,$userprofile::find($id) 只会为您提供管理员的信息。

find() only returns 1 tuple from the database.

find() 仅从数据库中返回 1 个元组。

After you call find(), you can access the data as such:

调用 find() 后,您可以这样访问数据:

$userprofile->firstname

回答by Ray

you mean when you pass $id to index method of User Controller all the data of other users are returned?

你的意思是当你将 $id 传递给 User Controller 的 index 方法时,其他用户的所有数据都会被返回?

That's kinda weird.

这有点奇怪。

  1. $userprofile = User::find($id); change that line to $userprofile = User::find(1); and see if you can only get the data of admin.
  2. If not, probably you are calling other method. Probably you iterate over the index method somewhere else for multiple times so that you get all the data in the database. To confirm both possibilities, just add echo 'I am in' at the entry point of your index method. If no echo message is shown, the first possibility is true. If multiple echo messages occur, the second possibility is true.
  1. $userprofile = User::find($id); 将该行更改为 $userprofile = User::find(1); 看看是不是只能拿到admin的数据。
  2. 如果没有,您可能正在调用其他方法。可能您在其他地方多次迭代 index 方法,以便获得数据库中的所有数据。要确认这两种可能性,只需在 index 方法的入口点添加 echo 'I am in' 。如果没有显示回显消息,则第一种可能性为真。如果出现多个回显消息,则第二种可能为真。

Hope this helps.

希望这可以帮助。

回答by itachi

first of all, you need to use Sessionafter login and store the idof the username in the Session.

首先,您需要Session在登录后使用id并将用户名的存储在 Session 中。

e.g.

例如

Step 1:

第1步:

if($login) //successfull login
{
    Session::put('userid', $userid);
    //this id, you have to retreive it from the database after successful login
}

Step 2:

第2步:

Setup the route

设置 route

Route::get('profile',['uses' => 'UserProfileContoller@index']);

Step 3:

第 3 步:

Controller

控制器

public function index()
{
     if(is_null(Session::get('userid'))) return Redirect::to('login');

     return View::make('userprofile.index', ['userprofile' => $this->model->index()]);
     // Above code uses dependency injection to retrieve the information from db.
}

Step 4:

第四步:

UserProfileModel

用户档案模型

public function index()
{
    return DB::table('user')->where('id','=', Session::get('userid'))->first();
}

p.s. i find it easier to use query builder than eloquent orm. if you want to use orm, change the code where needed.

p.s. i find it easier to use query builder than eloquent orm. if you want to use orm, change the code where needed.