checked for multiple checkbox in update form - laravel

This is my blade file for the update form. So how I should I retrieve those data in the form by checked the checkbox.
<div class="form-group row">
<label class="col-md-3 col-form-label form-control-label" for="input-description">Hobbies</label>
<div class="col-md-9">
<input type="checkbox" name="hobbies[]" class="form-control-label" value="football" > Football
<input type="checkbox" name="hobbies[]" class="form-control-label" value="basketball"> Basketball
<input type="checkbox" name="hobbies[]" class="form-control-label" value="table_tennis"> Table Tennis
<input type="checkbox" name="hobbies[]" class="form-control-label" value="hockey"> Hockey
<input type="checkbox" name="hobbies[]" class="form-control-label" value="others"> Others
</div>
</div>
</div>
This is my Controller
public function edit($id)
{
$student=Student::find($id);
return view('students.edit', compact('student'));
}
In the database, .hobbies are stored in hobbies column like this
["basketball","football",'table_tennis']

You should use foreach loop inside blade that will be more maintainable:
<div class="form-group row">
<label class="col-md-3 col-form-label form-control-label" for="input-description">Hobbies</label>
<div class="col-md-9">
#foreach($hobbies as $hobby)
<input type="checkbox" name="hobbies[]" #if(in_array($hobby,$checked_hobbies)) checked #endif class="form-control-label" value="{{ $hobby }}" > {{ $hobby }}
#endforeach
</div>
</div>
and of course you should pass hobbies and checked_hobbies array in controller:
$hobbies = ["basketball","football",'table_tennis'];
$checked_hobbies = ["basketball","football"];
return view('test', compact('hobbies','checked_hobbies'));

Related

fetch datas from database based on selected data dropdown using laravel 8

hai everyone i want to fetch data (affected device name, device intended use, class of device) from database based on my selected dropdown (mda registration num),i get the registration numb er from database but i don't how to fetch other related datas based on registration num.How i can gets datas.Here my controller n create.blade.php,pls help me
create.blade.php
<div class="form-group row">
<label for="inputMedName col-auto" class="col-lg-3 col-form-label " for="mdaRegisNo1">1. MDA Device Registration No. <span style="color:red;">*</span></label>
<div class="col-6 col-lg-6">
<select name="mdaRegisNo" class="form-control " id = "mdaRegisNo1" select data-size="5" data-live-search="true">
<option > Select MDA Registration Number</option>
#foreach($devices as $key=>$device)
<option value="{{$key}}">{{$device->device_reg_no}}</option>
#endforeach
</select>
</div>
<input type='button' value='Seleted option' id='but_read'>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label ">2. Affected Device Name <span style="color:red;">*</span></label>
<div class="col-9 col-lg-9">
<input name="devName" type="text" class="form-control" >
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label ">3. Device intended use <span style="color:red;">*</span></label>
<div class="col-9 col-lg-9">
<input name="devUse" type="text" class="form-control" >
</div>
</div>
<div class="form-group row">
<label class="col-lg-3 col-form-label ">4. Class of Device<span style="color:red;">*</span></label>
<div class="col-9 col-lg-9">
<input name="devClass" type="text" class="form-control" >
</div>
</div>
RecallControlle.php
public function create()
{
$recall = Recall::latest()->first();
$ref_recall = 'MDA/Recall/P'.$this->getRefSuffix($recall);
//$devices = DB::table('devices');
$devices = Device::get();
$post = Device::query();
$device = DB::table('devices')->select('device_reg_no')->distinct()->get()->pluck('device_reg_no')->sort();
//dd($devices);
//return json_encode($devices);
return view('recall.create',['devices'=>$devices])->with([
'ref_recall' => $ref_recall,
'ref_mpr' => ''
]);
}
You have to use js to request data when your dropdown been selected.
Follow this doc to request data: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
pass your selected value to your api
get result from api
append value to input

