dongshi1868 2019-02-03 19:37
浏览 81

Laravel回显用于NON Auth :: User ID

Trying to work out if the following is possible:

I understand you can echo data in a blade via {{ Auth::user()->email }} to get email, {{ Auth::user()->name }} to get name, {{Auth::user()->id }} to get user ID etc.. and this is for the current logged in user.

But what i am trying to achieve is, if i type in a 'name' into a text field in a basic one field form, is there any way that i can have it dynamically show an echo of that 'name' related users ID, even though it is not that of the Auth::User ?

For Example: User ID: 1 with 'name' as ROGERHELLO can type in the 'name' POTATOHEAD and have it echo it's relative ID visible on the page which could be ID: 5?

I have searched about but cant seem to find a clear answer.. and hopefully this post can help others with a similar query in the future..

Thank you for any help!

  • 写回答

3条回答

  • doude1917 2019-02-03 20:28
    关注

    I understand you want to be able to search for users. If that is the case, try this:

    //in Controller
    public function search(Request $request)
    {
        $keyword = $request->get('search');
        $data = [];
    
        if (!empty($keyword)) {
            //you can search for anything here
            $data['users'] = User::where('name', 'LIKE', "%$keyword%")
                ->orWhere('email', 'LIKE', "%$keyword%")
                ->orWhere('occupation', 'LIKE', "%$keyword%")
                ->get();
        } else {
            //return a response to require an input 
        }
    
        return view('users.search', $data);
    }
    

    You can now display the search results on the view (users.search) returned.

    评论

报告相同问题?