laravel 5.4中未定义的视图变量

问题描述 投票:1回答:2

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

controller:

<?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)
    {
        //
    }
}

view

@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
2个回答
0
投票

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

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

至:

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

store()方法中,不要返回视图。改为使用back()index()show()方法。


0
投票

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

即:把“dd('$ total',$ total);”在你设置之前,在返回之前。

第二:让它更简单qazxsw poi

在视图(BLADE文件)中测试$ totalAmount的值,即:return view(...)->with('totalAmount', $total);

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

© www.soinside.com 2019 - 2024. All rights reserved.