无法在Laravel 5.7中保存-什么都没发生

时间:2019-01-07 17:29:02

标签: php laravel view controller save

我有一个仍然无法解决的问题。 在一个视图中,我试图从表中获取一些信息,从这些信息中,我需要保存新数据,并更新同一表上的其他一些数据,但是,当我单击“保存”时,什么也没发生。

我一直在这里寻找和通过互联网,发现类似的东西,但没有任何帮助。

我有这样的看法:

<div class="form-group">
    {!! csrf_field() !!}        

    @if(isset($impressora))       

        <div class="form-row">
            <div class="form-group col-md-4">
                <label for="setor">Setor*:</label>
                <input class="form-control" placeholder="Digite o setor..." required="required" name="setor" type="text" value="{{ $impressora->setor }}" id="setor">
            </div>
            <div class="form-group col-md-4">
                <label for="marca">Marca*:</label>
                <input class="form-control" placeholder="Digite a marca..." required="required" name="marca" type="text" value="{{ $impressora->marca }}" id="marca">
            </div>
            <div class="form-group col-md-4">
                <label for="modelo">Modelo*:</label>
                <input class="form-control" placeholder="Digite o modelo..." required="required" name="modelo" type="text" value="{{ $impressora->modelo }}" id="modelo">
            </div>     
        </div>

        <div class="form-row">
            <div class="form-group col-md-4">
                <label for="tipo">Tipo*:</label>                    
                <select name="tipo" id="tipo" class="form-control" required="required">
                    <option value="Toner">Toner</option>
                    <option value="Tinta">Tinta</option>
                    <option value="Pecas">Peças</option>
                </select>
            </div>
            <div class="form-group col-md-4">
                <label for="tonner">Tonner*:</label>                    
                <select name="toner" id="toner" class="form-control" required="required">
                    <option value="Q2612A">Q2612A</option>
                    <option value="TN580/650">TN580/650</option>
                    <option value="T664 - PRETO">T664 - PRETO</option>
                    <option value="T664 - AMARELO">T664 - AMARELO</option>
                    <option value="T664 - CIANO">T664 - CIANO</option>
                    <option value="T664 - MAGENTA">T664 - MAGENTA</option>
                    <option value="CARTUCHO 45">CARTUCHO 45</option>
                    <option value="12017SR">12017SR</option>
                    <option value="CE278A">CE278A</option>
                    <option value="CB435A/CB436A/CE285A">CB435A/CB436A/CE285A</option>
                    <option value="TN360">TN360</option>
                    <option value="TN3472">TN3472</option>
                    <option value="TN750">TN750</option>
                    <option value="AMARELO">AMARELO</option>
                    <option value="CIANO">CIANO</option>
                    <option value="MAGENTA">MAGENTA</option>
                    <option value="PRETO">PRETO</option>
                    <option value="ROLO PRESSÃO BROTHER HL2240">ROLO PRESSÃO BROTHER HL2240</option>
                    <option value="ROLO PRESSÃO BROTHER HL2270">ROLO PRESSÃO BROTHER HL2270</option>
                    <option value="TN660">TN660</option>
                    <option value="LEXMARK E120">LEXMARK E120</option>
                    <option value="CLINDRO BROTHER DCP8080">CLINDRO BROTHER DCP8080</option>
                    <option value="CLINDRO BROTHER DR450">CLINDRO BROTHER DR450</option>
                    <option value="CLINDRO BROTHER DR620">CLINDRO BROTHER DR620</option>
                    <option value="CLINDRO BROTHER DR350">CLINDRO BROTHER DR350</option>
                    <option value="ROLO DE PAPEL DE PONTO">ROLO DE PAPEL DE PONTO</option>
                    <option value="TN450">TN450</option>
                </select>
            </div>
            <div class="form-group col-md-4">
                <label for="setor">Marca do Toner*:</label>                    
                <select name="tonerMarca" id="tonerMarca" class="form-control" required="required">
                    <option value="TONER CARTRIDGE">TONER CARTRIDGE</option>
                    <option value="EVOLUT">EVOLUT</option>
                    <option value="CHINAMATE">CHINAMATE</option>
                    <option value="EPSON">EPSON</option>
                    <option value="HP">HP</option>
                    <option value="S.M.">S.M.</option>
                    <option value="A.E.">A.E.</option>
                    <option value="TONER LASER">TONER LASER</option>
                    <option value="MC">MC</option>
                    <option value="J.PROLAB">J.PROLAB</option>
                    <option value="PQTC">PQTC</option>
                    <option value="KATUN">KATUN</option>
                    <option value="N.S.">N.S.</option>
                </select>
            </div>
        </div>

        <div class="form-row">
            <div class="form-group col-md-4">
                <label for="datacompra">Data da Troca*:</label>
                <input class="form-control" required="required" name="datacompra" type="date" value="" id="datacompra">
            </div>
            <div class="form-group col-md-4">
                <label for="local">Quantidade*:</label>                                     
                <input class="form-control" placeholder="Quantidade de toner..." required="required" name="quantidade" type="text" id="quantidade">               
            </div>                
            <div class="form-group col-md-4">
                <label for="local">Folhas*:</label>                                     
                <input class="form-control" placeholder="Quantidade de folhas..." required="required" name="folhas" type="text" id="folhas">
            </div>
        </div>           
    @endif

    <div class="form-group">            
        <input type="submit" Value="Salvar" class="btn btn-primary form-control">
    </div>
