laravel 5.4中未定义的视图变量

时间:2017-12-25 10:12:04

标签: php laravel laravel-5 laravel-5.4

我在视图中定义了一个变量。但问题是它显示未定义。我的任务是,首先我使用表单助手从用户那里获取所有输入,然后我想将其发布到商店功能&做一些计算,然后将其传递给视图以显示目的。但是我试过了,遗憾的是显示未定义。未定义的变量是“ 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

2 个答案:

答案 0 :(得分:0)

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

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

要:

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

store()方法中,不要返回视图。取而代之的是重定向back()index()show()方法。

答案 1 :(得分:0)

首先:检查$total=$request->fee+$total;

的值

ie:put&#34; dd(&#39; $ total&#39;,$ total);&#34;在你设置的行之后,在返回之前。

第二:简化return view(...)->with('totalAmount', $total);

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

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