简体   繁体   English

将 id 传递给视图中的超链接的 href

[英]Passing an id to a hyperlink's href within a view

In my laravel application I'm aiming to display plan id when I hover over a link.在我的 Laravel 应用程序中,当我将鼠标悬停在链接上时,我的目标是显示计划 ID。 Basically get the id of the plan's table from the database.基本上从数据库中获取计划表的 id。 I'm getting an error of:我收到以下错误:

Undefined variable: plan  

Controller:控制器:

<?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');

    }

}

Routes:路线:

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

    'uses' => 'PlanController@getPlan',
    'as' => 'get-plan'

]);

View with the link:用链接查看:

  <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>

You should return the view where you're building the link, instead of using redirecting:您应该返回构建链接的视图,而不是使用重定向:

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

And as I told you in your other question, if you're using redirect, you should use ->with('plan', $plan);正如我在你的另一个问题中告诉你的,如果你使用重定向,你应该使用->with('plan', $plan); to flash data into the session.将数据闪存到会话中。

In that code, the variable $plan is local to the function.在该代码中,变量 $plan 是函数的局部变量。 It does not have external scope.它没有外部作用域。 You can either declare it as global您可以将其声明为全局

global $plan;

or use public variable on the class:或在类上使用公共变量:

$this->plan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM