Laravel - Too Few Arguements - php

I'm trying to setup a simple button to update a db column value when clicked. I can't seem to figure out why my route isn't getting passed my value however?
HTML:
<form method="post" action="{{ route('approveResturant') }}">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $resturant->id }}" />
<button class="btn btn-outline-success" type="submit">
Approve
</button>
</form>
Controller:
public function approveResturant($request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
Route:
Route::post('approveResturant'[ResturantController::class,'approveResturant'])->middleware(['auth'])->name('approveResturant');
And finally, the error itself:
Any help appreciated!

Add the Request type-hint to your function:
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
The difference here is that Laravel understands the Request type-hint and knows that it should inject the Request object from the pre-defined services it has in its service container. Otherwise, Laravel doesn't know where that parameter is coming from so assumes you will provide it. Simply naming your parameter $request is insufficient.
Update
Do you know why the function would still not be saving the new approved value to the DB?
A few potential reasons:
You have not removed the dd($request->all()); statement
$resturant = Resturant::find($id); failed to find a record in the database
save is a function not a property so $resturant->save; should be $resturant->save();
To isolate the exact issue you will need to perform some debugging (e.g. either using xdebug or dd statements).

Use Request Class
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}

<form method="post" action="{{ route('restaurant.approveResturant') }}">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $resturant->id }}" />
<button class="btn btn-outline-success" type="submit">
Approve
</button>
</form>
Route::post("/restaurant/store", [RestaurantController::class, "approveResturant"])->name("restaurant.approveResturant");
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
$restaurant = Restaurant::where("id", $request->input("id"))->update([
"approved" => 1
]);
return redirect()->back()->with('message', 'Restaurant Approved Successfully!');
}

Related

Can't update in database data. Laravel

I have a small problem with my Controller action. I can't update my "link" in Database, bt dd method work is correctly when I'm try to check data.
Form
<form class="col-lg-push-6" action="/admin/links/{{$link->id}}/update" method="POST">
#csrf
<div class="form-group bmd-form-group">
<label class="bmd-label-floating">New Link Value</label>
<input type="text" class="form-control" size="100" name="value">
<button class="btn btn-primary" type="submit">Update</button>
</div>
</form>
Controller
public function update(Request $request, $id)
{
$this->validate($request, [
'value' => 'required'
]);
$link=RedirectUrl::AllLinks()->where('id', $id);
$link->value = $request->input('value');
return redirect()->back()->with('message', 'Link Updated!');
}
Model
public function scopeAllLinks($query){
return $query->get();
}
Route
Route::prefix('admin')->middleware('auth')->group(function(){
Route::get('/', 'Admin\IndexController#index');
Route::get('dashboard', 'Admin\IndexController#index')->name('dashboard');
Route::get('links', 'Admin\LinkController#index')->name('links');
Route::get('links/{id}', 'Admin\LinkController#linkById');
Route::post('links/{id}/update', 'Admin\LinkController#update');
});
Few things here:
Your scopeAllLinks scope is incorrect, you don't call get inside a scope, instead you return the query builder instance.
You can use find since you're passing in a record id:
$link = RedirectUrl::find($id);
You never call save or update on the record:
$link->value = $request->input('value');
$link->save(); // <--- add this

How to add a custom method to resource controller in Laravel 5.6

What is the correct way of adding a custom method to a resource controller in Laravel 5.6?
What I have so far is a new method in my ProfileController:
public function approve($id){
$user = User::find($id);
$user->state = '1';
$user->save();
return redirect('/dashboard')->with('success', 'User approved.');
}
As well as the following lines added to my web.php file:
Route::post('/profile/{$id}/approve', 'ProfileController#approve');
Route::resource('profile', 'ProfileController');
The form in my view is (afaik) correctly rendered to:
<form method="POST" action="http://myurl.com/profile/10/approve" accept-charset="UTF-8">
<input name="_token" type="hidden" value="v3F1RRhi7iJL2o4egOhcRiuahaGQBwkGkfMal1lh">
<input name="_method" type="hidden" value="PATCH">
<input class="btn btn-success" type="submit" value="Approve User">
</form>
Unfortunately nothing happens, except the "Sorry, the page you are looking for could not be found." page to be shown.
What am I missing? And to expand a bit on this question also, is this even a valid way to implement "single field updates" on a db entry?
Thank you for your help!
i see you have two problems:
firstly correct the route like that
Route::post('/profile/{id}/approve', 'ProfileController#approve');
secondly you have to delete
<input name="_method" type="hidden" value="PATCH">
or replace your route like that:
Route::patch('/profile/{id}/approve', 'ProfileController#approve');
You would want to remove the $ sign from your route:
Route::post('/profile/{id}/approve', 'ProfileController#approve');
The rest of it is correct.
You have written the parameter like var: $id, and you may write it without '$'.
But really you can use the Laravel implicit model binding function to do this:
Route::post('/profile/{user}/approve', 'ProfileController#approve');
And then in your controller:
public function approve(User $user){
// Delete this line--> $user = User::find($id);
$user->state = '1';
$user->save();
return redirect('/dashboard')->with('success', 'User approved.');
}

