Laravel: Edit Data on Same Page it's Displayed on - laravel

I'd like to make changes to user data on the same page it's been displayed.
Basically, I want to either select verified or rejected, click submit button and the page refreshes with the change.
So i have this code:
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Receipt</th>
<th scope="col">Status</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<form method='POST' action="{{ action('PaymentController#update' , $user->id )}}"
enctype="multipart/form-data">
#csrf
#method('PUT')
<th scope="row">{{$user->id}}</th>
<td>{{$user->name}}</td>
<td>
{{$user->receipt}}
</td>
#if( $user->payment_status != NULL )
<td>
PAYMENT VERIFIED
</td>
#else
<td>
<div class="form-group">
<select multiple class="form-control-sm" id="exampleFormControlSelect2" name="nsap_reg"
required="">
<option value="verified">Verified</option>
<option value="NULL">Rejected</option>
</select>
</div>
</td>
#endif
<td>
<input type="submit" value="SUBMIT">
</td>
</form>
</tr>
#endforeach
</tbody>
</table>
Controller:
public function update(Request $request, $id)
{
$request->validate([
'nsap_reg' => 'required',
]);
$payment_status = \App\User::find($id);
['nsap_reg' => $request->nsap_reg];
$payment_status->save();
return redirect('user-status');
}
My problem is nothing happens when I click submit, the page just refreshes.
Any help will be appreciated.

You just need to set the value of nsap_reg to user object and save it.
$payment_status = \App\User::find($id);
$payment_status->nsap_reg = $request->nsap_reg;
$payment_status->save();

As I can see you created select list with multiple select and when you are saving data to database then you sending array data to saving.
But you have to firstly convert that to json and then set to save.
Below is the example to save multi-select nsap_reg.
<?php
public function update(Request $request, $id)
{
$request->validate([
'nsap_reg.*' => 'required',
]);
$payment_status = \App\User::find($id);
$payment_status->nsap_reg = json_encode($request->input('nsap_reg'));
$payment_status->save();
return redirect('user-status');
}
But if you are using single select then remove multiple attribute from the select list and then use this code
<?php
public function update(Request $request, $id)
{
$request->validate([
'nsap_reg' => 'required',
]);
$payment_status = \App\User::find($id);
$payment_status->nsap_reg = $request->input('nsap_reg');
$payment_status->save();
return redirect('user-status');
}

Related

Returning wrong id in laravel

Departemen =
'nama_departemen',
'nama_manager',
'jumlah_pegawai',
Pegawai = 'nomor_induk_pegawai',
'nama_pegawai',
'id_departemen',
'email',
'telepon',
'gender',
'status'
PegawaiController
public function index()
{
$pegawai = Pegawai::Join('departemens','pegawais.id_departemen','=','departemens.id')->paginate(5);
return view ('pegawai.index', compact('pegawai'));
}
public function destroy($id)
{
Pegawai::find($id)->delete();
return redirect()->route('pegawai.index')->with(['success'=> 'Item Berhasil Dihapus']);
}
public function edit($id)
{
$pegawai=Pegawai::join('departemens','pegawais.id_departemen','=','departemens.id')->find($id);
$departemen = Departemen::all();
return view('pegawai.edit',compact('pegawai','departemen'));
}
pegawai.index
#forelse ($pegawai as $item)
<tr>
<td class="text-center">{{ $item->nomor_induk_pegawai }}</td>
<td class="text-center">{{ $item->nama_pegawai }}</td>
<td class="text-center">{{ $item->nama_departemen }}</td>
<td class="text-center">{{ $item->email }}</td>
<td class="text-center">{{ $item->telepon }}</td>
#if($item->gender==0)
<td>Wanita</td>
#else
<td>Pria</td>
#endif
#if($item->status==0)
<td>Inactive</td>
#else
<td>Active</td>
#endif
<td class="text-center">
<form onsubmit="return confirm('Apakah Anda Yakin ?');" action="{{ route('pegawai.destroy', $item->id ) }}" method="POST">
Edit
#csrf
#method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Hapus</button>
</form>
</td>
</tr>
#empty
<div class="alert alert-danger">
Data Pegawai belum tersedia
</div>
#endforelse
routes.web
Route::get('/', function () {
return view('dashboard');
});
Route::resource('/departemen',\App\Http\Controllers\DepartemenController::class);
Route::resource('/pegawai',\App\Http\Controllers\PegawaiController::class);
so i tried the Hapus and Edit button. it returning id_departemen instead id from table pegawai. i assume this is because im using join. i use join because i want to show nama_departemen from table departemen in pegawai. so what should i do to fix this.
maybe table departemen should look like this
'id', 'nama_departemen', 'nama_manager', 'jumlah_pegawai'
and table pegawai should look like this :
'id','nomor_induk_pegawai', 'nama_pegawai', 'id_departemen', 'email', 'telepon', 'gender', 'status'
and the controller should look like this :
Pegawai::select ('pegawai.nomor_induk_pegawai','pegawai.nama_pegawai','departemen.nama_departemen','pegawai.email','pegawai.telepon','pegawai.gender','pegawai.status','pegawai.id as id_pegawai','departemen.id as id_departemen')->Join('departemens','pegawais.id_departemen','=','departemens.id')->paginate(5);
just choose which do you want to use, id_pegawai or id_departemen
you can change the join with leftjoin if you need
read this documention for another resolved
https://laravel.com/docs/9.x/queries#select-statements
The issue here is that your are joining two tables that both have an ID column.
To get around this you need to be specific in what columns you want to return.
For example;
$pegawai = Pegawai::Join('departemens','pegawais.id_departemen','=','departemens.id')->select('pegawais.id','departemens.otherColumnName')->paginate(5);
Hope that helps.

