laravel 9 error 404 page not found when update - php

At the most basic understanding, I've been trying to match the route and the form action. I think that I am doing it right but I wonder why the error keeps on showing. I may have missed something anywhere but I just really couldn't find it. Please help. With a very tight schedule, I need to complete this project.
When I submit the form it goes to this address.
index.blade.php
<tbody>
#foreach($payments as $payment)
<tr>
<td>{{ $payment->order->first_last_name }}</td>
<td>{{ $payment->order->business_name }}</td>
<td>{{ $payment->order->business_type }}</td>
<td>{{ $payment->order->phone_number }}</td>
<td>{{ $payment->order->postal_code }}</td>
<td>{{ $payment->order->email }}</td>
<td>{{ $payment->order->address }}</td>
<button type="button" class="btn btn-success btn-sm" data-action="{{ route('payments.send', $payment->id) }}" data-bs-toggle="modal" data-bs-target="#send" data-id="{{ $payment->id }}">اSend</button>
</td>
<td>
#php $status = $payment->status #endphp
<span class="{{ $status['bs_class'] }}">
{{ $status['label'] }}
</span>
</td>
<td>{{ $payment->accounting_code }}</td>
</tr>
#include('Admin.layouts.modal')
#endforeach
</tbody>
js
<script>
$('#send').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var action = button.data('action');
var orderId = button.data('id');
var modal = $(this);
modal.find('form').attr('action', action);
document.getElementById("orderId").value = orderId;
});
</script>
modal.blade.php
<div class="modal fade" id="send" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="send" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form action="{{ route('payments.accounting', $payment->id) }}" method="post">
#csrf
#method('PUT')
<input type="hidden" id="orderId" value="">
<div class="modal-header">
<h5 class="modal-title" id="sendTitle">send</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="accounting_code" class="form-label">کد accounting_code</label>
<input type="text" class="form-control" id="accounting_code" name="accounting_code">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success">save</button>
</div>
</form>
</div>
</div>
</div>
web.php
Route::put('/payments/accounting/{payment}', [App\Http\Controllers\Admin\PaymentController::class,'accounting'])->name('payments.accounting');
PaymentController.php
public function accounting(Payment $payment, Request $request)
{
dd('hello');
$data = [
'accounting_code' => $request->post('accounting_code')
];
$payment->update($data);
return back();
}

Because your js code modal.find('form').attr('action', action) replaced the form action when showing the modal. Try to change the data-action from route payments.send to payments.accounting for the send button:
<button type="button" class="btn btn-success btn-sm" data-action="{{ route('payments.accounting', $payment->id) }}" data-bs-toggle="modal" data-bs-target="#send" data-id="{{ $payment->id }}">اSend</button>

Did you try it without naming the route?
delete this:
->name('payments.accounting')
and then in HTML form:
action="/payments/accounting"
I think you are mixing POST, PUT and GET Request here. Make sure you send your form with the correct method.

Related

laravel 8 crude operation not showing

i have laravel connected
to database mysql
when i do insert/update i find it on the mysql table
but the page show data does not reflect the change
my page is products.
the web route:
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ProductController;
Route::get('/', function () {
return view('welcome'); });
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::resource('products', ProductController::class);
the show index.blade.php page :
#extends('products.layout')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 CRUD Example from scratch - ItSolutionStuff.com</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('products.create') }}"> Create New Product</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Details</th>
<th width="280px">Action</th>
</tr>
#foreach ($products as $product)
<tr>
<td>hi</td>
<td>{{ $product->name }}</td>
<td>{{ $product->detail }}</td>
<td>
<form action="{{ route('products.destroy',$product->id) }}" method="POST">
<a class="btn btn-info" href="{{ route('products.show',$product->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('products.edit',$product->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
#endsection
does any one can give me the reason why change not reflected
enter image description here
In your controller, after updating product, try to do:
return redirect()->back();

Delete button not work bootstrap modal and Laravel

