Am trying to update multiple data in laravel but the solutions am getting when i search on goole are not what am lookin for. here is mycode:
In blade
<form action="{{url('users/update')}}" method='post'>
#foreach($data as $users)
<tr>
<td>
<input type="" name="id[]" value="{{ $users->id }}">
<input type="" name="names[]" value="{{ $users->name }}">
</td>
</tr>
#endforeach
<button type="submit">Submit</button>
</form>
In Controller
public function updateuser(Request $request){
$users = user::where("id" ,$request->$id)->update(["name" => $request->names]);
return back()->with('success','Successfully');
}
I want to be able to update lets say i have 10 records in the database i want to update all 10 using their respective IDs
Thank you in advance
First if you want to update or do something you need to provide #csrf in the form. If you are doing update or delete method you need to provide #method('PUT') or #method('DELETE').
Second thing I am not sure that you can provide user::, try User::.
Here is what you asked for
foreach($request->id as $key => $id) {
User::where('id', $id)->first()->update([
'name' => $request->names[$key]
]);
}
This should work fine :)
Related
I have a form at the Blade like this:
#forelse($orders as $order)
<form method="POST" action="{{ route('orders.newprint', ['id'=>$order->id]) }}">
#csrf
<tr>
<td><input class="form-check-input" name="orderCheck[]" type="checkbox" value="{{ $order->ord_id }}"> </td>
<td>{{ $order->ord_id }}</td>
</tr>
#empty
<td colspan="7" class="text-center">Nothing to show</td>
#endforelse
<hr>
<label>Actions:</label>
<select class="form-control select2" name="actions" id="actions">
<option value="">Select...</option>
<option value="print_factors">Print Factors</option>
<option value="print_orders">Print Orders</option>
</select>
<button class="btn btn-primary float-left">Do</button>
</form>
And I added this route:
Route::post('orders/newprint/{order}','OrdersController#prnpriview')->name('orders.newprint')->middleware('permission:shop-manage');
And the method of Controller goes here:
public function prnpriview(Request $request, Order $order)
{
$checks = $request->input('orderCheck');
foreach($checks as $check){
dd($check);
}
}
But I get this error:
The POST method is not supported for this route. Supported methods: GET, HEAD, DELETE.
I don't know why it shows this because I have specified POST as the route action!
So if you know how to solve this, please let me know...
Thanks in advance.
This problem comes when you have another route that either begins like that one, for example:
Route::post('orders/newprint/...
or it has the same name
->name('orders.newprint')
And it's defined after the wanted route.
Clarification: In Laravel, when there are same route names defined only the last one will be taken... It works as the array key principle... If you push same key into existing array, that what you set last will be the one in use:
$array = [
'name' => 'route_name'
];
$array['name'] = 'some_different_name';
print_r($array);
// result
Array
(
[name] => some_different_name
)
The pattern to match should begin with a forward slash: '/orders/newprint/{id}'
While I'd rather call it: '/orders/{id}/print' (a matter of taste).
In your action prnpriview() in Controller, You put Order object in parameter. And you are sending order id in form
change
public function prnpriview(Request $request, Order $order)
to
public function prnpriview(Request $request, $order_id)
May be issue is in your routing methods.
Try out below things.
<form method="POST" action="{{ route('orders.newprintv1', $order->id) }}">
Mention id directly in action route we didn't required a array for single record.
In Route :
Route::post('orders/newprint/{order}','OrdersController#prnpriview')->name('orders.newprintv1')->middleware('permission:shop-manage');
Change name of the route orders.newprint make sure any other route name is not like the same above you write route in route.php check all the routes before this one.
may be check for the resource route ? define any route with orders/newprint.
due to that also making issue.
one more thing for confirm move your existing route without making any changes
make it to first at top of the route.php file like this is your first route.
and then check it will works then other route making conflict with same name.
Use this way
<form method="POST" action="{{ route('orders.newprint', $order->id) }}">
I'm working with Laravel 8 to develop basic online ordering system and currently I have a table named orders and each order can have one of these statuses:
suspending, awaiting, verified, confirmed, ignored, and rejected.
So at the Controller, I coded this:
public function index()
{
$uid = auth()->user()->id;
$suspendeds = Order::where('status', 'suspending')->where('user_id', $uid)->latest()->paginate(2);
$awaitings = Order::where('status', 'awaiting')->where('user_id', $uid)->latest()->paginate(2);
$verified = Order::where('status', 'verified')->where('user_id', $uid)->latest()->paginate(2);
$confirmed = Order::where('status', 'confirmed')->where('user_id', $uid)->latest()->paginate(2);
$canceled = Order::whereIn('status', ['rejected','ignored'])->where('user_id', $uid)->latest()->paginate(2);
return view('profile.index', compact(['suspendeds','awaitings','verified','confirmed','canceled']));
}
And then at the Blade, I added this:
<div class="profile-body BKoodakBold">
#include('profile.orders.suspends')
#include('profile.orders.awaites')
#include('profile.orders.verifies')
#include('profile.orders.confirms')
#include('profile.orders.cancels')
</div>
And each Blade, follow the same structure just like this:
<tbody>
#foreach($verified as $verify)
<tr>
<td><input class="form-control" type="text" value="{{ $verify->title }}" disabled="disabled"></td>
<td><input class="form-control" type="text" value="{{ $verify->material }}" disabled="disabled"></td>
<td><input class="form-control" type="text" value="{{ $verify->color }}" disabled="disabled"></td>
<td><input class="form-control"type="text" value="{{ $verify->description }}" disabled="disabled"></td>
</tr>
#endforeach
</tbody>
But now the problem is, it does not show anything at all! However, there are many Orders available at orders table, following those specific statuses that I have written earlier.
And also if I dd() each variable at the Controller method, it will prints the data successfully on page, meaning that the queries are working fine.
So what is going wrong here? How can I show data properly?
You just need to provide variables to child views
#include('profile.orders.suspends', ['suspends' => $suspends])
#include('profile.orders.awaites', ['awaites' => $awaites])
#include('profile.orders.verifies', ['verifies' => $verifies])
#include('profile.orders.confirms', ['confirms' => $confirms])
#include('profile.orders.cancels', ['cancels' => $cancels])
I have an edit form with a text field and an image field where a user can edit and upload new text or a image if he/she wants to. But if the user does not upload a new image I just want to keep the old image in the database.
My Problem
The problem is when I hit upload button, I'm redirected to the article page and no error is shown but the DB has not been updated what so ever.
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.
web.php
Route::get('/tests/edit/{id}', 'TestController#edit');
Route::patch('/tests', 'TestController#update');
TestController.php
public function edit(Request $request)
{
$test = Test::findOrFail($request->id);
return view('test.edit', ['test' => $test]);
}
public function update(Request $request, Test $test)
{
$test->user_id = $request->user()->id;
$test->name = $request->name;
if($request->hasFile('image')) {
Storage::delete('public/image/' . $test->image); //Delete old image
$path = $request->file('image')->store('public/image');
$test->image = basename($path);
}
$test->update();
return redirect('/tests')
}
edit.blade.php
#extends('layouts.app')
#section('content')
<div>
<form action="/tests" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
{{ method_field('patch') }}
name:<input type="text" name="name" value='{{ $test->name }}'><br>
image: <input type="file" name="image"><br>
<input type='submit' value='upload'>
</form>
</div>
#endsection
edit.blade.php add this :
<input type="hidden" name="id" value="{{ $test->id }}">
TestController.php
use $test->save(); instead of $test->update();
I had the same problem as you.I work on a ordering website with laravel that you can get data from laravel and show it to user and also he can order something from the website and the database will be updated.But, I cannot neither get or update data in database.I work with wampserver, mysql and the problem is solved once I switched to MariaDB instead of mysql in phpmyadmin: http://localhost/phpmyadmin/
I am trying to update just one column from'business_account' table. I tried something like bellow, When trying to get the form value to my 'packPurchasedMembers' controller function I am getting null value. What should be the right code to getting expected result.
Route::group(['prefix' => 'super', 'middleware' => 'super', 'as' => 'super.'], function () {
Route::post('verify-account', array('as' => 'verify-account', 'uses' => 'packPurchasedMembers#postVerifyAccount'));});
My Controller-
public function postVerifyAccount(Request $request){
$uid = $request->get('userid');
$verfiy = $request->get('verification');
DB::table('business_account')
->where('user_id', $uid)
->update(['verified' => $verfiy]);}
My Form -
<div class="pull-left">
<h4>Verify Account</h4>
#foreach ($verification as $verify)
<form action="{{ url('super/verify-account') }}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="userid" value="<?php echo $uid; ?>" />
<input type="radio" id="reinv1" name="verification" value="0"
<?php if ($verify->verified == '0') echo 'checked' ?> >
<label for="reinv1"> Not Verified</label>
<input type="radio" id="reinv2" name="verification" value="1"
<?php if ($verify->verified == '1') echo 'checked' ?> >
<label for="reinv2"> Verified</label>
<button type="submit" class="btn btn-success" value="Submit">Submit</button>
</form>
#endforeach
</div>
Please use the following function to retrieve input value
public function postVerifyAccount(Request $request){
$uid = $request->input('userid');
$verfiy = $request->input('verification');
DB::table('business_account')
->where('user_id', $uid)
->update(['verified' => $verfiy]);
}
Just in case if anyone lands up to this question. I would like to address the solution for the above question.
So the problem was he had not used the proper HTTP request. Instead of using POST request he was using GET request.
In routes/web.php
The request method is changed from GET -> POST.
Hope it may help someone.
I'm trying to make something that I'm not sure if is possible and how exactly can happen.
What I want is to have one table which is in form and one addition form inside. The depending of which button I hit to perform different actions in controller. Here is what I have so far
my blade
{{ Form::open(array('url' => 'admin/inv')) }}
{{ Form::open(array('url' => 'admin/inv/multiPC')) }}
<table class="table table-bordered">
<tbody>
<tr>
<td><input type="checkbox" name="delete[]" value="{{ $product->product_id }}"> </td>
<td><strong>${{ $product->price }}</strong><input type="number" name='price[]' class="form-control"/></td>
</tr>
</tbody>
</table>
<button type="submit" href="{{ URL::to('/admin/del') }}?_token={{ csrf_token() }}">Delete</button>
<button type="submit" href="{{ URL::to('/admin/multiPC') }}?_token={{ csrf_token() }}">Update Price</button>
{{ Form::close() }}
{{ Form::close() }}
Those are both functions
public function pDelete() {
$delete = Input::only('delete')['delete'];
$pDel = Product::whereIn('product_id', $delete)->delete();
return Redirect::to('/admin/inv')->with('message', 'Product(s) deleted.');
}
public function priceUpdate() {
$pchanges->price = Input::only('price')['price'];
$pChange = Product::whereIn('product_id', $pchanges);
$pChange->save();
return Redirect::to('/admin/inv')->with('message', 'Product(s) price changed.');
}
And route
Route::post('/admin/inv', ['uses' => 'AdminController#pDelete', 'before' => 'csrf|admin']);
Route::post ('/admin/inv/multiPC', ['uses' => 'AdminController#priceUpdate', 'before' => 'csrf|admin'])
What happen is when I check product and hit Delete button product is deleted. But when I input price in the input field for price and hit Update Price page only refreshed and price isn't changed.
Is there a way to accomplish this without using JS?
try this type of approach
<form method="POST" class="form-horizontal" action="myapplication/personal">
<input type="number" name='price[]' class="form-control"/>
<input type="checkbox" name="delete[]" value="{{ $product->product_id }}">
<button type="submit" name="step[0]" value="Delete">Delete</button>
<button type="submit" name="step[1]" value="Update">Update Price</button>
</form>
from your controller check the value of step and do as you like
public function formProcess() {
$action = request::get('step'); // i forgot laravel 4 syntex. used laravel 5 instead here :D
if($action == 'Delete')
{
// do delete operation
}
else
{
//do update operation
}
}
hope this helps