Perform specific action on button in Bootstrap modal (Laravel)

I created a modal which displays information specific to their id entries and placed the Approve and Reject button as below.
Screenshot of modal
When a user click on "Accept" or "Reject", it needs to pass id related to the viewed entries so the user can perform the requested action, whether to accept or reject the entries (default status is 'pending').
vendor.blade.php
<div class="modal-footer">
<span class="pull-left">
<form method="POST" action="{{ route('approve') }}">
#method('PUT')
#csrf
<button type="submit" class="btn btn-success">Approve</button>
</form>
</span>
<span class="pull-right">
<form method="POST" action="{{ route('reject') }}">
#method('PUT')
#csrf
<button type="submit" class="btn btn-danger">Reject</button>
</form>
</span>
</div>
In VendorController.php
public function index()
{
$vendors = DB::select('select company_name, roc_no, created_at from mides_vendors');
$vendor_id = Vendor::where('status', 'Pending');
return view('panel.vendor', ['vendors' => $vendors]);
}
ApprovedVendorController.php
<?php
namespace App\Http\Controllers;
use App\User;
use App\Vendor;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ApproveVendorController extends Controller
{
public function approve(Request $request, $id)
{
DB::insert('insert into mides_users(name, email, password) select name,roc_no,password from mides_vendors where id = :id', ['id' => $id]);
DB::update('update mides_vendors set status = :status where id = :id', ['status' => 'Approved', 'id' => $id]);
return redirect('/');
}
public function reject(Request $request, $id)
{
DB::update('update mides_vendors set status = :status where id = :id', ['status' => 'Rejected', 'id' => $id]);
return redirect('/');
}
}
routes/web.php
Route::prefix('/panel')->group(function () {
Route::get('/dashboard', function () {
return view('panel.dashboard');
});
/* These routes only display the information/modal
Route::get('/approve-vendor', 'VendorController#showNewRegistration'); // return vendor.blade.php
Route::get('/vendor-approved', 'VendorController#showApproved'); // return vendor-approve.blade.php
Route::get('/vendor-reject', 'VendorController#showRejected'); // return vendor-reject.blade.php
/* These route used to perform the specific action */
Route::put('/approve/{id}', 'ApproveVendorController#approve')->name('approve');
Route::put('/reject{id}', 'ApproveVendorController#reject')->name('reject');
});
However, it returns this error.
Error got after clicking Accept or Reject
How do I pass the id of data? I tried as shown in pass the database value to modal popup to create the modal using second answer option (besides the ajax ones). Do I need to create another controller for these?
Edited: after do as explained by #Wreigh, it works, means that the status changed from 'pending' to 'accept/reject'. But, when I return to the previous page, which is the /panel/approve-vendor (the page is used for showing the pending list modal) then I got the error undefined variable vendorId.
You can provide the id via the url, or you can also use post values. However, let's try via the url as a parameter.
Update your routes to be like these:
Route::put('/approve/{id}', 'ApproveVendorController#approve')->name('approve');
Route::put('/reject/{id}', 'ApproveVendorController#reject')->name('reject');
Then in your form:
<form method="POST" action="{{ route('approve', $vendorId) }}">
<form method="POST" action="{{ route('reject', $vendorId) }}">
If you want via post values.
There's no need to update your routes, but update your controller action signatures:
public function approve(Request $request) {
$id = $request->input('id');
}
public function reject(Request $request) {
$id = $request->input('id');
}
And then insert this in your form:
<input type="hidden" name="id" value="{{ $vendorId }}">
WHY DO YOU ENCOUNTER THE ERROR?
In your function signature, you are expecting an $id parameter, which in your route definition, you do not have. Laravel cannot provide it magically like that, you have to provide it via the url, as a parameter.
since your approve route is like this /approve/{id} you cannot go to panel/approve because your are not passing the id
so try this:
vendor.blade.php (remove put and your id as hidden input)
<div class="modal-footer">
<span class="pull-left">
<form method="POST" action="{{ route('approve') }}">
<input type="hidden" name="id" value="{{ $id}}">
#csrf
<button type="submit" class="btn btn-success">Approve</button>
</form>
</span>
<span class="pull-right">
<form method="POST" action="{{ route('reject') }}">
#csrf
<input type="hidden" name="id" value="{{ $id}}">
<button type="submit" class="btn btn-danger">Reject</button>
</form>
</span>
</div>
ApprovedVendorController.php (remove all the parameters)
<?php
namespace App\Http\Controllers;
use App\User;
use App\Vendor;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class ApproveVendorController extends Controller
{
public function approve()
{
$id = request('id');
DB::insert('insert into mides_users(name, email, password) select name,roc_no,password from mides_vendors where id = :id', ['id' => $id]);
DB::update('update mides_vendors set status = :status where id = :id', ['status' => 'Approved', 'id' => $id]);
return redirect('/');
}
public function reject()
{
$id = request('id');
DB::update('update mides_vendors set status = :status where id = :id', ['status' => 'Rejected', 'id' => $id]);
return redirect('/');
}
}
routes/web.php (remove parameters)
Route::prefix('/panel')->group(function () {
Route::get('/dashboard', function () {
return view('panel.dashboard');
});
/* These routes only display the information/modal
Route::get('/approve-vendor', 'VendorController#showNewRegistration'); // return vendor.blade.php
Route::get('/vendor-approved', 'VendorController#showApproved'); // return vendor-approve.blade.php
Route::get('/vendor-reject', 'VendorController#showRejected'); // return vendor-reject.blade.php
/* These route used to perform the specific action */
Route::put('/approve', 'ApproveVendorController#approve')->name('approve');
Route::put('/reject', 'ApproveVendorController#reject')->name('reject');
});