I created one table that list items, and I added two buttons(edit and delete) so, the button edit is working, but the button delete is not working, and I'm making me carzy because I don't find the error. this is the table:
<table id="datatable" class="table align-items-center table-flush">
<thead class="thead-light">
<tr>
<th>text</th>
<th class="disabled-sorting" width="10%"></th>
<th class="disabled-sorting" width="10%"></th>
</tr>
</thead>
<tbody>
#foreach($Items as $item)
<tr>
<td>{{ $item->text }}</td>
<td><a href="{{ route('ItemsController.edit', $item->id) }}" class="btn btn-primary btn-fab btn-icon btn-round" title="Edit">
<i class="fa fa-edit"></i></a>
</td>
<td>
<a href="#" class="btn btn-primary btn-fab btn-icon btn-round btn-delete" title="Delete" data-id="{{ $item->id }}" data-toggle="modal" data-target="#modal-default" data-route="{{ route('ItemsController.destroy', $item->id) }}" data-title="{{ $item->id }}">
<i class="fa fa-trash"></i></a>
</td>
</tr>
#endforeach
</tbody>
</table>
the modal is:
<div class="modal fade" id="modal-default" tabindex="-1" role="dialog" aria-labelledby="modal-default" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Delete Item</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Delete?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<form class="" action="" method="post">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="submit" class="btn btn-danger" name="button">yes, delete</button>
</form>
</div>
</div>
</div>
</div>
javascript data:
<script>
$(document).on('click', '.btn-delete', function(){
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
})
</script>
and controller function destroy
public function destroy(Items $item) {
$item->delete();
return redirect()->route('items.index')->with('success', 'Deleted Success');
}
I checked it line by line, and I don't know what I'm wrong, please help me
Many times thanks for getting into this.
You need to trigger your modal from JS.
If it is bootstrap 4 then just simply open it using,
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
//This will trigger modal
$("#modal-default").modal();
If it is bootstrap 5 then just simply open it using,
$('.modal form').attr('action', $(this).data('route'));
$('#ModalLabel').text($(this).data('title'));
//This will trigger modal
modal = new bootstrap.Modal(document.getElementById("modal-default"));
modal.show();

How to pass data to edit modal from #foreach

<tbody>
#foreach($courses as $course)
<tr>
<td>{{ $course->course_code }}</td>
<td>{{ $course->course_name }}</td>
<td>{{ $course->description }}</td>
#if(is_null($course->teacher))
<td></td>
#else
<td>{{ $course->teacher->last_name }}, {{ $course->teacher->first_name }}</td>
#endif
<td>
<button type="button" class="assign-modal btn btn-sm btn-success" data-toggle="modal" data-target="#assignModal" data-id="{{ $course->id }}">
<i class="bi bi-person-plus"></i>
</button>
</td>
</tr>
#endforeach
This is my table body
<!-- Assign Modal -->
<div class="modal fade" id="assignModal" tabindex="-1" role="dialog" aria-labelledby="assignModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="assignModalLabel">Assign Teacher</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
#if(!empty($course->id))
<form method="POST" action={{ route('courses.update', $course->id) }}>
#csrf
#method('PUT')
<input type="text" class="form-control" name="id" value="" id="course-id" hidden="">
<select class="form-control select-teacher" style="widht: 100%" name="teacher_id">
#foreach($teachers as $teacher)
<option value="{{ $teacher->id }}">{{ $teacher->last_name }}, {{ $teacher->first_name }}</option>
#endforeach
</select>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Assign</button>
</div>
</form>
#endif
</div>
</div>
</div>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
This is my Modal.
Apparently, it was working fine until yesterday, but It's no longer work.
The problem now with my code is that only the last column of data is editable. How can I modify the values ​​of other columns?
each modal must have a specific id, try by adding {{ $course->id }} to tag id and aria-labelledby attribute
<div class="modal fade" id="assignModal{{ $course->id }}" tabindex="-1" role="dialog" aria-labelledby="assignModalLabel{{ $course->id }}" aria-hidden="true">
the previous code must also be inside a foreach loop as the first one in tbody
#foreach($courses as $course)
<div class="modal fade" id="assignModal{{ $course->id }}" tabindex="-1" role="dialog" aria-labelledby="assignModalLabel{{ $course->id }}" aria-hidden="true">
...
</div>
#endforeach
And also update all id in modal tag as previous
An id must be unique.
And also update as following to attach button to the specific id
<button type="button" class="assign-modal btn btn-sm btn-success" data-toggle="modal" data-target="#assignModal{{ $course->id }}" data-id="{{ $course->id }}">

Make bootstrap modal create an object to be added to list

