So I'm printing user complaints in table where I'm also printing a Delete button with every row. When I click that delete button, I want to delete that specific complaint from the table. I'm not using Resource Controller for this but a Basic Controller. Now, this is my code:
ViewComplaint.blade.php (Complaints Table with Delete Button):
<table id="cTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>Student Name</th>
<th>Complaint Title</th>
<th>Complaint Description</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($complaints as $complaint)
<tr>
<td>{{ $complaint->name }}</td>
<td>{{ $complaint->cname }}</td>
<td>{{ $complaint->cbody }}</td>
<td class="btn-group">
{!! Form::open(array('route'=>['complaint.destroy',$complaint->id],'method'=>'DELETE')) !!}
{!! Form::submit('Delete',['type'=>'submit','style'=>'border-radius: 0px;','class'=>'btn btn-danger btn-sm',$complaint->id]) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
Web.php (Routes):
Route::get('/complaint/create','ComplaintController#create')->name('complaint.create');
Route::post('/complaint','ComplaintController#store')->name('complaint.store');
Route::get('/complaint','ComplaintController#index')->name('complaint.index');
Route::delete('/complaint/{$complaint->id}','ComplaintController#destroy')->name('complaint.destroy');
ComplaintController.php (Basic Controller):
class ComplaintController extends Controller
{
public function index() {
$complaints = Complaint::all();
return view('viewcomplaint',compact('complaints'));
}
public function create(User $user) {
$user = User::all();
$user->name = Auth::user()->name;
return view('createcomplaint',compact('user'));
}
public function store(Request $request, Complaint $complaint, User $user) {
$user = User::find($user);
$complaint->name = Auth::user()->name;
$complaint->cname = $request->input('cname');
$complaint->cbody = $request->input('cbody');
//update whichever fields you need to be updated
$complaint->save();
return redirect()->route('home.index');
}
public function destroy(Complaint $complaint,$id)
{
$complaint = Complaint::findOrFail($complaint->id);
$complaint->delete();
return redirect()->route('complaint.index');
}
}
Now when I click the Delete button on the table, it just gives me "404 | Not Found" error. What am I doing wrong here? I would really appreciate some help.
remove the $id from the route
Route::delete('/complain/{id}','ComplaintController#destroy')->name('complaint.destroy');
public function destroy($id) {
}
The route parameter is just a name; you are saying this particular route segment is dynamic and I want the parameter named complaint:
Route::delete('complaint/{complaint}', 'ComplaintController#destroy')->name('complaint.destroy');
Then you can adjust your destroy method to take the parameter complaint typehinted as Complaint $complaint to get the implicit binding:
public function destroy(Complaint $complaint)
{
$complaint->delete();
return redirect()->route('complaint.index');
}
Seems to me you're defining your route wrong. Change your route to:
Route::delete('/complaint/{id}','ComplaintController#destroy')->name('complaint.destroy');
You don't need an array() in your form opening, so hange your form opening to this:
{!! Form::open(['method' => 'DELETE', 'route' => ['complaint.destroy',$complaint->id]]) !!}
And remove the $complaint->id from your submit button, you don't need it there.
All you have to do now inside your function is to find Complaint that has the id you passed in your form:
public function destroy($id)
{
$complaint = Complaint::findOrFail($id);
$complaint->delete();
return redirect()->route('complaint.index');
}
Let me know if you stumble on any errors.
Related
view page:
#if(count($applications)>0)
<table class= "table table-striped">
<tr>
<th>Email</th>
<th>Date Applied</th>
<th>Leave Type</th>
<th>Leave Status</th>
<th></th>
</tr>
#foreach($applications as $application )
<tr>
<td>{{$application ->user->email}}</td>
<td>{{$application ->dateApplied}}</td>
<td>{{$application ->leaveType}}</td>
<td>{{$application ->leaveStatus}}</td>
<td>
{!!Form::open(['action'=>['ApplicationAdminController#destroy', $application ->appId], 'method'=>'DELETE'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
</td>
</tr>
#endforeach
</table>
controller:
public function destroy($appId)
{
$application = Application::find( $appId);
$application ->delete();
$this->deleteLeave($appId);
return redirect('/applicationAdminHistory')->with ('success', 'Application Removed');
}
public function deleteLeave($appId){
$user_id = Auth::id();
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType,$day);
}
What I want to do is when click delete button the application will deleted and will do increments at the leave_summaries table. The increment at the leave_summaries based on the value of day and leave type in the application that has been deleted. After i run this code i got undefined variable error as shown below:
In this Code Please Pass $user_id , $leaveType ,$day:
public function deleteLeave($appId){
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType,$day);
}
You didn't defined these three variables : $user_id, $leaveType, $day
You need to pass these values as parameters into the deleteLeave() method or these values had to be defined before the method called.
public function deleteLeave($user_id, $leaveType, $day){
$req = DB::table('leave_summaries')
->where('user_id',$user_id)
->increment($leaveType, $day);
}
I want to send my data from controller to xedit.blade.php, but I get the same error:
Undefined variable: users
in controller:
public function index3()
{
$users=User::all();
return view('xedit')->with('users' => $users);
}
Routes:
Route::get('/index3','Admin\UsersController#index3');
and I want to use $users in blade.Maybe there is a Route problem?
in your index method
public funtion index()
{
$users=User::all();
return view('xedit', compact('users'));
}
in your view add $users
<table>
#foreach ($users as $item)
<tr>
<td>{{ $item->id }}</td>
<td>{{ $item->name }}</td>
</tr>
#endforeach
</table>
Your code logic is perfect, I guess you have to use proper naming with your routes because of Laravel Standard.
Route::get('/admin/show','Admin\UsersController#index')-name('admin.show');
public function index()
{
$users = User::all();
return view('xedit')->with('users' => $users);
}
In view, blade use a professional approach like below
#isset($users)
... loop ...
#endisset()
check record before sending to view by using dump and die function dd($users);
Want to comment but doesn't have 50 reputation
Replace ('users' => $users); this with (['users' => $users]); as you are using =>
Hi I'm building a ticket system i made tables with heads as:
<table class="table">
<thead>
<tr>
<th>Category</th>
<th>Title</th>
<th>Status</th>
<th>Last Updated</th>
</tr>
</thead>
I'm having trouble calling category name in views:
#foreach ($tickets as $ticket)
#foreach ($categories as $category)
#if ($category->id === $ticket->category_id)
{{ $category->name }}
#endif
#endforeach
My #if statements seems to be wrong i thinks as i can pull all the category names but with #if code seems to be breaking and its showing me nothing.
My routes:
Route::get('my_tickets', 'TicketsController#userTickets');
My Controller Function:
public function userTickets()
{
$tickets = Ticket::where('user_id', Auth::user()->id)->paginate(10);
$categories = Category::all();
return view('tickets.user_tickets', compact('tickets', 'categories'));
}
Update:
My Category Model:
protected $fillable = ['name'];
public function tickets()
{
return $this->hasMany(Ticket::class);
}
&Ticket Model:
protected $fillable = [
'user_id', 'category_id', 'ticket_id', 'title', 'priority', 'message', 'status'
];
public function category()
{
return $this->belongsTo(Category::class);
}
I'm trying to follow this tutorial but i think I'm doing something wrong idk what.
https://scotch.io/tutorials/build-a-support-ticket-application-with-laravel-part-1
https://scotch.io/tutorials/build-a-support-ticket-application-with-laravel-part-2
I think that you are trying to do something like this:
#foreach ($tickets as $ticket)
{{ $ticket->category->name }}
#endforeach
If you made a relation between your classes with belongsTo and hasMany then you can access using $ticket->category->name. If it does not work perhaps you have to get tickets with:
$tickets = Ticket::with('category')->where('user_id', Auth::user()->id)->paginate(10);
You haven't really followed it through. See how it's done in the article
#foreach ($tickets as $ticket)
#foreach ($categories as $category)
#if ($category->id === $ticket->category_id)
{{ $category->name }}
#endif
#endforeach
$ticket->$category is not a valid syntax.
I'm receiving an array from my controller to my view to map it with the #foreach blade loop, but after an insertion into the database, when I want to redirect my app to the index blade document, this error appears:
This is my controller:
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return view('libros.index');
}
And this is my view:
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Titulo</th>
</tr>
</thead>
<tbody>
#foreach ($libros as $libro)
<tr>
<td>{{ $libro->id }}</td>
<td>{{ $libro->titulo }}</td>
</tr>
#endforeach
</tbody>
</table>
I expect that my controller method redirect send me to the view correctly, but everytime I enter a register to the database and the controller redirects me to my blade view, this error appears and I have to reload the page to enter into the view.
In your store method you either have to redirect the user to the index page, or retrieve and pass all the libros array again.
The former way is preferred.
Assuming you have given a name to your route, you can do something like:
public function store(Request $request)
{
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
return redirect()->route('libros.index'); // replace libros.index with the index route name
}
I think it will work if you rewrite like this
public function store(Request $request)
{
//
$libro = new Libro;
$libro->titulo = $request->titulo;
$libro->save();
$libros = Libro::all()->get();
return view('libros.index', compact('libros'));
}
Got a ManyOnMany system (3 tables, projects, users, project_user)
Many users can work on a project, and a user can have many projects.
When checkbox = checked it sends the database to the pivot table.
Now I'm facing the problem that everytime I click the project/user id will get send to the project_user table.
And I need to have the checkbox already checked when the user is actually added to the project.
So how I see it: the form::checkbox has a third function checked or not checked, and with an if/else statement in my controller.edit I will have a validation somehow. Please help me!
Blade:
#foreach($users as $user)
<tr>
<td>
{{$user->firstname}} {{$user->middlename}} {{$user->lastname}}
</td>
<td>
{!! Form::checkbox('contribute['.$user->id.']', '1', $checkifinproject) !!}
</td>
</tr>
#endforeach
Controller:
public function edit($id, Project $project)
{
$users = User::all();
$project = $this->project->find($id);
if ($users == $project)
{
$checkifinproject = 'checked';
}
else {
}
return view('project.edit', ['project' => $project, 'id' => 'edit', 'project_id' => $id], compact('users'));
}
public function update(CreateProjectRequest $request)
{
if($request->get('contribute'))
{
foreach($request->get('contribute') as $k => $contribute)
{
if($contribute == 1)
{
$project = $this->project->find($request->project_id);
$project->users()->attach($k);
}
}
}
$project = $this->project->find($request->project_id);
$project->fill($request->input())->save();
return redirect('project');
}
Model:
User
public function projects()
{
return $this->belongsToMany('App\Project', 'project_user', 'user_id', 'project_id');
}
Project
public function users()
{
return $this->belongsToMany('App\User', 'project_user', 'project_id', 'user_id');
}
I think the issue is you are comparing two things that will never be the same and trying to determine if that user belongs to the project. A better thing to do would be to query all the users with their projects and as you loop through the users, check to see that the project you are modifying is one of the projects the user belongs to.
That can be done like this...
Controller
public function edit($id, Project $project)
{
$users = User::with('projects')->get();
$project = $this->project->find($id);
return view('projects.edit', ['users' => $users, 'project' => $project]);
}
View
#foreach($users as $user)
<tr>
<td>
{{$user->firstname}} {{$user->middlename}} {{$user->lastname}}
</td>
<td>
{!! Form::checkbox('contribute['.$user->id.']', '1', $user->projects->contains('id', $project->id)) !!}
</td>
</tr>
#endforeach