2017-01-23 97 views
1

在我的laravel应用程序中,我将鼠标悬停在链接上时显示计划ID。基本上从数据库中获取计划表的ID。我得到的错误:将id传递给视图中的超链接href

Undefined variable: plan 

控制器:

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\Plan; 


class PlanController extends Controller 
{ 

    public function getPlan(Request $request, $id){ 

      $plan = Plan::find($id); 


     return redirect()->route('index'); 

    } 

} 

路线:

Route::get('/plan_id/{id}', [ 

    'uses' => '[email protected]', 
    'as' => 'get-plan' 

]); 

查看该链接:

<a href="{{route('get-plan', ['id' => $plan->id])}}" type="submit" id="basic" class="btn btn-primary btn-lg btn-block">Sign up with Basic </a> 

回答

1

您应该返回视图你在建立链接,而不是使用redir ecting:

return view('view', compact('plan')); 

正如我告诉你,在你的另一个问题,如果你使用重定向,你应该使用->with('plan', $plan);数据闪进了会议。

+0

IM的回报视图。我仍然在视图中未定义。 – steven

+0

我需要更改超链接href中的任何内容吗? – steven

+0

@steven如果你正在使用'return view('view',compact('plan'));'它仍然给你同样的错误,这意味着一些其他方法返回视图。 –

0

在该代码中,变量$ plan是该函数的局部。它没有外部范围。 你可以将其声明为全球

global $plan; 

或使用类中的公共变量:使用

$this->plan