【问题标题】:Laravel Data passing from view to controllerLaravel 数据从视图传递到控制器
【发布时间】:2019-04-15 07:19:01
【问题描述】:

我需要将数据从视图文件传递到控制器文件。目的是在编辑记录时向用户显示以前的数据。我的视图文件包含以下代码:

<a href="{{ route('feestype.edit', $feesType) }}" class="btn btn-info btn-sm">Edit</a>

我在将 dd 方法传递给控制器​​之前调用了它。这是dd的结果:

  FeesType {#287 ▼
  #table: "fees_types"
  #fillable: array:2 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:5 [▶]
  #original: array:5 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

当我在控制器文件中收到对象时,它会在 dd 上显示此结果:

FeesType {#283 ▼
  #table: "fees_types"
  #fillable: array:2 [▶]
  #connection: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

问题是连接变量在控制器中返回空值,但在视图中它返回我当前的连接数据库:mysql。 这是我的控制器文件的编辑方法:

public function edit(FeesType $feesType)
{
    //
    //$feesType = FeesType::find($feesType->id);
    dd($feesType);
    return view('feestype.edit',['feesType'=>$feesType]);
}

这是我的路线定义:

Route::resource('feestype','FeesTypesController');

我不知道这背后的原因。有人可以帮我吗?

【问题讨论】:

  • connection variable returns a null 是什么意思?你说的连接变量是什么?我想我明白了......所以你这里唯一的问题是$feesType-&gt;connection 是空的?
  • dd结果的json#connection实例
  • “我需要将数据从视图文件传递到控制器文件”,您正在向后工作 - 这不是事情的工作方式。可能值得考虑用实际目标来更新你的问题,因为你有很多信息与你想要完成的任何事情相矛盾,即:你有很多与传递无关的代码从控制器到视图的数据。
  • 亲爱的朋友,我不明白你的回答。请让我知道您不清楚的地方
  • 这不是一个答案,而是一条评论,要求您澄清您想要完成的工作,因为您当前的描述与您共享的代码冲突。你为什么要获取数据库连接信息?这与这个等式无关。

标签: php laravel


【解决方案1】:

试试这样:

路线

<a href="{{ route('feestype.edit', $feesType->id) }}" class="btn btn-info btn-sm">Edit</a>

控制器

public function edit($id)
{
    $feesType = \App\FeesType::find($id);
    dd($feesType);
    return view('feestype.edit',['feesType'=>$feesType]);
}

【讨论】:

    猜你喜欢
    • 2017-01-29
    • 2015-07-25
    • 2017-09-03
    • 2016-01-30
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多