Button in table row redirect to another page with the data of the row in Laravel

I would like to insert a button in every row of a table and when I click on this button, it redirect me to another page with the data of the single table row using Laravel
How can I do this?
This is my form:
<html>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">USERNAME</th>
<th scope="col">MAC ADDRESS</th>
</tr>
</thead>
<tbody>
#foreach ($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->username}}</td>
<td>{{$item->mac_addr}}</td>
<td>
<form action="{{url('singleDevice')}}" method="get">
<button class="btn btn-primary" type="submit">Select</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</body>
</html>
This is my controller:
class DeviceController extends Controller
{
public function index()
{
$data=Device::all();
return view('device', compact("data"));
}
public function create()
{
return view('registrationDevice');
}
public function store(Request $request)
{
$input = new Device;
//On left field name in DB and on right field name in Form/view
$input -> username = $request->username;
$input -> mac_addr = $request->mac_address;
$input->save();
return redirect('registrationDevice')->with('message', 'DATA SAVED');
}
public function show(Device $device)
{
return view('singleDevice');
}
}
Thanks in advance
Change your form like :
<tbody>
#foreach ($data as $item)
<tr>
<th scope="row">{{$item->id}}</th>
<td>{{$item->username}}</td>
<td>{{$item->mac_addr}}</td>
<td>
Select
</td>
</tr>
#endforeach
</tbody>
If you want to use route name, you can change by :
<td>
Select
</td>
Change your route like :
Route::get('/singleDevice/{deviceID}', [DeviceController::class, 'show'])->name('show');
Change your show function of the controller like:
public function show($deviceID)
{
$device = Device::firstWhere('id', $deviceID);
return view('singleDevice', compact("device"));
}
Why do you need a form? use the link
<form action="{{url('singleDevice')}}" method="get">
<button class="btn btn-primary" type="submit">Select</button>
</form>
replace with
Select
and configure the URL in the router as /singleDevice/{device}
Route::get('/singleDevice/{device}', [DeviceController::class, 'show'])->name('show');
You also can use this as you have already used form in your code
<form action="{{ route('show', $item->id) }}" method="get">
<button class="btn btn-primary" type="submit">Select</button>
</form>
public function show(Device $device)
{
return view('singleDevice');
}
For Route you can pass $device:
Route::get('/singleDevice/{$device}', [DeviceController::class, 'show'])->name('show');

Laravel - How to Approve all the posts of a particular Employee

