Edit button not working perfectly on Laraver resource controller - laravel

I am using Laravel resource routing, all coding are ok, but when I am trying to press on the edit button from index page, its getting the url: localhost/8000/categories/id/edit but not uploading my edit.blade.php file. The loaded page is showing 404, not found.
index.blade.php
<div class = "span2">
<a href="{{url('/categories/'.$category->id.'/edit')}}">
<i class="halflings-icon white edit"></i>
</a>
</div>
CategoryController.php
public function edit(Category $category)
{
return view('admin.category.edit',compact('category'));
}
web.php
Route::resource('/categories/',CategoryController::class);

Related

Getting id value from url in controller and displaying values associated laravel

I'm trying to create a ticket management system with laravel jetstream and livewire. In the user page there's a table with all tickets that the user created. When the user clicks on the button to open one specific ticket, it should pass the id of the ticket and redirect to another page where it receives the data of that ticket he clicked, like title, message, etc..
The id is passed through the url, but my main problem is that whenever I try to display that data in the view, nothing shows, no errors either. I think that something might be wrong with my controller.
Here's my route:
Route::get('tickets.answers/{id}', [TicketsController::class, 'answers']);
The button to redirect to that specific ticket:
<a href="{{ url('tickets.answers' . '/'. $ticket->id ) }}" > <x-jet-secondary-button >
See Answer
</x-jet-secondary-button></a>
AnswersController:
public function render(Request $request)
{
$tickets = Ticket::where('id', $request->url('id'));
return view('livewire.tickets.answers', [
'tickets' => $tickets,
]);
}
And how I'm trying to display in my blade:
#foreach($tickets as $key => $ticket)
<!-- This example requires Tailwind CSS v2.0+ -->
<div class="bg-white shadow overflow-hidden sm:rounded-lg">
<div class="px-4 py-5 sm:px-6">
<h3 class="text-lg leading-6 font-medium text-gray-900">
Ticket nÂș {{$ticket->id}} - {{$ticket->title}}
</h3>
</div>
</div>
#endforeach
In your TicketsController you can fetch the id like this
public function answer(Request $request, int $id)
{
// Use the find() method, instead of where(), when searching for the primary key
$tickets = Ticket::find($id);
// .. more
}
In your routes files you specify an answer method, so use this in your TicketsController.
// See how to name a route
Route::get('tickets.answers/{id}', [TicketsController::class, 'answers'])->name('tickets.answers');
Then use the named route in your view like this:
<a href="{{ route('tickets.answers', ['id' => $ticket->id]) }}">
You can see a similar example in the Laravel docs.

multiple variables in routes. A route isn't leading to the controller defined blade

I have "show" and "edit" two routes. It's showing two different url. But "edit" route is using the 'show' blade, which is the SAME as the "show" route. How to lead the "edit" route to the 'edit' blade?
here is the web.php:
Route::get('/{user}/{course}', 'CoursesController#show')->name('course.show');
Route::get('/{user}/edit_{course}', 'CoursesController#edit')->name('course.edit');
here is the controller:
public function edit(Course $course) {
return view('courses.edit', compact('course'));
}
public function show(Course $course) {
return view('courses.show', compact('course'));
}
here is the index blade:
<a class="btn btn-xs btn-primary" href="{{ route("course.show", [auth()->user()->username, $course->title. $course->id] ) }}"> VIEW </a>
<a class="btn btn-xs btn-info" href="{{ route("course.edit", [auth()->user()->username, $course->title. $course->id]) }}"> EDIT </a>
Change the order or your routes in your routes files web.php
Route::get('/{user}/edit_{course}', 'CoursesController#edit')->name('course.edit'); //This one goes first.
Route::get('/{user}/{course}', 'CoursesController#show')->name('course.show');
Your show route is working as a wildcard if you invert the order as shown the edit route will catch it first when the second variable begins with edit_.

Laravel returns 404 error when passing a parameter