In my Laravel project, I have the following models:
Product ('name' string, 'amount' integer and 'price' decimal)
Cart ('cost' decimal)
CartProd ('unit_price' decimal, 'amount' integer, 'discount' decimal, 'total_cost' decimal and foreign keys to a product and a cart)
The idea is that, when you want to create a new cart, the CartController method creates a list of all the products, a brand new cart and an empty list in which the selected products for the cart will be added, and sends them all through the view's compact.
public function create(Request $request)
{
$product_list = Product::all();
$empty_cart = new Cart();
$selected_products = array();
return view('models.carts.create', compact('product_list','empty_cart','selected_products'));
}
public function store(Request $request){}
The view where this happens has the list where the selected products will be shown and a button to add a product to the list.
create.blade.php:
#extends('adminlte::page')
#section('content')
<div class="container"><div class="row justify-content-center"><div class="col-md-8"><div class="card">
<div class="card-header">
<h3 class="card-title"> New Cart </h3>
<div class="card-tools">
<a class="btn btn-success btn-tool" href="#" data-toggle="modal" data-target="#add">
<i class="fas fa-plus"></i> Add Product
</a>
</div>
</div><div class="card-body">
<table class="table table-striped projects table-sm">
<thead><tr><th>#</th><th>Name</th><th>Unit Cost</th><th>Amount</th><th>Discount</th><th>Final Cost</th><th></th></tr></thead>
<tbody>
#forelse($selected_products as $prodCart)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $prodCart->product->name }}</td>
<td>{{ $prodCart->product->price }}</td>
<td>{{ $prodCart->amount }}</td>
<td>{{ $prodCart->discount }}</td>
<td>{{ $prodCart->cost }}</td>
<td>
<a class="btn btn-danger btn-circle btn-sm" href="#" data-target="#delete">
<i class="fa fa-trash"></i>
</a>
</td>
</tr>
#empty
<tr><td colspan="7">Empty Cart</td></tr>
#endforelse
</tbody></table></div>
<div class="card-footer">
#if(!empty($selected_products))
<button class="btn btn-success" href="send $selected_products to CartController's store method" > Buy Cart </button>
#endif
</div></div></div></div></div>
#include('models.carts.add')
#endsection
#section('scripts')
#stop
The 'Add Product' button toggles a modal which has the displayed list of products, plus the 'amount' and 'discount' number inputs to create a CartProd with, and the button to actually add them to the list.
add.blade.php:
<!--Modal add -->
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-hidden="true"><div class="modal-dialog modal-lg"><div class="modal-content">
<div class="modal-header bg-dark">
<h5 class="modal-title" id="exampleModalLabel">Add Product to Cart</h5>
<span class="badge badge-danger" class="close" data-dismiss="modal" aria-label="Close">
<i class="fas fa-times fa-lg" style="color:#fff"></i>
</span>
</div>
<div class="modal-body">
<table class="table table-striped projects table-sm">
<thead><tr><th>#</th><th>Name</th><th>Amount</th><th>Price</th><th>Added</th><th>Discount</th><th>Amount</th><th>Add</th></tr></thead>
<tbody>
#forelse($product_list as $product)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->amount }}</td>
<td>{{ $product->price }}</td>
<td>{{ $product->created_at }}</td>
<td><input type="number" name="discount" id="discount" min="0" max="100" size="3" value="0"></td>
<td><input type="number" name="amount" id="amount" min="1" max="{{$product->amount}}" size="3" value="1"></td>
<td>
<a class="btn btn-info btn-circle btn-sm" data-datos="{{$product}}">
<i class="fa fa-plus"></i>
</a>
</td>
</tr>
#empty
<tr>
<td>No Products Available.</td>
</tr>
#endforelse
</tbody>
</table>
</div></div></div></div>
Here are the issues:
I haven't found the way to create this new CartProd instance within the modal, and send it back to the list of selected products.
How to delete said CartProd if adding it was a mistake.
How to update the selected product list every time a new product is added/deleted.
The 'Buy Cart' button to send me to the store method of the controller (where all the fancy stuff will be done to save the list of products.)
Thank you so much for your help!
You can create this model with livewire if you are familiar which has all the answers to your issues
First, wrap the modal fields with a form element
//Your modal
<form action="{{route('your_route'}}" method="post">
#csrf
<input type="number" name="discount" id="discount" min="0" max="100" size="3" value="0">
<input type="number" name="amount" id="amount" min="1" max="{{$product->amount}}" size="3" value="1">
<a class="btn btn-info btn-circle btn-sm" data-datos="{{$product}}" type="submit">
<i class="fa fa-plus"></i>
</a>
</form>
In the controller method you can redirect back to your create.blade.php with a message and the list will refresh.
public function store(Request $request)
{
// your saving logic
return redirect()->route('your_create_route')
->with('success', 'Added successfully');
}
As your question is too broad you can trigger a delete action with actioning a button by passing the product ID to delete. This is a custom approach from this. Normally you delete with the laravel's delete method #method('delete') and a POST method.
// delete link
<a href="{{route('delete_route', $product->id)}}">
Delete
</a>
// web.php
Route::get('delete/{id}','YourController#destroy')->name('delete_route');