Using Laravel-5.8, I have written a code for post of employees and it is working perfectly:
Controller
public function index()
{
$userEmployee = Auth::user()->employee_id;
$posts = Post::latest()->where('employee_id', $userEmployee)->get();
return view('admin.post.index', compact('posts'));
}
And it generates this view shown below. The view loads all the unapproved posts for a particular employee:
<table class="table table-bordered table-striped table-hover dataTable js-exportable">
<thead>
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th><i class="material-icons">visibility</i></th>
<th>Is Approved</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($posts as $key=>$post)
<tr>
<td>{{ $key + 1 }}</td>
<td>{{ str_limit($post->title,'10') }}</td>
<td>{{ $post->user->name }}</td>
<td>{{ $post->view_count }}</td>
<td>
#if($post->is_approved == true)
<span class="badge bg-blue">Approved</span>
#else
<span class="badge bg-pink">Pending</span>
#endif
</td>
<td>
#if($post->status == true)
<span class="badge bg-blue">Published</span>
#else
<span class="badge bg-pink">Pending</span>
#endif
</td>
</tr>
#endforeach
</tbody>
<form action="{{route('admin.post.approve', $post->id)}}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div>
<button type="submit" class="btn btn-primary"><i class="fas fa-arrow-right"></i> {{ trans('global.submit') }}</button>
</div>
</form>
I want to add a submit button to the view called approve. Once the button is clicked, it checks where is_approved is = 0 for the loaded employee and turns all the is_approved that relates to the employee (the loaded) data to 1.
I have written this function in the same controller, but I see that it will only work for a selected row:
public function approve($id){
$post = Post::find($id);
if ($post->is_approved == false){
$post->is_approved = true;
$post->save();
$post->user->notify(new AuthorPostApprove($post));
Toastr::success('Post Successfully Approved');
}else{
Toastr::info('Post is already Approved');
}
return redirect()->back();
}
and have this route/web.php
Route::get('posts/', 'PostController#index')->name('post.index');
Route::put('/post/{id}/approve', 'PostController#approve')->name('post.approve');
How do I re-write my Controller, view and route to achieve this?
Thank you.
try this:
public function approve($id){
$post = Post::where('employee_id', $id)
->where('is_approved', 0)
->update(['is_approved' => 1]);
if ($post){
$post->user->notify(new AuthorPostApprove($post));
Toastr::success('Post Successfully Approved');
} else {
Toastr::info('Post is already Approved');
}
return redirect()->back();
}
Ok I do something like below
All you need to do is that when user click the button then it should call for a function where that function will update all unapproved posts. So Im implementing it as below.
First I load all the unapproved post of the current logged in user . With just adding extra where clause in to your index function
Controoler for all unapproved posts
public function index()
{ //Fetching all unapproved post and passing it into view
$userEmployee = Auth::user()->employee_id;
$posts = Post::latest()->where('employee_id', $userEmployee)->where('is_approved',0)->get();
return view('admin.post.index', compact('posts'));
}
Defining the route
Define a route like below first and that route will call for the function named as approve_all_posts in PostController .
Route::post('/post/approve_all_posts', 'PostController#approve_all_posts')->name('post.approve.all');
New function for update
Then I make the function approve_all_posts () in PostController
public function approve_all_posts(){
$unapproved_post = Post::where('is_approved',0)->update(['is_approved' => 1]);
return redirect()->back();
}
Update blade file with button
Then in admin/post/index.blade.php
I add a button called approve all posts like below
Approve all unapproved posts

data is not submitting into db:laravel-5.8

