查找数据库中是否存在用户ID,如果不存在则显示按钮

I'm looking for a bit of advice on the best way to tackle an if statement.

I have a registration type site, built in Laravel 5.1, and I want new users to fill out a questionnaire.

The way I want to do this is to display a button for them to click on my homepage, however I only want it to be displayed if they haven't filled one out yet.

I've been toying around with how to do this for a while, however I can't find the right solution.

The way I initially did this was retrieve all the results of the table in my routes.php file and passed this in to the view, like so:

Route::get('/', function () {
    $data = Questionnaire::all();
    return View::make('home')->with('data', $data);
});

And this returns all the data in the table in an array, that I then tried to loop through these with a foreach, matching the memberid field in the array against Auth::user->id(). And if no match was found then place the button on the page. However this will always display the button because it always finds a set that don't match, even if it finds a match later on.

At the end of the day, I want to compare the memberid values in my questionnaire table against the id of the member logged in and if they aren't in the questionnaire table then display the button. However, the homepage can be accessed before they're logged in and so simply using a where clause in the route file doesn't always work:

$data = Questionnaire::where("memberid", "=", Auth::user->id());

As it will fail if they aren't logged in.

Any solutions on how to solve this?

In my opinion, a better solution is to pass to the model id member and get the record if it exist, and if it does not, receive null, for example:

$member_quest = Questionnaire::where('memberid', $member_id)->first();

This solution will tell you whether the record exists or not.

Regarding a button on the home page, you may want to first make sure you are logged into the system, and if so, check the above query to know when questionnaire is filled or not.

If you want to compare only the member_id and Auth::user()->id. Then try this:

In your controller:

  $data = Qustionaire::where('member_id', Auth::user()->id)->first();
  return View::make('home',compact('data') );

In view:

 @if Auth::check() //this code checks if the user is logged in or not
   @if Auth::user->id == $data->id
     //Do something
   @else
     <button>MyButton</button>
   @endif
@endif

There are so many ways to solve this. Here's what I'd probably do. I would make the questionnaire table a one to one relation to the users table. (see http://laravel.com/docs/5.1/eloquent-relationships#one-to-one).

This allows you do do something like:

if($user->questionnaire) { .. } 

That doesnt yet solve the problem of the user not being logged in. You can solve that by passing Auth::user() to the view. For instance like this:

Route::get('/', function () {
  return View::make('home')->with('user', \Auth::user());
});

In your view you can now check like this:

@if($user and $user->questionnaire === false)
  // show questionnaire button
@endif

This assumes you dont want to show the button to people that arent logged in.