簡體   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