Hello when I send a data press a link to show another view laravel returns a 404 error, what am I failing?, I have created other crud and I have had no problems so far
Expansion -> index.blade.php:
<a href="{{url('/cards/'.$expansion->exp_id.'/vcards')}}"class="btn btn-primary form-control" >Go!</a>
URL:
http://localhost/CardSystem/cartas/1/vcards
Route:
Route::resource('cards', 'cardControllerr');
cardController:
public function vcards($id){
$data['cards']= DB::table('cards')
->join('expansion','expansion.exp_id','=','cards.exp_id')
->select('cards.card_id','cards.card_nom','expansion.exp_nom')
->where('cards.exp_id','=',$id)
->orderBy('cards.card_num')
->paginate(5);
return view('cards.vcards',$data);
vcards.blade.php:
#extends('layouts.app')
#section('content')
<div class="container">
#foreach($cards as $card)
<div class="form-group">
<h5 class="card-title">{{$card->card_nom}}</h5>
</div>
#endforeach
</div>
#endsection
I think you misunderstood routing, views etc.
For your controller to be hit, you will need to make the following route.
Route::get('cartas/{card}/vcards', 'cardControllerr#vcards')->name('cartas.vcards');
If you wanna link to this route use route().
route('cartas.vcards', ['card' => $card]);

laravel - I try to delete row out of a table from the database with a href icon

So what I'm trying to do is delete a row from my database table with a icon button (href)
<span title="Delete Task"><i class="far fa-trash-alt"></i></span>
Here is my controller
public function destroy($idtask) {
DB::delete('delete from tasks where idtask = ?',[$idtask]);
return redirect('/todo/notstarted');
}
and here is my routing:
Route::get('notstarted/delete/{idtask}','NotstartedController#destroy');
so my button is on page /todo/notstarted , when i click it it goes to /todo/notstarted/delete/1 like I want but it shows the laravel 404|Not Found page. Normally the redirect in my controller should work but for some reason it broke.
here is a picture of my folder structure of my views:
enter image description here
Hopefully someone can help!
the problem is your anchor tag's href
you should use laravel url() helper.
like the code below :
<a href="{{url('notstarted/delete/',$task->idtask)}}" style="color: #8B0000">
<span title="Delete Task"><i class="far fa-trash-alt"></i></span>
</a>

Laravel 2 submit buttons in the same form

I am building a CRUD with Laravel. Each category hasMany attachments and each attachment belongsTo a category.
In the category.edit view I want to give the user the possibility of deleting the attachments (singularly) from the Category. I tried this method but it did not work:
Registering route for the attachment:
Route::group(['middleware' => ['auth']], function () {
Route::delete('attachment/{id}', 'AttachmentController#delete')->name('attachment');
});
Handling the delete building the AttachmentController#delete method:
class AttachmentController extends Controller
{
public function delete($id) {
$toDelete = Attachment::findOrFail($id);
$toDelete->delete();
return redirect()->back();
}
}
In the CategoryController (edit method), I fetch the attachments linked to each category to be rendered inside the view:
public function edit($category)
{
$wildcard = $category;
$category = Category::findOrFail($wildcard);
$attachments = App\Category::findOrFail($wildcard)->attachments()->get()->toArray();
return view('category.edit', [
'category' => $category,
'attachments' => $attachments
]);
}
In the view, I render the attachment and the button to delete. I am fully aware of the error of having a form inside another form, nevertheless I do not know antoher approach to submit this delete request.
// update Category form
#foreach ($attachments as $attachment)
<div class="row">
<div class="col-4">
<img style="width: 100%;" src={{ $attachment['url'] }} alt="">
</div>
<div class="col-4">
<div>
<p class="general_par general_url">{{ $attachment['url'] }}</p>
</div>
</div>
<div class="col-4">
<form action="{{ route('attachment', $attachment['id']) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete Image</button>
</form>
</div>
</div>
<hr>
#endforeach
// end of update Category form
Should I build a deleteAttachment method inside the CategoryController? If so, how can I still submit the Delete request? Also, if any other Model in the future will have attachments, should I build a deleteAttachment method inside each controller? That is cumbersome. Thanks in advance
if you don't like to use form, then use tag:
<a class="btn btn-danger" href="{{ route('attachment', $attachment['id']) }}">Delete Image</a>
And redefine the route to Route::get(...)
(or maybe use ajax for POST method if that is required)

Resources