Error "The PATCH method is not supported for this route. Supported methods: GET, HEAD, POST. " update method

Well hello there, I am trying to update a register of my table assistants, but when I push the button submit of the form, this error appear, I am using a table pivot between assistants and events, but i am only try to edit a assistant.This is the error
This is my code AssistantController.php file and the methods edit and update
edit and update methods
public function edit(Assistant $assistant)
{
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return view('assistants.edit',compact('assistant','event'));
}
public function update(Request $request, Assistant $assistant)
{
#updating
$assistant->update($request->all());
//$assistants->user()->associate(Auth::user());
//
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return redirect('assistants.index',compact('assistant','event'));
}
This is my form to assistants
input form assistants
#csrf
<div class="form-group">
<label for="id">Document:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('id') ?? $assistant->id }}" name="id" autofocus>
<div>{{ $errors->first('id') }}</div>
</div>
<div class="form-group">
<label for="name">Name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('name') ?? $assistant->name }}" name="name" placeholder="First Name assistant">
<div>{{ $errors->first('name') }}</div>
</div>
<div class="form-group">
<label for="last_name">Last name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('last_name') ?? $assistant->last_name }}" name="last_name" placeholder="Last name assistant">
<div>{{ $errors->first('last_name') }}</div>
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('phone') ?? $assistant->phone }}" name="phone">
<div>{{ $errors->first('phone') }}</div>
</div>
<div class="form-group">
<label for="email">Email:</label>
<small class="text-muted">Required(*)</small>
<input type="mail" class="form-control form-control-sm " value="{{ old('email') ?? $assistant->email }}" name="email">
<div>{{ $errors->first('email') }}</div>
</div>
<div class="form-group">
<label for="observations">Observations:</label>
<input type="text" class="form-control form-control-sm " value="{{ old('observations') ?? $assistant->observations }}"name="observations">
<div>{{ $errors->first('observations') }}</div>
</div>
This is my edit page
#extends('layouts.back')
#section('title','Edit assistants')
#section('content')
<div class="container p-4">
<div class="row">
<div class="card">
<div class="card-header">
<div class="card-title">
<h3>Edit details to assistant:</h3><br>
<h4><strong> {{$assistant->name}} {{$assistant->last_name}}</strong> </h4>
</div>
</div>
<div class="card-body">
<form action="/assistants" method="POST">
#method('PATCH')
#include('assistants.form')
<button type="submit" class="btn btn-primary">Update</button>
Cancel
</form>
</div>
<div class="card-footer">
</div>
</div>
</div>
</div>
#endsection
There are my routes
routes
Auth::routes();
Route::get('/', 'HomeController#index')->name('home');
Route::resource('events', 'EventController');
Route::resource('assistants', 'AssistantController');
Route::resource('certificates', 'CertificateController');
Route::resource('signers', 'SignerController');
I am beginner.
thank you all.
In your edit page, change your form action from
<form action="/assistants" method="POST">
to
<form action="/assistants/{{ $assistant->id }}" method="POST">
Without the assistant id in the action field, Laravel thinks you are trying to update the base assistants route, which is an invalid action for that route.

Update single field in a Profile Update Form in Laravel 5

