在laravel中使用带参数的函数

时间:2016-05-10 15:46:22

标签: php laravel-5 laravel-5.2

我有这条路线:

Route::post('/quiz/category/{$name}', 'playquiz@category');

playquiz控制器的类别功能是:

public function category($name)
  {
        $ch = DB::select('select * from quiz where category="$name" and level="1"');
        return View('quiz.index',['quiz'=>$ch]);     

 }

为什么会这样?

<a href="{{url('quiz/category/geography')}}" class="list-group-item list-group-item-success">Geography</a> 

链接不应该工作?怎么办呢?

quiz.index查看:

@foreach ($quiz as $q)

    {{ $q->qid }}.  
    {{ $q->question }}<br>
    <input type='radio' name='mycheck[".$q->qid."]' value='1'>     
    {{ $q->opt1 }}<br>
    <input type='radio' name='mycheck[".$q->qid."]' value='2'>     
    {{ $q->opt2 }}<br>
    <input type='radio' name='mycheck[".$q->qid."]' value='3'>    
    {{ $q->opt3 }}<br>
    <input type='radio' name='mycheck[".$q->qid."]' value='4'>   
    {{ $q->opt4 }}<br><br>

@endforeach        

2 个答案:

答案 0 :(得分:0)

将路线改为

//Check removing of $ from name
Route::get('/quiz/category/{name}', 'playquiz@category');

您正在使用实际上是GET请求的链接。

您的查询也是错误的。这是正确的。

public function category($name)
{
    $ch = DB::select("select * from quiz where category='$name' and level='1'");
    return View('quiz.index',['quiz'=>$ch]);     

}

或者您也可以这样做

DB::table('quiz')->where('name',$name)->get(); 
DB::table('quiz')->where('name',$name)->first(); 

首先会返回所有带有名称的测验的集合,然后只返回一个模型。您可以根据要求选择任何一种。

答案 1 :(得分:-1)

制作测验模型,确保它扩展模型。像这样:

$resource = Invoke-AzureRmResourceAction -ResourceGroupName <resource-group> -ResourceType Microsoft.Web/sites/config -ResourceName <appservice-name>/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
$resource.Properties

然后你的函数来查询:

使用App \ Quiz;

Routes:
Route::post('/quiz/category', 'playquiz@category');

<?php namespace App;

class Quiz extends Model
{

    protected $table = 'Quiz'; //table name
    protected $fillable = ['mass assignable table fields'];

}

查看:

public function category(Request $request)
  {
        $ch = Quiz::whereCategory($request->name)->whereLevel(1)->get();
        return View('quiz.index')->with(compact('ch');     
 }

享受:)