how making many submit button in one page laravel 8 - php

I have several buttons to insert data into the database, but only one command works, while the others don't work, what should I do, I will give an example of the data I created
this is my controller :
public function store(Request $request)
{
//
$days = $request->hari;
$hours = $request->jam;
$minutes = $request->menit;
$model = new Q_Topic_User;
$model->status = $request->status;
$model->topic_id = $request->topic_id;
$model->timer = Carbon::now()->addDays($days)->addHours($hours)->addMinute($minutes);
$topik = $request->topic_id;
$model->user_create = auth()->id();
// dd($request->all());
$model->save();
return redirect('qanswers/'. $topik);
}
public function storeAns(Request $request)
{
$modellama = Q_Answer::where('user_create', '=', $request->kode_user)
->Where('topic_id', '=', $request->topic_id)
->Where('question_id', '=', $request->question_id);
$a = $request->user_answer;
$b = $request->answer;
if ($a === $b){
$nilai = "1";
}
else{
$nilai = "0";
}
if ($modellama >= "1"){
$modellama->delete();
}
// dd($request->all());
$model = new Q_Answer;
$model->topic_id = $request->topic_id;
$model->question_id = $request->question_id;
$model->user_answer = $request->user_answer;
$model->answer = $request->answer;
$model->hasil = $nilai;
$model->user_create = auth()->id();
$topik = $request->topic_id;
$model->save();
return redirect('qanswers/'. $topik);
}
public function storeTot(Request $request)
{
$tot_nilai = Q_Answer::select(DB::raw("CAST(SUM(hasil) as int) as tot_nilai"))
->Where('topic_id', '=', $request->topic_idi)
->Where('user_create', '=', auth()->id())
->pluck('tot_nilai');
// dd($tot_nilai);
$tot_soal = Q_Question::select(DB::raw("CAST(COUNT(topic_id) as int) as tot_soal"))
->Where('topic_id', '=', $request->topic_idi)
->pluck('tot_soal');
dd($request->all());
$model = new Q_Answer_Tot;
$model->topic_id = $request->topic_idi;
$model->totalsoal = $tot_soal;
$model->totalnilai = $tot_nilai;
$model->user_create = auth()->id();
// dd($request->all());
$model->save();
$topik = $request->topic_id;
return redirect('qanswers/'. $topik);
}
blade :
<form method="POST" action="{{ url('qanswers') }}"
enctype="multipart/form-data">
#csrf
#foreach ($datas as $d_value)
<button type="submit" class="btn btn-sm bg-success">Kirim data</button>
#endforeach
</form>
<form method="POST" action="{{ url('qanswers.storeAns') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $q_value)
<button type="submit" class="btn btn-sm bg-success">Kirim Pilihan Pertanyaan</button>
#endforeach
</form>
<form method="POST" action="{{ url('qanswers.storeTot') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $t_value)
<button type="submit" class="btn btn-sm bg-success">Kirim Pilihan Peserta</button>
#endforeach
</form>
my web.php :
Route::resource('/qanswers', Q_AnswersController::class);
Route::post('/storeAns',[Q_AnswersController::class, 'storeAns'])->name('storeAns');
Route::post('/storeTot',[Q_AnswersController::class, 'storeTot'])->name('storeTot');
i can save data in store but i can save data using storeAns and storeTot. I dont find error in laravel log but i can see my data using dd($request->all()); can u help me?

Its good practice to give each form an unique id. With multiple forms and buttons, I would specify the form for the button. And you need to use value to figure out which button was pressed, especially when you are going to the same url in more than one form.
<form method="POST" id="answers" action="{{ url('qanswers') }}"
enctype="multipart/form-data">
#csrf
#foreach ($datas as $d_value)
<button type="submit" class="btn btn-sm bg-success" value="form1">Kirim data</button>
#endforeach
</form>
<form method="POST" id="storeans1" action="{{ url('qanswers.storeAns') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $q_value)
<button type="submit" class="btn btn-sm bg-success" value="form2">Kirim Pilihan Pertanyaan</button>
#endforeach
</form>
<form method="POST" id="storeans2" action="{{ url('qanswers.storeAns') }}"
enctype="multipart/form-data">
#csrf
#foreach ($questionss as $q_value)
<button type="submit" class="btn btn-sm bg-success" value="form3">Kirim Pilihan Pertanyaan</button>
#endforeach
</form>
I have shown the value with formx. SInce you are creating many buttons for each form, each should have an unique value such as
form1btn1
form1btn2
form1btn3

Related

Laravel route setting incorret and search function issues

