abarua
4 months ago

Use input from one view to print result into another view

Posted 4 months ago by abarua

I want to use input from a form in one view and print results into another view. I get the following error: Undefined variable: users Thanks in advance!

The form (in a view called 'dashboard') that I am using to get email address:

...
<div class="search">
            <header><h3>Search Friend</h3></header>
            <form action="{{ route('search.friend') }}" method="post">
                <div class="form-group">
                    <input class="form-control" type="text" name="email" id="email" placeholder="Friend's email">
                </div>
                <button type="submit" class="btn btn-primary">Post</button>
                <input type="hidden" value="{{ Session::token() }}" name="_token">
            </form>
        </div>
...

The route to send data from 'dashboard' to Controller:

Route::post('/searchfriend',[
        'uses' => '[email protected]',
        'as' => 'search.friend',
        'middleware' => 'auth'
    ]);

The controller where I use the email to find user:

class FriendController extends Controller
{
    public function getSearchFriend(Request $request)
    {
        $this->validate($request,[
            'email' => 'required | email'
        ]);

        $email = $request['email'];
        $users = User::where('email',$email)->get();

        return view('userlist',['$users' => $users]);
    }


}

The route to send the result to a 'userlist' view:

Route::get('/userlist',[
        'uses' => '[email protected]',
        'as' => 'userlist',
        'middleware' => 'auth'
    ]);

Finally, the 'userlist' view:

@extends('layouts.master')

@section('title')
    Users
@endsection

@section('content')
    <section class="row new-post">
        <div class="col-md-6 col-md-offset-3">
            <header><h3>Users</h3></header>
            <div class="userlist">
                <header><h2>Click to Add Friend</h2></header>
                @foreach($users as $user)
                    <a href="" class="" id="">Name: {{ $user->username }}</a>
                @endforeach
            </div>
        </div>
    </section>
@endsection

Please sign in or create an account to participate in this conversation.