【问题标题】:can't get the data from edit function using Laravel 5.6无法使用 Laravel 5.6 从编辑功能中获取数据
【发布时间】:2020-07-13 14:53:14
【问题描述】:

我使用 Laravel 5.6 创建了 CRUD 应用程序。我创建了资源控制器来执行该功能。在控制器(索引和创建)中,函数工作正常,但编辑函数我尝试回显它显示为空白的变量。我不明白为什么会这样。请帮助解决这些问题。

模型文件

<?php

namespace Asset_Management_System;

use Illuminate\Database\Eloquent\Model;

class assetType extends Model
{
    //
}

控制器文件

<?php

use Illuminate\Http\Request;
use Asset_Management_System\assetType;

public function edit(assetType $assetTypes)
{
    $arr['assetTypes'] = $assetTypes;
    return view('assets.asset_types.edit')->with($arr);
}
?>

网址路径

http://127.0.0.1:8000/AssetTypes/1/edit

路线

Route::resource('AssetTypes','AssetTypeController');

当我点击编辑按钮时,它会编辑页面,但数据来了。

错误信息

缺少 [Route: AssetTypes.update] [URI: AssetTypes/{AssetType}] 的必需参数。 (查看:C:\xampp\htdocs\Asset_Management_Laravel\resources\views\assets\asset_types\edit.blade.php)

我还附上了错误消息截图。

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

    URL 路径是一个 GET 路径,用于显示类似表单的内容。将显示数据,因为您正在加载assetType 的实例但未使用view();

    你应该有这样的东西

    public function edit(assetType $AssetTypeData)
    {
        return view('assetType.edit', ['assetType' => $AssetTypeData]);
    }
    

    【讨论】:

      【解决方案2】:

      我认为你应该在编辑方法 args 中获得 $id

      Route::put('user/{id}', 'UserController@update');
      
      
      
      <?php
      
        namespace App\Http\Controllers;
      
        use Illuminate\Http\Request;
      
        class UserController extends Controller
        {
          /**
         * Update the given user.
         *
         * @param  Request  $request
         * @param  string  $id
         * @return Response
         */
        public function update(Request $request, $id)
        {
            //
        }
       }
      

      https://laravel.com/docs/5.7/controllers#resource-controllers

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-14
        • 2023-02-15
        • 1970-01-01
        • 2020-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-12
        相关资源
        最近更新 更多