I'm new to laravel and trying to save data into db but nothing happens. I just want to save it into db.
controller:
public function store(Request $request)
{
$request->validate([
'date' => 'required',
'start_time' => 'required',
'end_time' => 'required',
'time_slot' => 'required',
]);
Form::create(request(['date', 'start_time', 'end_time', 'time_slot']));
$data->save();
return view('form.index');
}
blade file:
<table class="table table-hover mt-5">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Start Time</th>
<th scope="col">End Time</th>
<th scope="col">Time Slice</th>
</tr>
</thead>
<tbody>
<tr>
<th>
<input type="date" name="date">
</th>
<td>
<input type="text" id="datetimepicker" name="start_time" />
</td>
<td>
<input type="text" id="datetimepicker" name="end_time" />
</td>
<td>
<select name="time_slot">
<option value="1">15 min</option>
<option value="2">30 min</option>
<option value="3">45 min</option>
<option value="4">1 hr</option>
</select>
</td>
</tr>
</tbody>
</table>
<button type="submit" type="button" class="btn btn-primary">Add Data</button>
I'm using resource route for it.
you have 2 options to save it in database:
1---------------------------
public function store(Request $request)
{
$request->validate([
'date' => 'required',
'start_time' => 'required',
'end_time' => 'required',
'time_slot' => 'required',
]);
$form = new Form(); //make new object from your model
$form->date = $request['date']; //each of the DB field fill with $request fields
$form->start_time = $request['start_time'];
$form->end_time = $request['end_time'];
$form->time_slot = $request'time_slot'];
$form->save(); //Save this to your DB
return view('form.index');
}
2-------------------------------Recomended
in this way you need to do something in your model
Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Form extends Model
{
protected $table = "forms"; //table name in your DB
protected $fillable = ['date','start_date','end_date','time_slot']; //your fields in DB table that you want to fill them
}
Controller
public function store(Request $request)
{
$request->validate([
'date' => 'required',
'start_time' => 'required',
'end_time' => 'required',
'time_slot' => 'required',
]);
$form = new Form(); //make new object from your model
$form->create($request->all()); //NOTE:your fields in view should be the same name with your fields in DB, i mean for example you should get the start_date from an input that it's name is start_date, <input name="start_date">
return view('form.index');
}
i hope this helps you !
public function store(Request $request)
{
$request->validate([
'date' => 'required',
'start_time' => 'required',
'end_time' => 'required',
'time_slot' => 'required',
]);
Form::create($request->all());
return view('form.index');
}
blade file:
<form method="post" action="{{ route('form.store' }}">
{{ csrf_field() }}
<table class="table table-hover mt-5">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Start Time</th>
<th scope="col">End Time</th>
<th scope="col">Time Slice</th>
</tr>
</thead>
<tbody>
<tr>
<th>
<input type="date" name="date">
</th>
<td>
<input type="text" id="datetimepicker" name="start_time" />
</td>
<td>
<input type="text" id="datetimepicker" name="end_time" />
</td>
<td>
<select name="time_slot">
<option value="1">15 min</option>
<option value="2">30 min</option>
<option value="3">45 min</option>
<option value="4">1 hr</option>
</select>
</td>
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-primary">Add Data</button>
</form>

How to pass data using checkbox from view to controller laravel 5.2

The interface that showed is using #foreach method to display . Hence , i want to know how to pass only one checkbox (view) into controller.
Below is the view coding.
#foreach($articles as $article)
<tr>
<td>{{ $article->jenis_simptom}}</td>
<td style="width: 10px">
<input type="checkbox" value="{{ $article->jenis_simptom}}" name="{{ $article->id }">
</td>
</tr>
#endforeach
Thank You
Here is the dummy interface
Use articles[] as html field name with $article->id as a key
#foreach($articles as $article)
<tr>
<td>{{ $article->jenis_simptom}}</td>
<td style="width: 10px">
<input type="checkbox" value="{{ $article->jenis_simptom}}" name="articles[{{ $article->id }]">
</td>
</tr>
#endforeach
In your controller you'll be able to loop thru submitted array which will contain only elements that were checked in your html
foreach ($request->input('articles') as $key => $value) {
// $key will contain your article id
}
You can use Laravel Collective forms. If you're using Laravel 4, it works out of the box, for Laravel 5 you should install the package.
{!! Form::open(array('action' => 'Controller#store')) !!}
{!! Form::checkbox('name', 'value') !!}
{!! Form::submit('Send') !!}
{!! Form::close() !!}
When user submits the form, store action of Controller catches all form data, including checked checkboxes and puts it in $request variable.
public function store(Request $request)
{
dd($request);

Resources