【问题标题】:Undefined variable of view in laravel 5.4laravel 5.4中未定义的视图变量
【发布时间】:2017-12-26 15:52:38
【问题描述】:

我在视图中定义了一个变量。但问题是它显示未定义。我的任务是,首先我使用表单助手获取用户的所有输入,然后我想将其发布到 store 函数并进行一些计算,然后将其传递给视图以显示目的。但是我试过了,不幸的是显示未定义。未定义的变量是“totalamount”这里我附上了所有代码:

控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\TestSetup;
use Illuminate\Support\Facades\Input;
class TestRequestController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $allTestNames=TestSetup::all()->pluck('test_name','id');
//        $testNames=TestSetup::orderBy('test_name')
//                      ->get();

     return view('testrequestentries.createTestRequestEntry')->withAlltestnames($allTestNames);


//        return view('testrequestentries.createTestRequestEntry');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

//      if(Input::get('Add')) {
          $total=$request->fee+$total;
          return view('testrequestentries.createTestRequestEntry')->withTotalamount($total); 

//      }else if(Input::get('Save')){
//          
//      }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

查看

@extends ('main')
@section('title','|Test Request Entry')

@section('stylesheets')
    {!! Html:: style('css/parsley.css')!!}
@endsection  
@section('content')


            {!! Form::open(['route' => 'testrequests.store','data-parsley-validate'=>'']) !!}
                {{Form::label('patient_name','Name of the Patient:')}}
                {{Form::text('patient_name',null,array('class' => 'form-control','required'=>'', 'data-parsley-required-message' => 'Patient Name is required'))}}

                {{Form::label('date_of_birth','Date Of Birth:')}}
                {{Form::date('date_of_birth',null,array('class' => 'form-control','data-parsley-required' =>'true')) }}

                {{Form::label('mobile_no','Mobile No:')}}
                {{Form::text('mobile_no',null,array('class' => 'form-control','type'=> 'number','maxlength'=>'11','minlength' => '11','required'=>'', 'data-parsley-required-message' => 'Mobile No is required'))}}

                {{Form::label('test_id','Select Test:')}}               
                {{Form::select('test_id',$alltestnames,null,['class'=>'form-control','required'=>'','placeholder' => 'Pick a Test...'])}}         

                {{Form::label('fee','Fee:')}}
                {{Form::text('fee',null,array('class' => 'form-control','required'=>'', 'data-parsley-required-message' => 'Fee is required'))}}

                {{Form::submit('Add',array('class' => 'btn btn-lg btn-block btn-success','style' => 'margin-top:10px;'))}}


                <table class="table table-bordered" style="margin-top: 50px;">
                        <thead>
                            <tr>
                              <th>SL</th>
                              <th>Test Name</th>
                              <th>Fee</th>
                              <th>Type Name</th>
                            </tr>
                      </thead>
                      @php ($i=1)   

                             <tbody>
                              <tr>
                                <th scope="row">{{$i}}</th>
                                <td>{{$totalamount}}</td>


                              </tr>
                            </tbody>  
                            @php ($i++)


                    </table>

{!! Form::close() !!}

    @section('scripts')
        {!! Html:: script('js/parsley.min.js')  !!}
    @endsection
@endsection

【问题讨论】:

    标签: php laravel laravel-5 laravel-5.4


    【解决方案1】:

    我敢打赌视图是从create()这里返回的,所以改变这个:

    return view('testrequestentries.createTestRequestEntry')->withAlltestnames($allTestNames);
    

    收件人:

    return view('testrequestentries.createTestRequestEntry', [
        'alltestnames' => $allTestNames,
        'totalamount' => $total
    ]);
    

    store() 方法中,不返回视图。改为重定向 back()index()show() 方法。

    【讨论】:

    • @MohammadYakub 也在 store 方法中更改它。如果您遇到错误,请告诉我错误内容。
    • 我通过添加用户选择的所有费用得到总数。所以任务是如果用户按下添加按钮,那么之前会添加一笔费用。所以我的想法是,当用户按下添加按钮时,然后将其发布到存储功能中并仅向用户显示“总金额”但错误未定义
    • @MohammadYakub 所以,原来的问题解决了,你没看到Underfined variable 错误吗?至于total这个问题,不清楚也和原问题没有关系,所以建议你新建一个问题,具体描述一下你想得到什么。
    • 是的。如果我在创建中使用它......那么它可以......但我想在商店功能中使用
    【解决方案2】:

    第一件事:检查$total=$request-&gt;fee+$total;的值

    ie: put "dd('$total', $total);"就在你设置的行之后,在返回之前。

    第二个:让它更简单return view(...)-&gt;with('totalAmount', $total);

    在视图(BLADE 文件)中测试 $totalAmount 的值 即:{{dump('totalAmount', $totalAmount)}}

    无论如何,store() 方法不存储任何东西......为什么会这样?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      相关资源
      最近更新 更多