2017-01-23 96 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