Php 如果用户没有';我还没填呢?
我正在使用Laravel5.2。当前,当用户登录时,他们会被带到一个页面,该页面上必须显示一个表单。我想这样做,只有当表格还没有填写,然后它显示 我的表格结构如图所示:Php 如果用户没有';我还没填呢?,php,sql,forms,laravel,if-statement,Php,Sql,Forms,Laravel,If Statement,我正在使用Laravel5.2。当前,当用户登录时,他们会被带到一个页面,该页面上必须显示一个表单。我想这样做,只有当表格还没有填写,然后它显示 我的表格结构如图所示: User Questions -------------------------------------------- id id name q1 email
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()
!!}
Class User extends Model{
// ...
public function userregq(){
return $this->hasOne('App\Userregq');
}
}
Class HomeController extends Controller{
public function index(){
if (!Auth::user()->userregq){
// User didn't answer to the questions
}
else{
// User answered to the questions
}
}
}
请在开始之前填写此问题。如果您已经填写了,请单击下一个箭头:
{!!Form::open(数组('url'=>'home'))
{!!Form::label('Q1','为什么决定使用此应用程序?')!!}
{!!Form::textarea('Q1')!!}
{!!Form::label('Q2','您希望从这个应用程序中获得哪三样东西?')
{!!Form::textarea('Q2')!!}
{!!Form::label('Q3','你预见到了什么时间限制,你如何计划解决这些限制?')
{!!Form::textarea('Q3')!!}
{!!表单::提交('submit')
{!!Form::close()
!!}
更新
我已将此添加到我的家庭控制器中
<?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'));
}
}
在不知道模型关系如何工作的情况下,最简单的解决方案是检查表中是否已经存在当前用户id。就我个人而言,我更喜欢将这些信息放在控制器中,并将其作为变量加载。这里有一个例子:
// controller
$showForm = ! App\Userregq::whereUserId(Auth::user()->id)->exists();
return view('form-view', compact('showForm'));
// form
@if ($showForm)
// show the form here
@endif
请注意,这只是一个示例。您可能需要检查用户是否已登录,您可能希望通过模型关系等进行检查。我将在表中创建一个新列,例如questionairre\u 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);
}
}
您可以在用户模型中指定与Userregq的关系,检查问题表中是否有来自控制器的记录
用户型号:
<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()
!!}
Class User extends Model{
// ...
public function userregq(){
return $this->hasOne('App\Userregq');
}
}
Class HomeController extends Controller{
public function index(){
if (!Auth::user()->userregq){
// User didn't answer to the questions
}
else{
// User answered to the questions
}
}
}
家庭控制器:
<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()
!!}
Class User extends Model{
// ...
public function userregq(){
return $this->hasOne('App\Userregq');
}
}
Class HomeController extends Controller{
public function index(){
if (!Auth::user()->userregq){
// User didn't answer to the questions
}
else{
// User answered to the questions
}
}
}
我试着把它放在我的控制器中,作为一个变量加载,我试过用if,但似乎不能让它工作。你能展示一下你试过的吗?这个概念是合理的,所以你的代码中可能有一些小东西需要修正。嗯,看起来是正确的。您是否遇到一些错误,或者只是没有错误而显示了表单?我遇到了此错误调用未定义函数App\Http\Controllers\App\Userregq()
感谢您迄今为止的帮助!您必须在控制器中“使用”这个类Userregq。我已经在我的用户模型中定义了这种关系,比如:`public function userq(){return$this->hasOne('Userregq');}}`在你评论过的if-else中会出现什么?因此,在第一节中,我会使用正常的页面布局,在第二节中,我会添加表单代码?相反,在第一节中,你使用表单,在“else”中使用正常的home,我成功地做到了这一点,但现在我遇到了这个错误Class'App\Http\Controllers\Auth'未找到
我尝试了其他建议的方法,但它们都不起作用,因此我将尝试一下。提交表单以向布尔属性发送隐藏输入时,这是否与以前相同?