</div>

我有一个这样的模型:

<?php

namespace SouzaCambos;

use Illuminate\Database\Eloquent\Model;

class TiPrinter extends Model
{
   protected $table = 'titoner_troca';

   protected $primaryKey = 'idtroca';

   public $timestamps = false;

   public $increment = false;    
}

在我的控制器中,我有这个:

public function printerInsert(Request $request){

    $PrinterTable = TiPrinter::all();

    //dd($PrinterTable);

    if ($PrinterTable->idtroca != null) {
        $oldsheet = TiPrinter::latest('folhas')
            ->where('marca', $request->marca)
            ->where('modelo', $request->modelo)
            ->where('toner', $request->toner)
            ->where('marca_toner', $request->tonerMarca)
            ->first();

        $pageCount = TiPrinter::latest('folhasnew')
            ->where('marca', $request->marca)
            ->where('modelo', $request->modelo)
            ->where('toner', $request->toner)
            ->where('marca_toner', $request->tonerMarca)
            ->first();
    }   

    $TiPrinter = new TiPrinter;
    $TiPrinter->setor = $request->setor;
    $TiPrinter->marca = $request->marca;
    $TiPrinter->modelo = $request->modelo;
    $TiPrinter->toner = $request->toner;
    $TiPrinter->marca_toner = $request->tonerMarca;
    $TiPrinter->folhas = $request->folhas;

    if (is_null($pageCount)) {
        $pageCount->folhasnew = 0;
    } elseif ($pageCount->folhasnew >= 0) {            
        $pageCount->folhasnew = $request->folhas - $oldsheet->folhas;
        $TiPrinter->folhasnew = 0;
        //$TiPrinter->folhasnew = $request->folhas - $oldsheet->folhas;
    }      

    //Decremento toner
    $printerSub = TiToner::where('modelo', $request->toner)
        ->where('marca', $request->tonerMarca)
        ->first();

    $printerSub->quantidade = $printerSub->quantidade - $request->quantidade;
    $printerSub->data = $request->dataTroca;

    //Save na tabela toner
    if ($pageCount != null) {
        $pageCount->save();
    }
    $TiPrinter->save();
    $printerSub->save();        

    return redirect()->to('portal-cambos/ti/impressoras/TiImpressorasToner');
}

此外,我在dd($ PrinterTable)中什么也没得到;一行,我已经在寻找这段代码很多次了,没有发现与我以前编写的其他代码有误或不同。

有人可以帮助我吗?也许有人会看到我没看到的东西。

预先感谢所有,如果有关于此事的帖子,我想念它,请告诉我。

1 个答案:

答案 0 :(得分:1)

首先,您在视图中没有站点提供的开始表单标签,不确定是否忘记复制它。

<form method="post" action="{{ action('TestController@test') }}">

此表单内的“提交”按钮将基本上调用TestController中的测试方法,当然您需要根据需要对其进行修改。

第二,“表单”中有很多字段,但是模型中没有任何可填充数组,出于安全原因,您需要将每个字段(列)添加到可填充数组中,请执行以下操作:

class TiPrinter extends Model
{
   protected $table = 'titoner_troca';

   protected $fillable = ['value1', 'value2']; // add every fillable name into this array
}

您需要添加所有可填充元素,例如,如果我有一个表单,并且仅将value1和value2保存或修改,上面的方法就可以了。

第三,您应该能够将TiPrinter保存在存储方法中,如下所示:

public function store(Request $request) {
    $tiprinter = TiPrinter::create($request->all());

    if($tiprinter) {
        // success redirect to somewhere....
    }

    // error do something*
}

只需尝试以下步骤,如果仍然失败,请提供更多信息。