Search code examples
phplaravelviewcontrollersave

Can't save in Laravel 5.7 - Nothing happening


I'm with a problem that I still couldn't figure. In a view I'm trying to get some information from a table, from this information I need to save new data and also update some other data on this same table but, when I click on "Save", nothing happens.

I've been looking here and over the internet, found something similar but nothing that could help.

I have a view like this:

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

I have a model which is like this:

<?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;    
}

And in my controller I have this:

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');
}

Also, I'm not getting nothing in the dd($PrinterTable); line, I already look for this code a lot of times and didn't saw anything wrong or different from other codes that I wrote before.

Can anyone please help me? Maybe someone will see something that I didn't.

Thanks all in advance and, if there is some post that regards this matter and I miss it, please let me know about it.


Solution

  • First off you do not have an opening form tag in your view which you provide at the stop, not sure if you forgot to copy it.

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

    The submit button next to be inside this form, this will basically call the test method in the TestController, of course you need to modify this to your needs.

    Second you have many fields in your "form" but you don't have any fillable array in your Model, you need to add each field (column) to your fillable array for security reasons, do following:

    class TiPrinter extends Model
    {
       protected $table = 'titoner_troca';
    
       protected $fillable = ['value1', 'value2']; // add every fillable name into this array
    }
    

    You need to add in all your fillable elements, for instance if I have a form and only value1 and value2 are going to be saved or modified the above would be fine.

    Third you should be able to save TiPrinter in your store method just like this:

    public function store(Request $request) {
        $tiprinter = TiPrinter::create($request->all());
    
        if($tiprinter) {
            // success redirect to somewhere....
        }
    
        // error do something*
    }
    

    Just try these steps and if it still fails provide more information please.