Data were not changed in the database - Standard 5.7 eloquent

Asked

Viewed 177 times

0

Good afternoon guys, I’m trying to do an update but the values entered in the fields not saved in the database, follow files to check.

Index.blade.php

@extends('layouts.app')
@section('content')
<?php
if (session('message')):?>

    <div class="alert alert-success" style=" opacity: 1;">
        {{ session('message') }}
    </div>
 <?php
  else:?>

 <?php
      endif;
       ?> 

  <div class="col-sm-12">

    <div class="card">
    <div class="card-header">

    </div>
    <div class="card-block">
    <div class="dt-responsive table-responsive">
      <a href="{{route('Aulas.create')}}" class="btn btn-primary ">Cadastrar uma nova aula </a>
  <p> </p>
    <table id="simpletable" class="table table-striped table-bordered nowrap">
    <thead>
    <tr>
    <th style="text-align: center;">Ritmo</th>
    <th style="text-align: center;">Dia da Semana</th>
    <th style="text-align: center;">Horário</th>
    <th style="text-align: center;">Status</th>
    <th style="text-align: center;">Alterar</th>
    <th style="text-align: center;">Excluir</th>
    </tr>
    </thead>
    <tbody>
<?php
$RowsCount = $Aulas->count();

if($RowsCount >= 0 ):

      foreach($Aulas as $models):
      ?>
      <tr>
        <td style=""><?php echo $models['Ritmo']; ?></td>
        <td style="text-align: center;"><?php echo $models['DiaSemana']; ?></td>
        <td style="text-align: center;"><?php echo $models['Horario']; ?></td>
        <td style="text-align: center;"><?php echo $models['Status']; ?></td>
        <td style="text-align: center;">
         <a class="btn btn-primary btn-outline-primary btn-icon" href="{{route('Aulas.edit', $models->Id)}}">A</a>
        </td>
        <td style="text-align: center;">
              <form method="POST" action="{{ route('Aulas.destroy', $models->Id) }}">
                @csrf
                @method('DELETE')
                <button type="submit" name="btn-deletar" class="btn btn-warning btn-outline-warning btn-icon" data-type="success" data-from="top" data-align="right">E</button>
              </form>
      </td>
      </tr>
       <?php
      endforeach;
      else: ?>

      <tr>
        <td>-</td>
        <td>-</td>
        <td>-</td>
        <td>-</td>
      </tr>

       <?php
      endif;
       ?> 
    </tbody>
    </table>
    </div>
    </div>
    </div>
</div>

@stop

route/web

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();
Route::group(['middleware' => 'web'], function () {

    Route::get('/home', 'HomeController@index')->name('home');

    Route::get('/ListAula', 'CadastrosBasicos\AulasController@Index')->name('aulas'); 

    Route::get('/CadAula', 'CadastrosBasicos\AulasController@create')->name('create');

     Route::get('/edit', 'CadastrosBasicos\AulasController@edit')->name('edit');

    Route::post('/store', 'CadastrosBasicos\AulasController@store')->name('store');

    Route::post('/update', 'CadastrosBasicos\AulasController@update')->name('update'); 

    Route::resource('Aulas','CadastrosBasicos\AulasController');

});

Controller

<?php

namespace App\Http\Controllers\CadastrosBasicos;


use Illuminate\Foundation\Auth\AuthenticatesUsers;
use App\Models\CadastrosBasicos\Aulas;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;

class AulasController extends Controller
{
   public function edit(Request $request, $Id )
    {
         $Aula = Aulas::find($Id);
         return view('CadastrosBasicos.Aulas.Editar', compact('Aula'));
    }



    public function update(Request $request, $Id)
    {
      $AulaDb = Aulas::find($Id);
      $AulaDb->Id = $Id;
      $AulaDb->Ritmo = $request->get('Ritmo');
      $AulaDb->DiaSemana = $request->get('DiaSemana');
      $AulaDb->Horario = $request->get('Horario');
      $AulaDb->Status = $request->get('Status');
      $AulaDb->save();

       return redirect() ->route('aulas')
                       ->with('message', 'Aula Alterada com sucesso!');
    }
}

Molds

<?php

namespace App\Models\CadastrosBasicos;

use Illuminate\Database\Eloquent\Model;

class Aulas extends Model
{
    protected $table = 'Aulas';
    public $timestamps = false;

    protected $fillable = ['Id','Ritmo','Horario','DiaSemana','Status'];

}
  • Failed to put the form you are using to update.

  • its routes have no pattern, and bad if the project is growing, the names should also follow a nomenclature, all this is good not to have shock routes.

1 answer

0


Eai Claudio blz? , next guy, in his method update, you inform that is passing an ID, and with this ID, you are looking for the Class. But on your route you are not passing an ID to the method, you should standardize your route passing the ID like this:

Route::post('/update/{aulaId:[0-9]+}', 'Registers Aulascontroller@update')->name('update');

NOTE: Remember to pass the ID on the update button that accesses the route, as you did not post her code, I do not know how it is.

And in your method, you capture this ID (Remember, you don’t need to enter the ID again a line you are doing the update):

public function update(Request $request, $aulaId)
{
  $data = $request->all();
  $AulaDb = Aulas::find($aulaId);      
  $AulaDb->Ritmo = $data->Ritmo;
  $AulaDb->DiaSemana = $data->DiaSemana;
  $AulaDb->Horario = $data->Horario;
  $AulaDb->Status = $data->Status;
  $AulaDb->save();
  return redirect() ->route('aulas')
                   ->with('message', 'Aula Alterada com sucesso!');
}

Another way you can also do it is like this, if the fields you are passing in the request are the same as in the bank:

public function update(Request $request, $aulaId){
     $aula = Aulas::findOrFail($aulaId);
     $aula = $aula->fill($request->all());
     $aula->save();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.