繁体   English   中英

Laravel 5.4中未定义的视图变量

[英]Undefined variable of view in laravel 5.4

我在视图中定义一个变量。 但是问题是它显示未定义。 我的任务是,首先,我使用表单助手从用户那里获取所有输入,然后将其发布到存储功能并进行一些计算,然后将其传递给视图以显示目的。 但是我尝试了一下,不幸的是显示了undefined.Undefined变量是“ 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

我敢打赌,视图是从此处的create()返回的,因此请更改此内容:

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

至:

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

store()方法中,不要返回视图。 重定向back()或重定向到index()show()方法。

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

即:放“ dd('$ total',$ total);” 在您设置的行之后,返回之前。

第二:使其更简单地return view(...)->with('totalAmount', $total);

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

无论如何,store()方法不会存储任何内容...为什么?

暂无
暂无

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

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