on my screen, you can see number : 24,26,33 this is the id_user when i click on "Ajouter Mission", the id are set to 24, instead of 26,33. (24 is the id to auth admin), why the id change to the authentified user when i click on modal button ?
dont comment my indian quality front end, i'll do it later :)
and there is my html :
Sorry, i paste everythig because i have no idea where this problem come from.
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
#csrf
You are in home as Admin !
#if(Session::has('succes'))
<div class="alert alert-success">
{{ Session::get('succes')}}
</div>
#endif
</br>
Liste des Utilisateurs
<table class="table table-sstriped card-body">
<thead>
<tr>
<th>Numero Matricule</th>
<th>Nom</th>
<th>Prenom</th>
<th>Rue</th>
<th>Code Postal</th>
<th>Ville</th>
<th>Adresse mail</th>
</tr>
</thead>
<tbody>
#foreach($users as $resp)
<tr>
<td>{{$resp->MATRICULE}}</td>
<td>{{$resp->NOM}}</td>
<td>{{$resp->PRENOM}}</td>
<td>{{$resp->RUE}}</td>
<td>{{$resp->CP}}</td>
<td>{{$resp->VILLE}}</td>
<td>{{$resp->email}}</td>
<td>{{$resp->ID_PERSONNELS}}</td>
<td>
#if($resp->id_role==1)
<form action="{{action('UserController#destroy', $resp['ID_PERSONNELS'])}}" method="post">
#csrf
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Supprimer</button>
</form>
</td>
#endif
<td>
#if($resp->id_role ==1)
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
Ajouter Mission{{$resp->ID_PERSONNELS}}
</button>
#else
No Way, it is Admin
#endif
<!-- Modal -->
<div class="modal fade" data-id="$resp->ID_PERSONNELS" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Ajouter une Mission a {{$resp->ID_PERSONNELS}}{{$resp->NOM}}{{$resp->email}}</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="{{route('addmission',26)}}" method="post">
#csrf
<div class="modal-body justify-content-center">
<!----------------------------------Body---------------------------------------------->
<label for="NOM" class="col-md-4 col-form-label text-md-center">{{ __('NOM') }}</label>
<div class="col-md-6">
<input id="NOM" type="text" class="form-control #error('NOM') is-invalid #enderror" name="NOM" required autocomplete="NOM" autofocus> #error('NOM')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span> #enderror
</div>
<label for="DATE_MISSION" class="col-md-4 col-form-label text-md-center">{{ __('DATE_MISSION') }}</label>
<div class="col-md-6">
<input id="DATE_MISSION" type="date" class="form-control #error('DATE_MISSION') is-invalid #enderror" name="DATE_MISSION" required autocomplete="DATE_MISSION" autofocus> #error('DATE_MISSION')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span> #enderror
</div>
<!-- <label class="col-md-4 col-form-label text-md-center" for="ID_PRATICIEN">{{ __('PRATICIEN') }}</label> -->
<!-- <select class="custom-select col-md-4 col-form-label text-md-center" id="inputGroupSelect01">
<option selected>Choose...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
</select> -->
<!--------------------------------End----Body------------------------------------>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Annuler</button>
<button type="submit" class="btn btn-primary">Ajouter Mission</button>
</form>
</div>
</div>
</div>
</td>
</tr>
</tbody> #endforeach
</div>
</table>
</div>
</div>
</div>
</div>
</div>
#endsection #csrf
store controller :
public function store(Request $request, $id){
$miss = User::find($id);
$mission = new Mission;
$mission->NOM =$request->input('NOM');
$mission->ID_PERSONNELS = $miss->ID_PERSONNELS;
$mission->ID_NOTE_DE_FRAIS = 1;
$mission->DATE_MISSION = $request->input('DATE_MISSION');
$mission->save();
return redirect()->action('AdminController#index')->with('succes', 'Mission Ajoutée');
}
and to be sure, the controller for table :
public function index()
{
$id=auth()->id();
$users = User::all();
return view('homeAdmin')->with('users', $users);
}
thx for help :)
You should be using something like AngularJS for this sort of stuff.
However, I believe your issue is that all your generated modals share the same ID, causing the same one to be opened by all the buttons.
Try making the ID of the modal something like MODAL-{{ ID_HERE }} and remember to set the data-target of the button to MODAL-{{ ID_HERE }}
Do this to your button:
data-target=“#MODAL-{{ $resp->id }}”
And to your modal:
id=“MODAL-{{ $resp->id }}”
I don't know what your database structure looks like but use something unique to each row to implement this, e.g. $resp->id
Honestly though, use something like AngularJS, you wouldn’t then have to programmatically generate a modal for all of the entries, you could just fetch the data on page load, ng-repeat them all in however you want and then have a singular modal that refers to the one they clicked, look into it.
Related
I have trouble in my view Blade, what's wrong with my code, am I wrong to write code in the controller or in my code model was I declare?
the error said
Trying to get property 'nama_member' of non-object (View:
C:\xampp\htdocs\rezkastore\resources\views\pages\daftar_pelanggan.blade.php)
Blade/View
#extends('layouts.app')
#section('title', 'Daftar Pelanggan')
#section('content')
<div class="header bg-primary pb-6">
<div class="container-fluid">
<div class="header-body"> </div>
</div>
</div>
<div class="container-fluid mt--6">
<!-- Table -->
<div class="row">
<div class="col">
<div class="card">
<!-- Card header -->
<div class="card-header">
<div class="row align-items-center py-0">
<div class="col-lg-6 col-7">
<h6 class="h2 d-inline-block mb-0">Data Pelanggan</h6>
</div>
<div class="col-lg-6 col-5 text-right">
<button class="btn btn-icon btn-primary" type="button" data-toggle="modal" data-target="#addModal">
<span class="btn-inner--icon"><i class="fa fa-plus-circle" aria-hidden="true"></i></span>
<span class="btn-inner--text">Tambah Data</span>
</button>
</div>
</div>
</div>
<div class="table-responsive py-4">
<table class="table table-flush" id="datatable-basic">
<thead class="thead-light">
<tr>
<th width="30px">No</th>
<th>Nama Produk</th>
<th>Alamat</th>
<th>No.Telp</th>
<th>Member</th>
<th>Menu</th>
</tr>
</thead>
<tfoot>
<tr>
<th width="20px">No</th>
<th>Nama</th>
<th>Alamat</th>
<th>No.Telp</th>
<th>Member</th>
<th>Menu</th>
</tr>
</tfoot>
<tbody>
#php
$no = 1;
#endphp
#foreach($daftar_pelanggan as $pelanggan)
<tr>
<td>{{$no++ }}</td>
<td>{{ $pelanggan->nama_pelanggan }}</td>
<td>{{ $pelanggan->alamat }}</td>
<td>{{ $pelanggan->no_telp }}</td>
<td> {{ $pelanggan->diskon->nama_member }}</td>
<td>
<button data-toggle="modal" data-target="#editModal-{{ $pelanggan->id }}" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i></button>
<button class="btn btn-sm btn-danger" type="button" onclick="deletepelanggan({{ $pelanggan->id }})"> <i class="fa fa-trash"></i>
</button>
<form id="delete-form-{{ $pelanggan->id }}" action="{{ route('daftar_pelanggan.delete',$pelanggan->id) }}" method="POST" style="display: none;">
#csrf
#method('DELETE')
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- Modal Add -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title mb-0" id="addModalLabel">Tambah Data Pelanggan</h5>
</div>
<div class="modal-body">
<!-- Card body -->
<form role="form" action="{{ route('daftar_pelanggan.create') }}" method="POST">
#csrf
#method('POST')
<!-- Input groups with icon -->
<div class="form-group row">
<label for="addNamaPelanggan" class="col-md-4 col-form-label form-control-label">Nama <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="nama" placeholder="Nama Lengkap" id="addNamaPelanggan" name="addNamaPelanggan" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group row">
<label for="addAlamat" class="col-md-4 col-form-label form-control-label">Alamat <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="alamat" placeholder="Jatibarang" id="addAlamat" name="addAlamat" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group row">
<label for="addNoTelp" class="col-md-4 col-form-label form-control-label">No.Telp <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="notelp" placeholder="083XXXXXXXXX" id="addNoTelp" name="addNoTelp" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group row">
<label for="addNoTelp" class="col-md-4 col-form-label form-control-label">diskon Member <span class="text-danger">*</span></label>
<div class="col-md-8">
<select class="form-control" name="AddDiskonid" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')"">
<option disabled selected>-- Pilih Member --</option>
#foreach($diskons as $diskon)
<option value="{{ $diskon->id }}">{{ $diskon->nama_member }}</option>
#endforeach
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Tambah Data</button>
</div>
</form>
</div>
</div>
</div>
<!-- Modal edit -->
#foreach($daftar_pelanggan as $pelanggan)
<div class="modal fade" id="editModal-{{ $pelanggan->id }}" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title mb-0" id="editModalLabel">Update Data Pelanggan</h5>
</div>
<div class="modal-body">
<!-- Card body -->
<form role="form" action="{{ route('daftar_pelanggan.update', $pelanggan->id) }}" method="POST" id="editForm">
#csrf
#method('PUT')
<!-- Input groups with icon -->
<div class="form-group row">
<label for="updateNamaPelanggan" class="col-md-4 col-form-label form-control-label">Nama <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="hidden" name="id" value="{{ $pelanggan->id }}">
<input class="form-control" type="nama" value="{{ $pelanggan->nama_pelanggan }}" name="updateNamaPelanggan" required >
</div>
</div>
<div class="form-group row">
<label for="updateAlamat" class="col-md-4 col-form-label form-control-label">Alamat <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="alamat" value="{{ $pelanggan->alamat }}" name="updateAlamat" required>
</div>
</div>
<div class="form-group row">
<label for="updateNoTelp" class="col-md-4 col-form-label form-control-label">No.Telp <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="notelp" value="{{ $pelanggan->no_telp }}" name="updateNoTelp" required>
</div>
</div>
<div class="form-group row">
<label for="addNoTelp" class="col-md-4 col-form-label form-control-label">Diskon Member <span class="text-danger">*</span></label>
<div class="col-md-8">
<select class="form-control" name="diskon_id" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
<option disabled selected>-- Kategori Member --</option>
#foreach($diskons as $diskon)
<option
#if($produk->diskon_id == $diskon->id)
selected="selected"
#endif
value="{{ $diskon->id }}">{{ $diskon->nama_member }}</option>
#endforeach
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Update Data</button>
</div>
</form>
</div>
</div>
</div>
#endforeach
#include('layouts.footers.auth')
#section('scripts')
<script type="text/javascript">
function deletepelanggan(id) {
swal({
title: 'Yakin Ingin Hapus Data ini?',
text: "Data Tidak Bisa Dikembalikan Setelah Dihapus!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Ya, Hapus!',
cancelButtonText: 'Tidak',
confirmButtonClass: 'btn btn-success',
cancelButtonClass: 'btn btn-danger',
buttonsStyling: false,
reverseButtons: true
}).then((result) => {
if (result.value) {
event.preventDefault();
document.getElementById('delete-form-'+id).submit();
swal(
'Deleted!',
'Your file has been deleted.',
'success')
} else (
result.dismiss === swal.DismissReason.cancel
)
})
}
</script>
#endsection
#endsection
Controller
class DaftarPelangganController extends Controller
{
public function index()
{
$daftar_pelanggan = DaftarPelanggan::all();
$diskons = Diskon::all();
return view('pages.daftar_pelanggan', compact('daftar_pelanggan', 'diskons'));
}
public function update(Request $request, $id)
{
$update_pelanggan = DaftarPelanggan::findOrFail($id);
$update_pelanggan->nama_pelanggan = $request->updateNamaPelanggan;
$update_pelanggan->alamat = $request->updateAlamat;
$update_pelanggan->no_telp = $request->updateNoTelp;
$update_pelanggan->diskon_id = $request->diskon_id;
$update_pelanggan->save();
if ($update_pelanggan) {
Alert::success(' Berhasil Update Data ', ' Silahkan dicek kembali');
} elseif (!$update_pelanggan) {
Alert::error('Data Sudah Ada', ' Silahkan coba lagi');
}
return redirect()->back();
}
public function create(Request $request)
{
$simpan = DB::table('daftar_pelanggans')->insert([
'nama_pelanggan' => $request->post('addNamaPelanggan'),
'alamat' => $request->post('addAlamat'),
'no_telp' => $request->post('addNoTelp'),
'diskon_id' => $request->post('addDiskonid'),
]);
if ($simpan) {
Alert::success(' Berhasil Tambah data ', ' Silahkan dicek kembali');
} else {
Alert::error('data gagal disimpan ', ' Silahkan coba lagi');
}
return redirect()->back();
}
public function delete($id)
{
DB::table('daftar_pelanggans')->where('id', $id)->delete();
return redirect()->back();
}
}
Model
class DaftarPelanggan extends Model
{
use HasFactory;
protected $table = "daftar_pelanggans";
protected $primaryKey = 'id';
protected $fillable = [
'nama_pelanggan',
'alamat',
'no_telp',
'poin',
'diskon_id',
];
public function diskon()
{
return $this->belongsTo(Diskon::class, 'diskon_id');
}
}
Eager load the diskon relation, it will also help prevent the N+1 issue.
public function index()
{
$daftar_pelanggan = DaftarPelanggan::with('diskon')->get();
$diskons = Diskon::all();
//Since $diskons is required for selects where in only id and nama_member is required
//You can select the two columns only to save on memory
$diskons = Diskon::select('id', 'nama_member')->get();
return view('pages.daftar_pelanggan',compact('daftar_pelanggan','diskons'));
}
To avoid getting error when a relation/related model does not exist, when displaying the related model's in a loop in blade, we can define the relation with a default
public function diskon(){
return $this->belongsTo(Diskon::class,'diskon_id')->withDefault([
'nama_member' => 'Guest',
]);
}
I am using Laravel 7 and I can add entries and view them from the database. When I try to edit or update edited changes, I either get a warning from Laravel saying that the The GET method is not supported for this route. Supported methods: PUT. However, I am using PUT in both the web.php route as well as in my method calls. Surely I am doing something wrong.
Here is a view of my Routes calling artisan route:list
in my Route Group in web.php Here are the controllers I am calling:
Route::group(['middleware' => ['auth', 'isAdmin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('registered-user', 'Admin\RegisteredController#index');
Route::get('registered-empresa', 'Admin\EmpresaController#index');
Route::get('role-edit/{id}', 'Admin\RegisteredController#edit');
Route::put('role-update/{id}', 'Admin\RegisteredController#updaterole');
Route::post('save-empresa', 'Admin\EmpresaController#store');
Route::put('edit-empresa/{id}', 'Admin\EmpresaController#update');
});
Here is the update function I created in EmpresaController.php:
public function update(Request $request, $id)
{
$this->validate($request, [
'erfc' => 'required',
'enombre' => 'required',
'ecalle' => 'required',
'ecolonia' => 'required',
'eciudad' => 'required',
'eestado' => 'required',
'ecpostal' => 'required',
'epais' => 'required',
]);
$empr = Empresa::find($id);
$empr->erfc = $request->input('erfc');
$empr->enombre = $request->input('enombre');
$empr->ecalle = $request->input('ecalle');
$empr->ecolonia = $request->input('ecolonia');
$empr->eciudad = $request->input('eciudad');
$empr->eestado = $request->input('eestado');
$empr->ecpostal = $request->input('ecpostal');
$empr->epais = $request->input('epais');
$empr->update();
return redirect('/registered-empresa')->with('status', 'Empresa se actualizó correctamente.');
}
And finally, here is the location of my empresas table where I both add, view and update my table in my index.blade.php file:
#extends('layouts.admin')
#section('content')
<div class="container-fluid mt-5">
<!-- Heading -->
<div class="card mb-4 wow fadeIn">
<!--Card content-->
<div class="card-body d-sm-flex justify-content-between">
<h4 class="mb-2 mb-sm-0 pt-1">
Home Page
<span>/</span>
<span>Empresas Registradas</span>
</h4>
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<div class="modal fade" id="modalRegisterForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="modal-title w-100 font-weight-bold">Añadir Empresa</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="/save-empresa" method="POST">
{{ csrf_field() }}
<div class="modal-body mx-3">
<div class="md-form mb-1">
<input type="text" name="erfc" id="orangeForm-erfc" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-erfc">RFC</label>
</div>
<div class="md-form mb-1">
<input type="text" name="enombre" id="orangeForm-enombre" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-enombre">Nombre</label>
</div>
<div class="md-form mb-1">
<input type="text" name="ecalle" id="orangeForm-ecalle" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-ecalle">Calle</label>
</div>
<div class="md-form mb-1">
<input type="text" name="ecolonia" id="orangeForm-ecolonia" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-ecolonia">Colonia</label>
</div>
<div class="md-form mb-1">
<input type="text" name="eciudad" id="orangeForm-eciudad" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-eciudad">Ciudad</label>
</div>
<div class="md-form mb-1">
<input type="text" name="eestado" id="orangeForm-eestado" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-eestado">Estado</label>
</div>
<div class="md-form mb-1">
<input type="text" name="ecpostal" id="orangeForm-ecpostal" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-ecpostal">Codigo Postal</label>
</div>
<div class="md-form mb-1">
<input type="text" name="epais" id="orangeForm-epais" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-epais">País</label>
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="euser" readonly id="orangeForm-euser" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="eregby" readonly id="orangeForm-eregby" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
</div>
<div class="modal-footer d-flex justify-content-center">
<button type="submit" class="btn btn-deep-orange">Añadir</button>
</div>
</form>
</div>
</div>
</div>
<div class="text-center">
<i class="fa fa-plus" aria-hidden="true"></i> Add
</div>
<!--edit modal start-->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="modal-title w-100 font-weight-bold">Editar Empresa</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="edit-empresa/" id="editForm">
{{ csrf_field() }}
#method('PUT')
<div class="modal-body mx-3">
<div class="md-form mb-1">
<input placeholder="RFC" type="text" name="erfc" id="erfc" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Nombre" type="text" name="enombre" id="enombre" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Calle" type="text" name="ecalle" id="ecalle" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Colonia" type="text" name="ecolonia" id="ecolonia" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Ciudad" type="text" name="eciudad" id="eciudad" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Estado" type="text" name="eestado" id="eestado" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Codigo Postal" type="text" name="ecpostal" id="ecpostal" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="País" type="text" name="epais" id="epais" class="form-control validate">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="euser" readonly id="euser" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="eregby" readonly id="eregby" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
</div>
{{-- <div class="modal-footer d-flex justify-content-center">
<button type="submit" class="btn btn-deep-orange">Editar</button>
</div> --}}
<div class="modal-footer d-flex justify-content-center">
<button type="submit" class="btn btn-deep-orange">Editar</button>
</div>
</form>
</div>
</div>
</div>
<!--end edit modal-->
</div>
</div>
<!-- Heading -->
<!--Grid row-->
<!--Grid column-->
<div class="row">
<!--Card-->
<div class="col-md-12 mb-4">
<!--Card content-->
<div class="card">
<!-- List group links -->
<div class="card-body">
<table id="datatable2" class="table table-bordered">
<thead>
<tr>
<th>RFC</th>
<th>Nombre</th>
<th>Calle</th>
<th>Colonia</th>
<th>Ciudad</th>
<th>Estado</th>
<th>Codigo Postal</th>
<th>País</th>
<th>Acción</th>
</tr>
</thead>
<tbody>
#foreach ($empresas as $empresa)
<tr>
<td>{{ $empresa->erfc }}</td>
<td>{{ $empresa->enombre }}</td>
<td>{{ $empresa->ecalle }}</td>
<td>{{ $empresa->ecolonia }}</td>
<td>{{ $empresa->eciudad }}</td>
<td>{{ $empresa->eestado }}</td>
<td>{{ $empresa->ecpostal }}</td>
<td>{{ $empresa->epais }}</td>
<td>
<div class="text-center">
Editar
<a class="badge badge-pill btn-danger px-3 py-2" href="">Borrar</a>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- List group links -->
</div>
</div>
<!--/.Card-->
</div>
<!--Grid row-->
</div>
#endsection
#section('scripts')
<script>
$(document).ready(function() {
let table = $('#datatable2').DataTable();
// Start edit record
table.on('click', '.edit', function() {
$tr = $(this).closest('tr');
if($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
let data = table.row($tr).data();
console.log(data);
$('#erfc').val(data[0]);
$('#enombre').val(data[1]);
$('#ecalle').val(data[2]);
$('#ecolonia').val(data[3]);
$('#eciudad').val(data[4]);
$('#eestado').val(data[5]);
$('#ecpostal').val(data[6]);
$('#epais').val(data[7]);
$('#editForm').attr('action', '/edit-empresa/'+data[0]);
$('#editModal').modal('show');
});
// End edit record
});
</script>
#endsection
I am pretty sure it is in this file that I am doing something wrong. Any help on how I can do this better or if I missed something, I would surely appreciate it. Thank you in advance.
in web.php
Route::patch('edit-empresa/{id}', 'Admin\EmpresaController#update');
index.blade.php
#method('PATCH')
You need to specify method as POST when defining the form even though you are including the #method('PUT') inside the form. That is because HTML does not support PUT method directly, and by default it will be a GET. So to correct:
Change this:
<form action="edit-empresa/" id="editForm">
TO
<form action="edit-empresa/" id="editForm" method="POST">
For some reason I could not get this to work using the modal and jquery method so I eliminated the datatables jquery from the bottom of the index.blade.php. My first error was to not call the data first. I created a seperate file called edit.blade.php within the view-admin-empresa folder. Here is the code:
#extends('layouts.admin')
#section('content')
<div class="container-fluid mt-5">
<!-- Heading -->
<div class="card mb-4 wow fadeIn">
<!--Card content-->
<div class="card-body d-sm-flex justify-content-between">
<h4 class="mb-2 mb-sm-0 pt-1">
<span>Empresa Registrada - Editar Empresa</span>
</h4>
</div>
</div>
<!-- Heading -->
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4 class="card-title">Editar Empresa</h4>
<form action="{{ url('empresa-update/'.$empresa->id) }}" id="editForm" method="POST">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="modal-body mx-3">
<div class="md-form mb-1">
<label for="erfc">RFC</label>
<input value="{{ $empresa->erfc }}" type="text" name="erfc" id="erfc" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="enombre">Nombre</label>
<input value="{{ $empresa->enombre }}" type="text" name="enombre" id="enombre" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecalle">Calle</label>
<input value="{{ $empresa->ecalle }}" type="text" name="ecalle" id="ecalle" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecolonia">Colonia</label>
<input value="{{ $empresa->ecolonia }}" type="text" name="ecolonia" id="ecolonia" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecuidad">Ciudad</label>
<input value="{{ $empresa->eciudad }}" type="text" name="eciudad" id="eciudad" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="eestado">Estado</label>
<input value="{{ $empresa->eestado }}" type="text" name="eestado" id="eestado" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecpostal">Codigo Postal</label>
<input value="{{ $empresa->ecpostal }}" type="text" name="ecpostal" id="ecpostal" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="epais">País</label>
<input value="{{ $empresa->epais }}" type="text" name="epais" id="epais" class="form-control validate">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="euser" readonly id="euser" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="eregby" readonly id="eregby" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
</div>
<div class="modal-footer d-flex justify-content-center">
Cancelar
<button type="submit" class="btn btn-deep-orange">Editar</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Then in the web.php, I created the following routes:
Route::group(['middleware' => ['auth', 'isAdmin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('registered-user', 'Admin\RegisteredController#index');
Route::get('registered-empresa', 'Admin\EmpresaController#index');
Route::get('role-edit/{id}', 'Admin\RegisteredController#edit');
Route::put('role-update/{id}', 'Admin\RegisteredController#updaterole');
Route::post('save-empresa', 'Admin\EmpresaController#store');
Route::get('/edit-empresa/{id}', 'Admin\EmpresaController#edit');
Route::put('/empresa-update/{id}', 'Admin\EmpresaController#update');
});
As mentioned earlier, I eliminated the edit modal and redirected to the empresa edit.blade.php file.
Now, I am able to edit without any problems. Thank you to Arjun bhati and user3532758 for taking a stab at this issue. I really appreciate it.
I have a page that has a form with a select menu and if there are some validation errors in that form the validation errors are shown using "#include('includes.errors')". But in this same page I have a button that when the user clicks in it it shows a modal where the user can introduce a subject, a message to send an email. In this modal I also have "#include('includes.errors')".
Issue: So the issue is that if there are validation errors in the form in the modal because the subject or messare were not filled by the user that errors appear on the modal but also on the same page above the form that has the select menu. Also if there are some validation errors in the form that has the select menu and the user opens the modal that validation erros also appear in the modal.
Do you know how to fix the issue?
Form with select menu:
#include('includes.errors')
<div class="card">
<div class="card_body">
<form method="post"
action="{{route('conferences.storeQuantities', ['id' => $confernece->id, 'slug' => $conference->slug])}}">
<ul class="list-group list-group-flush">
{{ csrf_field() }}
#foreach($registration_types as $rtype)
<li>
<span>{{$rtype->name}}</span>
<select id="rtype_{{ $rtype->id }}" data-price="{{ $rtype->price }}"
name="rtypes[{{ $rtype->name }}]">
<option value="">0</option>
#for ($i = $rtype->min_participants; $i <= $rtype-> max_participants; $i++)
<option value="{{ $i }}">{{ $i }}</option>
#endfor
</select>
<span>X {{$rtype->presentPrice()}}€</span>
</li>
#endforeach
</ul>
</form>
</div>
</div>
Modal:
<div class="modal fade bd-example-modal-lg" id="contactOrganizer" tabindex="-1"
role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Contact Organizer</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<div class="col">
#include('includes.errors')
#if($flash = session('email_sent'))
<div class="alert alert-success" role="alert">
<strong><i class="fa fa-check-circle" aria-hidden="true"></i></strong>
{{ $flash }}
</div>
#endif
<form method="post"
action="{{route('users.contactOrganizer', ['conference_id' => $conference->id])}}"
enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<label for="subject">Subject</label>
<input type="text" class="form-control" value="{{ old('subject') }}"
name="subject"
id="subject">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" value="{{ old('message') }}"
name="message" rows="3"></textarea>
</div>
<input type="submit" class="btn btn-primary btn" value="Send"/>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
jQuery to show the moda if there are validation errors in the contact organizer form:
$(window).on('load', function () {
#if ($errors->any())
$('#contactOrganizer').trigger('click');
#endif
#if(session('email_sent'))
$('#contactOrganizer').trigger('click');
#endif
});
The errors.blade.php file:
#if ($errors->any())
<div class="alert alert-danger mt-3">
<ul>
#foreach ($errors->all() as $error)
<li class="text-danger"><strong><i class="fa fa-times" aria-hidden="true"></i></strong> {{ $error }}</li>
#endforeach
</ul>
</div>
#endif
You can acheave what you want using the named message bag like this :
In the controller you have to name your errors like this :
function storeQuantities(){ //assuming this is your controller function
$validator = Validator::make($request->all(), [
// your validation rules here :)
]);
if ($validator->fails())
{
return redirect()->back()->withErrors($validator, 'conferneceErrors');
}
}
Use the same thing for the other function :
function contactOrganizer(){ //assuming this is your controller function
$validator = Validator::make($request->all(), [
// your validation rules here :)
]);
if ($validator->fails())
{
return redirect()->back()->withErrors($validator, 'contactErrors');
}
}
In the view you can do this :
#include('includes.errors', ['errors' => $errors->conferneceErrors])
For the modal :
#include('includes.errors', ['errors' => $errors->contactErrors])
First of all, i want you to know that i'm a beginner, (please be kind) and i first searched for a solution but couldn't find.
I'm trying to build a blog using an php mvc framework. In this moment, when i want to edit an article, i'm redirecting to a new page and do there the work, but i want to switch to jquery (or javascript) and i'm stuck to selecting the article category from the select menu. I have an twig global variable containing all the categories (for the navigation). This is my code so far:
JS:
$('#editModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var title = button.data('title')
var category = button.data('category')
var body = button.data('body')
var modal = $(this)
modal.find('#title').val(title)
modal.find('#article-id').val(button.data('id'))
modal.find('#category').val(category)
modal.find('#body').val(body)
});
Modal:
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Changed your mind? Paste your thoughts!</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form class="form" method="post" action="/adminzone/articles/update" id="formEditArticle">
<div class="modal-body">
<input id="article-id" name="id" type="hidden" value={{article.id}} />
<div class="form-group">
<label for="title">Title</label>
<input id="title" class="form-control" name="title" placeholder="Title" value="{{ article.title }}" autofocus required pattern=".{2,80}" title="The title must have at least 2 characters and maximim 80."/>
</div>
<div class="form-group">
<label for="category">Category</label>
<select id="category" class="form-control" name="category" required>
{% for category in categories %}
<option value="{{ category.id }}" {% if category.id == article.category %} selected="selected" {% endif %}>{{ category.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea id="body" class="form-control" name="body" placeholder="Paste here your ideas..." required minlength="20" rows=10>{{ article.body }}</textarea>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-lg btn-secondary" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-lg btn-primary mr-5"><i class="fa fa-lg fa-save"></i> Spreat the word!</button>
</div>
</div>
</form>
</div>
</div>
</div>
The button:
I have this code below, if a user is not authenticated it appears this "Login" link:
#if(!\Auth::check())
<span><a id="show_login_modal" href="javascript:void(0)">Login</a>
</span>
#endif
If the user logins with success the modal close and the user is loged in. Its working fine.
But if there is a validation error, for example if password is inscorrect, the modal is closed the user is not loged in and no error message appears. For the validaiton message appear is necessary to click in "login" to open again the modal and it apperas "These credentials do not match our records."
Do you know how to if there is a validation error open the modal without be necessary to click in "login"?
jQuery:
$('#show_login_modal').click(function(){
$('#login_modal').modal('show');
})
$('#close_login_modal').click(function(){
$('#login_modal').modal('hide');
})
Modal:
<div class="modal fade" id="login_modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Login</h5>
<button type="button" class="close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
#include('includes.errors')
<form class="clearfix" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group col-12 px-0">
<label for="inputEmail4">Email</label>
<input type="email" class="form-control" value="{{ old('email') }}" name="email" required autofocus placeholder="Email">
</div>
<div class="form-group col-12 px-0">
<label for="inputEmail4">Password
<a href="{{ route('password.request') }}" class="text-gray ml-1" style="text-decoration: underline">
<small>Recover password</small></a> </label>
<input type="password" class="form-control" name="password" required placeholder="Palavra-passe">
</div>
<button type="submit" class="btn btn-primary btn d-block w-100">Login</button>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" id="close_login_modal" class="btn btn-primary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
errors.blade.php
#if ($errors->any())
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif