【问题标题】:Fetching all users in laravel 5.2在 laravel 5.2 中获取所有用户
【发布时间】:2017-01-13 18:25:26
【问题描述】:

我正在尝试获取我的应用程序中的所有用户,以便在选择表单中选择用户名或 id,但经过长时间的努力我无法实现这一点。我检索消息的 MessageController 是:

public function getMail(){
        $message = Message::orderBy('created_at', 'desc')->get();

$pple = User::where('id', '!=', Auth::id())->pluck('name', 'id')->all();
    var_dump($pple);

            return view('mail', array('user' => $pple))->with('messages', $message, 'users',$pple );
    }

我想要呈现给用户的观点是:

<div>
<select id="recipient">
   <option>{{$pple->name}}</option>
       </select>
          </div>

我确实尝试过这样做来帮助我选择用户 id 或 name 以在我的消息系统中使用。如果有人帮助我,我将不胜感激

【问题讨论】:

    标签: laravel laravel-5


    【解决方案1】:

    你应该让你的用户得到结果:

    $messages = Message::orderBy('created_at', 'desc')->get();
    
    $users = User::where('id', '!=', Auth::id())->pluck('name', 'id');
    

    然后返回

    return view('mail')->with(compact('messages', 'users'));
    

    然后在你的视图中这样使用它

    <select name="recipient" id="recipient">
        @foreach($users as $id => $name)
            <option value="{{ $id }}">{{ $name }}</option>
        @enforeach
    </select>
    

    不要忘记将name 添加到您的选择中。

    【讨论】:

    • 我的退货单应该是什么样子?我有这样的“return view('mail', array('user' => $pple))->with('messages', $message, 'users',$pple );"
    • 做了一些修改来澄清
    • mmh 未定义变量:用户(查看:C:\xampp\htdocs\dating\resources\views\mail.blade.php)
    • 你不需要在-&gt;pluck()之前使用-&gt;get(),因为Eloquent有自己的pluck()方法。
    • 习惯的力量 ;)
    【解决方案2】:

    在您看来,这应该可行

    @foreach($pple as $userId => $userName)
        <option value="{{ $userId }}">{{ $userName }}</option>
    @endforeach
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 2016-11-24
      • 2014-04-24
      • 2017-03-27
      相关资源
      最近更新 更多