This function search user from MySQL. Can anyone knows which part I did wrong ?, searchUser function is not triggered.
admin view
<form type="get" action="role-permission-search">
<input type="search" class="" name="query" placeholder="Search"/>
<button type="button" class="btn btn-outline-primary">search</button>
</form>
Route::group(['prefix'=>'admin', 'middleware'=>['isAdmin','auth']], function(){
// Serch users
Route::get('role-permission-search', [AdminController::class, 'searchUsers']);
});
function searchUsers()
{
dd('Where are you');
$search_request = $_GET['query'];
$users = User::where('name', 'LIKE', '%'.$search_request.'%')-get();
return view('dashboards.admins.rolePermission', compact('users'));
}
You can remove type="button" from below line
<button type="button" class="btn btn-outline-primary">search</button>
or change type="submit"
If still that not works for you then you should defile action="{{ route('YOUR ROUTE NAME') }}" in tag

How i can Delete image from users table in Laravel?

I have multiple fields in my users table in laravel but i want to delete only image from users table, Please let me know the process.
Here are my usercontroller.php file..
public function userprofile(Request $request)
{
$user = DB::table('users')->where('id',Auth::user()->id)->get();
//dd($user);
$userWish = DB::table('package_wishlist')->where('user_id',Auth::user()->id)->where('is_active',1)->count();
$userComp = DB::table('package_comparison')->where('user_id',Auth::user()->id)->where('is_active',1)->count();
$bookings = DB::table('agent_bookings')
->where('cust_email',Auth::user()->email)->get();
return view('user/profile',['bookings'=>$bookings,'user'=>$user[0],'userWish'=>$userWish,'userComp'=>$userComp]);
}
public function imagedestroy($id)
{
$image = DB::table('users')->where('id', $id)->get();
$file= $image->user_image;
$filename = public_path().'/uploads/user_uploads/'.$image;
File::delete($filename);
}
And here is my profile.blade.php file..
#foreach($user as $delimage)
<a onclick="event.preventDefault();
document.getElementById('delete-image-form').submit();"
class="btn btn-default btn-sm btn-block">Delete</a>
<form id="delete-image-form" method="POST" action="{{
route('delimage.imagedestroy', $delimage->id) }}"
style="display: none;">
#method('DELETE')
#csrf
</form>
#endforeach
And my web.php file is this...
Route::get('/userprofile', 'CustomHomeController#userprofile')->name('userprofile');
Route::get('/userprofile/{id}', 'CustomHomeController#imagedestroy')->name('imagedestroy');
use first() instead of get(). get() gives you a collection which you need to iterate to get an attribute. so you just can't get image of a user like
$image = DB::table('users')->where('id', $id)->get();
$file= $image->user_image; //this won't work.
Try like
public function imagedestroy($id)
{
$image = DB::table('users')->where('id', $id)->first();
$file= $image->user_image;
$filename = public_path().'/uploads/user_uploads/'.$image;
File::delete($filename);
}
And your userprofile method should be like
public function userprofile(Request $request)
{
$user = DB::table('users')->where('id',Auth::user()->id)->first();
//dd($user);
$userWish = DB::table('package_wishlist')->where('user_id',Auth::user()->id)->where('is_active',1)->count();
$userComp = DB::table('package_comparison')->where('user_id',Auth::user()->id)->where('is_active',1)->count();
$bookings = DB::table('agent_bookings')
->where('cust_email',Auth::user()->email)->get();
return view('user/profile',['bookings'=>$bookings,'user'=>$user,'userWish'=>$userWish,'userComp'=>$userComp]);
}
So you don't need to iterate in view. just use
<a onclick="event.preventDefault();
document.getElementById('delete-image-form').submit();"
class="btn btn-default btn-sm btn-block">Delete</a>
<form id="delete-image-form" method="POST" action="{{
route('imagedestroy', $user->id) }}"
style="display: none;">
#method('DELETE')
#csrf
</form>

Pass id from controller index to store - Laravel

This is my route :
Route::post('/store', 'EditlinkController#store');
My controller
public function index()
{
$id = $_GET['id'];
$links = DB::table('slugs')->where('slugs.id',$id)
->join('links','links.sid','=','slugs.id')
->get();
return view('editlink', ['links' => $links]);
}
public function store(Request $request, $id)
{
$url = $request->input('url');
$data =new link;
$sid = $id;
$data->sid = $sid;
$data ->url = $url;
$data ->save();
return redirect('/links');
}
And my view:
<form role="form" method='POST' action="{{url('store')}}">
<div class="entry input-group col-xs-3">
<input class="form-control" name="url" type='url' placeholder="https://..." size="100"/>
<input type="hidden" name="_Token" value="{{ csrf_token() }}">
<button type="submit" class="btn btn-primary" type="button">
<span class="material-icons md-12">add</span>
</button>
{{ csrf_field() }}
</div>
</form>
So basically here I want to call $id from index to store. I also have tried
Route::post('/store/{id}', 'EditlinkController#store');
But still doesn't work. Thank you.
Your route, /store/{id}, requires you to have a route parameter. Make sure you have an $id available to you before you generate your url for your form.
An example of what your open form tag should look like with the $id included:
<form role="form" method='POST' action="{{url('store', $id)}}">
I'm assuming the view editlink is where the markup for the form resides. If so, then you can simply pass the value of the id to your view from your controller:
return view('editlink', ['links' => $links, 'id' => $id]);

