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
Related
I tried to delete some file of my database from the aplication using a destroy function but doesn't work, it works with routes that are simples but no composed,its for school
Below i will let the code of my route, the destroy function() , how i tried to invocate the function and my index function()
Give me the Error:Missing required parameters for [Route: asociados.destroy] [URI: eventos/{eventos}/miembros/{miembros}/asociados/{asociado}]
Route
Route::resource('/eventos/{eventos}/miembros/{miembros}/asociados', 'miembroController');
Destroy function()
public function destroy($id)
{
$miembro=Miembro::find($id);
$miembro->delete();
return back()->with('Evento eliminado');
}
* Index function()*
public function index(Request $request, $id_evento,$id_miembro){
$miembros = DB::select(DB::raw(
"SELECT id_miembro, razon_social, denominacion_comercial, web,
rif
FROM miembro
" ));
return view ('home.miembro')->with('miembros', $miembros)->with('id_evento', $id_evento)->with('id_miembro', $id_miembro);
}
Code in the html of how i tried to invocate the destroy function()
<td>
<form action={{ route('asociados.destroy', ['miembro' => $item->id_miembro]) }}
method="POST" class="d-inline">
#csrf
#method('DELETE')
<button class="btn btn-dark btn-sm" type="submit">Eliminar</button>
</form>
</td>
You have to give to the route helper also the event id:
<td>
<form action={{ route('asociados.destroy', ['eventos'=> $id_evento ,'miembro' => $item->id_miembro]) }}
method="POST" class="d-inline">
#csrf
#method('DELETE')
<button class="btn btn-dark btn-sm" type="submit">Eliminar</button>
</form>
</td>
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]);
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');
}
I want To Store the current user $emp->id from href , and use in the input value as like in my code that is written below. Is is possible? or if possible then please help me? and if this Question is not a big problem so i am sorry for that in advance.
<a href="{{'/employee'}}?id={{$emp->id}}" type="button" name="user_id" class="btn btn-primary" data-toggle="modal" data-target="#myModal">
Apply Attribute
</a>
<form action="{{'/rating'}}" method="post">
{{csrf_field()}}
<input type="hidden" name="user_id" value="{{store here current user}}">
</form>
Seems you just need paste your current user inside INPUT:
<form action="{{'/rating'}}" method="post">
{{csrf_field()}}
<input type="hidden" name="user_id" value="{{ $emp->id }}">
</form>
.....
If you want to use an id from a url you can :
Route::get('/url/{emp}', 'YourController#method');
In your controller:
public function method(Employee $emp) {
//Your code
return view('youre.view', compact('emp'))
}
Now in your view you have $emp and can acces id like $emp->id.
Offcourse name it what you want but be sure to name it the same in your route,controller and view.
Now you don't need a forEach loop, because of the binding you already have the employee from the url
p.s: Employee model is just an assumption..name it whatever your model is called.
You can use route function in blade for this.
Try this on web.php :
Route::get('/employee/{id}', 'YourController#YourMethod')->name('routename');
on your Controller your method need to have an arguments
public function YourMethod($id){
// Code here
}
And on your blade make your href with route
Link
Href Like That:
<a href="{{$emp->id}}" type="button" id="uu_id" class="btn btn-primary uu"
data-toggle="modal" data-target="#myModal"> Apply Attribute </a>
Using This Script To get Value from Href:
<script type="text/javascript">
$(document).ready(function() {
$(".uu").click(function(event) {
var u_id = $(this).attr('href');
event.preventDefault();
document.getElementById("hiddenVal").value = u_id;
});
});
</script>
And in your Form like this:
<form action="{{'/rating'}}" method="post">
{{csrf_field()}}
<input type="submit" style="margin-bottom: 10px;" class="btn btn-success
pull-right" name="apply" value="Apply"/>
<input type="hidden" name="hiddenVal" id="hiddenVal" />
</form>
And Last How to get this Value in the Controller And save to Database:
public function store(Request $request)
{
$rates = new Rating;
$user_id = $_POST['hiddenVal'];
$rates->user_id = $user_id;
$rates->save();
return redirect('/employee');
}
I create form in Laravel:
<form action="/redeem" method="get" class="sidebar-form">
<div class="input-group">
<input type="text" name="key" class="form-control" placeholder="ENTER VOUCHER CODE">
<span class="input-group-btn">
<button type="submit" name="search" id="search-btn" class="btn btn-flat"><i class="fa fa-search"></i>
</button>
</span>
</div>
</form>
and now when I try to submit that I get:
http://localhost:8888/redeem?key=NBtGJ5pZls&search=
but I need to get just:
http://localhost:8888/redeem/NBtGJ5pZls
also in route I have:
Route::get('/redeem/{key}', 'OrdersController#redeem');
How to get redirected to redeem/NBtGJ5pZls with my form?
Change your route to:
Route::post('/redeem', 'OrdersController#redeem');
And then get key in controller:
public function redeem(Request $request)
{
$key = $request->key;
And finally, change your form:
<form action="/redeem" method="post" class="sidebar-form">
{{ csrf_field() }}
You've got 2 options:
The first is let your frontend code be the way it is, update the route, and work with the GET parameter.
The second one is using some javascript to rewrite the URL you want to access.
For example this (it's using jQuery):
$('#search-btn').click(function() {
var key = $('input[name=key]').val();
window.location.href = '/redeem/' + key;
});
I prefer the first one, because javascript can be modified by the end-user.