我正在使用Laravel5.2。目前,当用户登录时,他们被带到页面,此页面是必须显示的表单。我想这样做只有在表格还没有被填写然后展示时才会这样做。
我的表结构如下所示:
User Questions
--------------------------------------------
id id
name q1
email q2
password q3
user_id
我已设法做到这一点,当用户发布表单时,他们的用户ID也会发布到该表单,但我不知道如何检查是否显示此内容。
routes.php文件
Route::get('/', function () {
return view('welcome');
});
Route::auth();
Route::get('/home', 'HomeController@index');
Route::post('home', function () {
$userq = new App\Userregq();
$userq->Q1 = Input::Get('Q1');
$userq->Q2 = Input::Get('Q2');
$userq->Q3 = Input::Get('Q3');
$userq->user_id = Auth::user()->id;
$userq->save();
return redirect('level1');
});
这是我的形式:
<P> Please fill in this questionairre before you start if you have already done just click the next arrow:</p>
<div id="form_container">
{!! Form::open(array('url' => 'home'))!!}
{!!Form::label('Q1','Why have you decided to use this application?')!!}
{!!Form::textarea('Q1')!!}<br><br>
{!!Form::label('Q2','What three things would you like to get out of this application?')!!}
{!!Form::textarea('Q2')!!}<br><br>
{!!Form::label('Q3','What time constraints do you foresee and how can you plan around them?')!!}
{!!Form::textarea('Q3')!!}<br><br>
<input type = "hidden" name = "id" value = "{{Auth::user()->id}}">
{!!Form::submit('Submit')!!}
{!! Form::close()
!!}
更新
我已将此添加到 HomeController
<?php
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$showForm = ! App\Userregq()->whereUserId(Auth::user()->id)->exists();
//return view('home');
return view('form-view', compact('showForm'));
}
}
这是我更新后的表格:
@if($showForm)
<div id="form_container">
{!! Form::open(array('url' => 'home'))!!}
{!!Form::label('Q1','Why have you decided to use this application?')!!}
{!!Form::textarea('Q1')!!}<br><br>
{!!Form::label('Q2','What three things would you like to get out of this application?')!!}
{!!Form::textarea('Q2')!!}<br><br>
{!!Form::label('Q3','What time constraints do you foresee and how can you plan around them?')!!}
{!!Form::textarea('Q3')!!}<br><br>
<input type = "hidden" name = "id" value = "{{Auth::user()->id}}">
{!!Form::submit('Submit')!!}
{!! Form::close() !!}
@endif
答案 0 :(得分:1)
我会在表格中创建一个新列,例如questionairre_filled
,这是一个布尔值。因此,如果用户已回答所有问题,请将questionairre_filled
设置为1 / true,然后使用中间件进行检查。
如果它失败,中间件将重定向到带有Questionairre表单的页面。
中间件看起来像这样
<?php
namespace App\Http\Middleware;
use Closure;
class QuestionairreMiddleware
{
/**
* Run the request filter.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $role
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
if (! $request->user()->questionairre_filled) {
redirect to the form page
}
// If true redirect to the page without the forms
return $next($request);
}
}
答案 1 :(得分:0)
在不了解模型关系如何工作的情况下,最简单的解决方案是检查表中是否已存在当前用户ID。就个人而言,我更喜欢将此信息放在控制器中并将其作为变量加载。这是一个例子:
// controller
$showForm = ! App\Userregq::whereUserId(Auth::user()->id)->exists();
return view('form-view', compact('showForm'));
// form
@if ($showForm)
// show the form here
@endif
请注意,这只是一个例子。您可能需要检查用户是否已登录,您可能希望通过模型关系等来执行此操作。
答案 2 :(得分:0)
您可以在User模型中指定Userregq的关系,检查控制器的Question表中是否有记录。
用户模型:
Class User extends Model{
// ...
public function userregq(){
return $this->hasOne('App\Userregq');
}
}
<强> HomeController中:强>
Class HomeController extends Controller{
public function index(){
if (!Auth::user()->userregq){
// User didn't answer to the questions
}
else{
// User answered to the questions
}
}
}