Laravel- arranging records in ascending and descending order

I have a create method in my controller
public function create()
{
$image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
foreach($image as $property)
{
$id = $property->property_id;
}
$image_main = Image::where('property_id', $id)->get();
return view('settings.photos', ['image_array' => $image_main]);
}
This is my photos.blade file
<form name="asc" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="submit" value="Ascending " class="settings-photos-header2 text-center"/> |
</form><form name="dec" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="submit" value= " Descending" class="settings-photos-header2 text-center"/>
</form>
<h2 class="settings-photos-header2 text-center">Photo Gallery</h2>
#foreach ($image_array as $images)
<div class="image-warp"><img src="{{$images->filename}}"
style="width:100px;height:100px;"><br/><span style="color: #1b1e21">{{$images->description}}</span>
</form>
</div>
#endforeach
Question- How can i make the asc button sort the images in ascending order and des in descending order, is there a way to connect it to my controller, or is there a way to sort them in ascending and descending order through any other means?
There are many ways you can achieve the sorting issue but as you have setup your page let me tell you the way you can sort records. Create another action which point to your settings.photos route
public function settings_photos($property_id) {
$form = Input::get('submit');
$orderBy = $form == "Ascending" ? "asc" : "desc";
# Fetch Images in of Specific Property
$list_of_images = Image::where('property_id', $property_id)
# Order by Asc/Desc
->sortBy('id', $orderBy)->get();
return view('settings.photos', ['image_array' => $list_of_images]); }
Now add Image->id to pass in your controller action by form
https://stackoverflow.com/questions/50432659/laravel-arranging-records-in-ascending-and-descending-order#
<form name="asc" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="image_id" value="{{$id}}" />
<input type="submit" value="Ascending " class="settings-photos-header2 text-center"/> |
</form>
<form name="dec" action="{{route("settings.photos")}}" method="post" class="text-center">
#csrf
<input type="image_id" value="{{$id}}" />
<input type="submit" value="Descending" class="settings-photos-header2 text-center"/>
</form>
Now make some changes to your previous Controller code
public function create()
{
$image = PropertyUser::where('user_id', '=', Auth::user()->id)->get();
foreach($image as $property)
{
$id = $property->property_id;
}
$image_main = Image::where('property_id', $id)->get();
return view('settings.photos', ['image_array' => $image_main, 'id' => $id]);
}
As I told you there are various way to sort records even by Ajax but I suggest you using your code example. I also did not optimize the code.
Let me know if you have any question.
Thanks

laravel deleting user (cant get the id of the user)

I'm new to laravel and I'm having an issue deleting a user.
I cannot get the id of the user I wish to delete.
Any help is appreciated.
view
<form method="post" action="/staff/{{$user->id}}">
<input type="hidden" name="_method" value="DELETE">
{{csrf_field()}}
<button style="padding: 0" type="submit" class="btn btn-link margin-left-40"
onclick="return confirm('Are you sure you want to delete {{ucfirst($user->name)}}?');">
<i class="icmn-bin"></i> Delete</button>
</form>
controller
public function destroy(User $user)
{
$thisuser = User::find($user->id);
$thisuser->delete();
return redirect('/staff');
}
route
Route::resource('/staff', 'User\UserController');
Please try
public function destroy($id)
{
$thisuser = User::find($id);
$thisuser->delete();
return redirect('/staff');
}
<form method="post" action="{{route('staff.destroy',$user->id)}}">
<input type="hidden" name="_method" value="DELETE">
{{csrf_field()}}
<button style="padding: 0" type="submit" class="btn btn-link margin-left-40"
onclick="return confirm('Are you sure you want to delete
{{ucfirst($user->name)}}?');">
<i class="icmn-bin"></i> Delete</button>
</form>
//your route
Route::get('/staff/{id}/delete', 'User\UserController#destroy')->name('staff.destroy');
//method
public function destroy($id)
{
$thisuser = User::find($id);
$thisuser->delete();
return redirect('/staff');
}
In addition to Kuldeep Mishra's solution, make sure your form is properly bound to the user you intend to delete.
You should try this:
public function destroy($id)
{
//Soft delete
$thisuser = User::destroy($id);
//OR
//Permanent Delete
$thisuser = User::where('id',$id)->delete();
return redirect('/staff');
}
html
<form method="post" action="/staff/{{$user->id}}">
<input type="hidden" name="_method" value="DELETE">
{{csrf_field()}}
<button style="padding: 0" type="submit" class="btn btn-link margin-left-
40" onclick="return confirm('Are you sure you want to delete
{{ucfirst($user->name)}}?');">
<i class="icmn-bin"></i> Delete</button>
</form>
route
Route::delete('/staff/{id}', 'UserController#destroy');
function
public function destroy($id)
{
$thisuser = User::find($id);
$thisuser->delete();
return redirect('/staff');
}

Categories