I am trying to update a single field in my user profile form section in the Laravel app.
I can save fields correctly in DB but input values and placeholders are taking wrong values. In every hit Save, the values doesn' change, and they are taken from the last listed user profile details. In my case this is user#3. The problem is when I log in with the user's #1 credentials, value and placeholder are taken from user #3. When I log in with user #2, again from user #3. Only values of user#3 are correct and I can manipulate it with no issues for both fields.
When i update the profile fields with user#1 it saves the entered one filed, but because the 2nd filed inherits the user#3 input details it saves it in field 2 of user#1 which makes a wrong entry. I can't leave null in those fields by default. My mass assignment is guarded.
How can save/update just a single field in the blade template without affecting the other fields in the form?
My routes:
Route::get( '/profile', 'userController\\profileEdit#profileEdit')->name('profileEdit');
Route::post('/profile', 'userController\\profileEdit#update')->name('update');
My controller:
namespace App\Http\Controllers\userController;
use App\Model\Hause_users;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class profileEdit extends Controller
{
function profileEdit (Request $request){
$user = Hause_users::all();
$name = $request->session()->get('name');
$request->session()->keep([request('username', 'email')]);
return view('frontview.layouts.profile',['user'=>$user])->with('username' , $name );
}
function update (Request $request){
$user = Hause_users::where('username', $request->session()->get('name'))->first();
$user->fill(['email' => request('Email')]) ;
$user->save();
$user->phone;
//dd($user->phone->phone);
if ($user->phone === null) {
$user->phone->phone->create(['phone' => request('tel')]);
}
else{
$user->phone->update(['phone' => request('tel')]);
}
return back()->withInput();
}
Blade file: `
#extends('frontview.layouts.userView')
#extends('frontview.layouts.default')
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#section('title')
#endsection
#section('content')
#foreach($user as $v )
#endforeach
<h2 class="form-group col-md-6">Здравей, {{$username }} </h2>
<form class = "pb2" method="POST" name = 'profile' action='profile' >
{{ csrf_field()}}
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">Поща</label>
<input type="email" class="form-control" name = "Email" id="inputEmail4"
value="{{$v['Email']}}"
placeholder="{{$v->Email}}">
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Промени Парола</label>
<input type="password" class="form-control" id="inputPassword4" placeholder="Парола">
</div>
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" name = "Adress" id="inputAddress" placeholder="Снежанка 2">
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputAddress">Телефон</label>
<input class="form-control" type="text" name = 'tel' value="{{$v->phone['phone']}}"
placeholder="{{$v->phone['phone']}}"
id="example-tel-input" >
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputCity">Град</label><input type="text" class="form-control" id="inputCity">
<label for="inputCity">Квартал</label><input type="text" class="form-control" id="inputCity">
</div>
{{--<div class="col-md-6" >--}}
{{--<label for="image">Качи снимка</label>--}}
{{--<input type="file" name = "image">--}}
{{--<div>{{$errors->first('image') }}</div>--}}
{{--</div>--}}
</div>
{{--<div ><img src="https://mdbootstrap.com/img/Photos/Others/placeholder-avatar.jpg"--}}
{{--class="rounded-circle z-depth-1-half avatar-pic" alt="example placeholder avatar">--}}
{{--</div>--}}
{{--<div class="d-flex justify-content-center">--}}
{{--<div class="btn btn-mdb-color btn-rounded float-left">--}}
{{--<span>Add photo</span>--}}
{{--<input type="file">--}}
{{--</div>--}}
{{--</div>--}}
{{--</div>--}}
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="gridCheck">
<label class="form-check-label" for="gridCheck">
Запомни ме!
</label>
</div>
</div>
<button type="submit" class="btn btn-primary">Запази</button>
</form>
#endsection
#section('name')
{{ $username }}
#endsection
Output summary:
On img#1 are the correct entry details . This is other section not the Profile edit one. Currently loged user is U#1 but as you can see on image 2, values and placeholder of both fields are for the U#3. When i hit the blue button U#1 saves the untouched filed input of U#3. Same is when i log in with U#2.
Actually the answer here is quite simple. What i am doing wrong is that i am not passing the value of the currently logged user to the view correctly. On my profileEdit method i was using $user = Hause_users::all(); and then looping trough all id's into the view and then fetching every field. But because the view doesn know which user passes the data, the foreach always returns the last user id from the array with its input, no matter which user is currently logged in. Then the data was overridden with wrong inputs.
The solution is also simple.
Instead of $user = Hause_users::all();
i have used
$user = Hause_users::where('username', $request->session()->get('name'))->first();
and then into view i was objecting the $user variable without any loops like this:
<form class = "pb2" method="POST" name = 'profile' action='profile' >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{{--<input type="hidden" name="_method" value="PATCH">--}}
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputEmail4">Поща</label>
<input type="Email" class="form-control" name = "Email" id="inputEmail4"
value="{{$user->Email}}"
placeholder="{{$user->Email}}">
</div>
<div class="form-group col-md-6">
<label for="inputPassword4">Промени Парола</label>
<input type="password" class="form-control" id="inputPassword4" placeholder="Парола">
</div>
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" name = "Adress" id="inputAddress" placeholder="Снежанка 2">
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputAddress">Телефон</label>
<input class="form-control" type="text" name = 'tel' value="{{$user->phone['phone']}}"
placeholder="{{$user->phone['phone']}}"
id="example-tel-input" >
Basically this is a detailed explanation to all that not using the built in Auth system of Laravel

How to autofill Edit form with old Information from two different tables?

I have this form "Edit tache". Editing one "tache" affects two tables user and technicien, when i choose to edit tache the edit form is autofilled with information from the technicien table but the fields related to the table user are not autofilled. How could i autofill the form with information from the two tables? Thank you.
controller
public function edit($id)
{
$technicien=technicien::find($id);
$user = user::orderBy('id', 'asc')->get();
return view('technicien.edit',['moyenne_avis'=>$technicien],['actif'=>$technicien],['user_id'=>$technicien])->with('users', $user );
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
return redirect('technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Tache</h1>
<form action="{{ route('technicien.update', $actif->id , $moyenne_avis->id ) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="">Nom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">Prenom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name ="moyenne_avis" class="form-control"value ="{{$moyenne_avis->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name ="actif" class="form-control"value ="{{$actif->actif}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
route
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::put('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
$user = user::find($id);
if they are retrieved using one id on the controller, else please tell the relation between them.
and in your view, i don't see a reason to check old since its not updated yet! use
{{$user->nom)}}

Laravel :error when updating the database after editing data

I have two tables table "technician" and table "user" with a connection one to one and I have a form through which I edit the technician information. When I save the changes I get an error and the update doesn't work.Here is my form my code and a screenshot of the error Thank you in advance.
route.php
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::patch('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
edit.php
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Technicien</h1>
<form action="{{ route('technicien.update', $moyenne_avis->id , $actif->id , $user->nom , $user->prenom ,$user->email ) }}" method="update">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="nom">Nom</label>
<input id="nom" type="text" class="form-control" name="user[nom]" value="{{$user->nom}}" >
</div>
<div class="form-group">
<label for="prenom">Prenom</label>
<input id="prenom" type="text" class="form-control" name="user[prenom]" value="{{$user->prenom}}" >
</div>
<div class="form-group">
<label for="prenom">Email</label>
<input id="prenom" type="text" class="form-control" name="user[email]" value="{{$user->email}}" >
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name="moyenne_avis" class="form-control" value ="{{$moyenne_avis->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name="actif" class="form-control" value ="{{$moyenne_avis->actif}}" >
</div>
<div class="form-group">
<input type="submit" value="enregistrer" class="form-control btn btn-primary">
</div>
</div>
</form>
</div>
</div>
#endsection
controller1.php
public function edit($id)
{
$technicien=technicien::find($id);
$user = $technicien->user;
return view('technicien.edit',['moyenne_avis'=>$technicien],
['actif'=>$technicien],['user_id'=>$technicien])->with('user',$user);
}
controller2.php
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
$technicien->user->update($request->get('user'));
return redirect('technicien');
}
You are passing too many param and in your route you only take one param!
I think you only need one param in your view which is technician ID and which will find the record in your controller query!
You can try follow code with removing unusable params from form:
<form action="{{ route('technicien.update', $moyenne_avis->id ) }}" method="POST">
And your route looks like:
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::post('/technicien/{id}', 'TechnicienController#update')->name('technicien.update');
Hope this helps you!

Resources