Error while editing a post using Laravel update

This is my html form
<form class="form-horizontal" action="{{action('BlogController#update',[$blog->id]) }}" method="post">
<input name="method" type="hidden" value="patch"/>
<div class="form-group">
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
Here is route:
Route::patch('blog/{id}','BlogController#update');
Controller :
public function update(Request $request,$id){
$input = $request->all();
$blog =findOrFail($id);
Blog::update($input);
//var_dump($input);
return back();
}
Can you please show me where is the issue?
In your code you have write $blog = findOrFail($id); to get blog which is not correct. You can do it using
$blog = Blog::findOrFail($id);
Now you have the blog, you need to update the blog. So, the update code should be
$blog->update($input);
To make this update method works, you need to make the fields(the fields you are updating) fillable in Blog model.
You're using the wrong syntax. Do something like this to make it work:
public function update(Request $request, $id)
{
Blog::where('id', $id)->update($request->all());
return back();
}
give the name whatever you wish say blog:
Route::patch('blog/{id}','BlogController#update')->name('blog');
your HTML code
<form class="form-horizontal" action="{{route('blog', $blog->id)}}" method="post">
hope this help you!!
you have many syntax problems!
try it this way:
form:
<form class="form-horizontal"
action="{{ route('blog.update', ['id' => $blog->id]) }}"
method="post">
{{ csrf_field() }}
<input name="_method" type="hidden" value="patch"/>
<!-- other inputs -->
</form>
Route:
Route::any('blog/{id}','BlogController#update')->name('blog.update');
Controller:
public function update(Request $request, $id){
$blog = Blog::findOrFail($id);
$blog->update([
'key' => 'value'
]);
// never use $request->all() because of security issues!
return back();
}
<form class="form-horizontal" action="{{route('blog.update',[$blog->id]) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
Your Route Like This
Route::resource('blog', 'BlogController');
Your Controller
public function update(Request $request,$id){
$blog =Blog::findOrFail($id);
$blog->database_fieldname1=$request->value1;
$blog->database_fieldname2=$request->value2;
$blog->database_fieldname3=$request->value3;
$blog->save();
return back();
}

how to print the database content on a desired page?

I am using laravel 5.2. I want to print the database content that is stored in my database dynamically on the desired page. I tried but an error appears everytime i.e;( undefined variable:). I just want to print whatever content I store in my database table dynamically.
My code is here:
My model name is:gallery
My routes:
Route::get('/gallery/list' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/gallery/save' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
My controller:
public function viewgallerylist()
{
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
}
public function savegallery(Request $request)
{
$gallery1=$request['gallery_name'];
$gallery=new gallery();
$gallery->name=$gallery1;
$gallery->save();
return redirect()->route('viewgallery');
}
My desired page:
<form method="post" action="{{ route('savegallery') }}">
<input class="form-control" type="text" name="gallery_name">
<button type="submit" class="btn btn-primary" id="upl">Create+</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
#foreach($galleries as $gallery)
<p>{{ $gallery->name }}</p>
#endforeach
Most model Classes will have a capital letter. Are you sure your Model isn't called Gallery instead of gallery?
which means that you need to call Gallery::all() in your controller and make sure use App\Gallery; is at the top of your page.
public function viewgallerylist()
{
$galleries = Gallery::all();
return view('gallery')->with('galleries', $galleries);
}
The problem might be with your model.... please provide a larger view
Route::get('/' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);

Categories