Can't change title of modal using Laravel

I've created a resource database using Laravel, and have the option to (flag, edit, and/or delete) a resource. When I hit the 'flag' button a modal pops up for you to add input as to why you're flagging the resource. I'd like for the title of the modal to show which resource was clicked, and I have it working, but in this instance I cannot change the title of the modal regardless of which resource I flag. Here's some of my code, hopefully it'll be more clear.
Resource View
#foreach($resources as $resource) #foreach ($resource->locations as $location)
<tr>
<td> <a class="btn btn-small btn-default" style="float:right; margin-right:5px;" href="{{ URL::to('resource/addToCart/' .$resource->Resource_ID) }}">+</a> {{ $resource->Name }}</td>
<td>{{ $resource->Description }}</td>
<td>{{ $location->Address }}</td>
<td>{{ $location->City }}</td>
<td>{{ $location->Zip_Code }}</td>
<td>{{ $location->County }}</td>
<td>
<button type="button" class=" msgBtn btn btn-default" style=" display:inline; margin-right:auto;">Edit
</button>
<button type="button" class=" msgBtn2 btn btn-default" id="flagged" data-toggle="modal" data-target="#flagResource" style="display:inline; margin-right:auto;">Flag
</button>
<button type="button" class=" msgBtn3 btn btn-default" style="display:inline; margin-right:auto;">Delete
</button>
</td>
</tr>
#endforeach
#endforeach
</tbody>
So, when the middle button is clicked, it brings up the modal which looks like this
Modal View
<div class="modal fade" id="flagResource"
tabindex="-1" role="dialog"
aria-labelledby="flagResourceLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close"
data-dismiss="modal"
aria-label="Close">
<span aria-hidden="true">×</span></button>
<h4 class="modal-title"
id="flagged" style="text-align:center;"> {{ $resource->Name }}
</h4>
</div>
<div class="modal-body">
{!! Form::open(['method' => 'POST', url('FlagsController#addFlag'), $resource->id ]) !!}
<div class="form-group">
{!! Form::label('reason', 'Reason for Flag: ') !!}
{!! Form::textarea('Reason', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('comments', 'Other Comments: ') !!}
{!! Form::text('Comments', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="modal-footer">
<button type="button"
class="btn btn-default"
data-dismiss="modal">Close</button>
<span class="pull-right">
<button type="button" class="btn btn-primary" style="margin-left:5px;">
Submit
</button>
</span>
</div>
</div>
</div>
</div>
So whenever I click flag on ANY resource, It shows this:
modalExample
I can't figure out why it keeps showing up that as my title. Any help would be great, thanks!
The modal uses this data:
$resource->Name
$resource->id
You probably have the modal somewhere after your foreach in your code, so $resource is filled with data from the last row in this loop:
#foreach($resources as $resource) #foreach ($resource->locations as $location)
<td> <a class="btn btn-small btn-default" style="float:right; margin-right:5px;" href="{{ URL::to('resource/addToCart/' .$resource->Resource_ID) }}">+</a> {{ $resource->Name }}</td>
#endforeach
What you should do is send more data to the modal so it can show the correct information. So in your loop you should attach more data for every row.
I am assuming you are using bootstrap for the modal. In version 3.2.0 you can do something like this to add this data:
Flag
And use javascript to get the variables and set them in your modal instead of using $resource->Name.
$('#my_modal').on('show.bs.modal', function(e) {
var resourceId = $(e.relatedTarget).data('resource-id');
var resourceName = $(e.relatedTarget).data('resource-name');
$(e.currentTarget).find('#resourceId').val(resourceId);
$(e.currentTarget).find('#resourceName').val(resourceName);
});

Categories