Edit Page takes me to "Page not Found" rather than "Update Route" - laravel

When i click on the Edit item Button on "Edit Page" the form submits and takes me to "Page Not Found" rather than update route..
Laravel
<form action="{{ route('admin.products.update', $id) }}" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH">
// else form elements goes here
</form>
//route file
Route::resource('/products','ProductController');
public function update(Request $request, Product $product)
{
return "hello";
}
i just wanted it to return "hello"

You need to pass the id parameter correctly in your route()
{{ route('products.update', [id => $id]) }}
Admin prefix is applied by the route, you don't need to pass it in route();

Related

Solution for redirect to POST route Laravel

SCENARIO:
When I tried to search (POST route) a keyword, results of item will be display as lists. These items has a button (Save or UnSave) on each list. The save button, is either form POST or DELETE method and what happens is when this button clicked, either the item will SAVE or UNSAVE from the tables.
ERROR
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
ROUTES:
Route::post('/jobseeker/jobs/search-results', [JobSeekerController::class, 'searchJobsNow'])->name('jobseeker.jobs.searchNow');
Route::post('/jobseeker/saved-jobs/{id}/save', [JobSeekerController::class, 'saveJob'])->name('jobseeker.savedJobs.save');
Route::delete('/jobseeker/saved-jobs/{id}/unsave', [JobSeekerController::class, 'unsaveJob'])->name('jobseeker.savedJobs.unsave');
CONTROLLER:
public function searchJobsNow(Request $request)
{
// codes
}
public function saveJob()
{
// code for saving a job
return redirect()->back();
}
public function unsaveJob()
{
// code for unsaving a job
return redirect()->back();
}
VIEW
#if ($job->candidateHadSavedTheJob)
<form action="{{ route('jobseeker.savedJobs.unsave', $job->id) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-primary">Saved</button>
</form>
#else
<form action="{{ route('jobseeker.savedJobs.save', $job->id) }}" method="POST">
#csrf
<button type="submit" class="btn btn-outline-primary"> Save</button>
</form>
#endif

send value from dropdownlist to controller laravel

I am new to laravel, I need a dropdown list, then select one option, and click search button to show the result.
Controller: index shows all tutors to be select, then when select it, pass the value to the select_tutor_page.
public function index()
{
$tutors = Tutor::all();
return view('home', ['tutors' =>$tutors]);
}
public function selectTutor(Request $request, $tutorId)
{
// $tutorId = Input::get('selectTutor');
// i think should use input to get the value, but it get error with:Trying to get property of non-object (View: E:\xampp\htdocs\appointment\resources\views\selectTutor.blade.php)
$tutor = Tutor::find($tutorId);
return view('selectTutor',['tutor' => $tutor]);
}
Home page view:
<form action="" method="POST" id="tutors">
{{ csrf_field() }}
<select class="form-control" name="selectTutor" id="selectTutor" data-parsley-required="true">
#foreach($tutors as $tutor)
<option value="{{ $tutor->id }}">{{ $tutor->name }}</option>
#endforeach
</select>
select
</form>
Select tutor page:
<h1>{{$tutor->name}}<h1>
Route:
Route::get('/home', 'HomeController#index')->name('student.home');
Route::get('selectTutor/{tutorId}', 'HomeController#selectTutor')->name('select.tutor');
please help....
This should work for you:
$tutorId = $request->selectTutor;
You can see all current request data if you add this line to your controller:
dd($request->all());
Also, to make it work, you'll need to add an action to the form:
<form action="{{ url('selectTutor/'.$tutor->id) }}" method="POST" id="tutors">
And submit this form with submit button instead of creating a href link.

Undefined variable in view laravel

I have this code here which is listing all items from one user, user and item are two differents model and they have relationship with their primary keys.
#foreach($user->items as $item)
<p class="heading">
{{ $item->item_id }}
</p>
#endforeach
My question is how to send that item_id with post request to controller, because when I write like this for example <form class="form-horizontal" method="post" action="{{ route('preview', $item->item_id) }}" role="form" enctype="multipart/form-data"> it gives me that $item variable is not defined.
My controller:
public function preview($item_id){
return view('itempreview')->with('item_id', $item_id);
}
Any ideas how to send that item_id?
You should use array as 2nd param to route() method!
You are doing it wrong, you should send it like this:
route('preview', ['item_id' => $item->item_id]);
Make sure, your route should be defined as this:
Route::post('preview/{item_id}', 'ControllerName#preview')->name('preview');
Hope this helps!
You need to send $item_id as part of the POST:
#foreach($items as $item)
<form method="post" action="{{ route('preview') }}">
<input type="hidden" name="item_id" value="{{ $item->item_id }}">
</form>
#endforeach
Then in the method that receives the POST:
public function index() {
$item_id = request()->input('item_id');
return view('preview');
}

How to populate edit form fields in laravel 5.3

I have a problem in populating edit form fields in my first Laravel 5.3 application. In examples I have found online, they have used Form facade to do it. However, I prefer to use plain html.
This is my controller action
public function edit()
{
$profile = Profile::find(Auth::user()->id);
return view('profile.edit', ['profile' => $profile]);
}
In my view file I have a form like this
<form method="POST" action="{{ url('profile/update') }}">
{{ csrf_field() }}
<input id="name" type="text" name="name" value="{{ old('name') }}">
...
</form>
Problem is, above input field is not populated with the value of the $profile->name.
I can get it to work, setting the value attribute like
{{ (old('name')) ? old('name') : $profile->name }}
Since I'm new to Laravel framework I want to know if there is a better way to do that.
Use old($key, $default). It will return $default if $key is not found.

Laravel 5 Plain Html MethodNotAllowedHttpException

Hello so I am trying to make an update form in Laravel using plain html. The url of my edit form is http://localhost:8000/profile/sorxrob/edit, sorxrob is the username. And the code in that url is:
<form class="form-horizontal" role="form" action="" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
//more inputs
</form>
And in my controller named AccountController:
public function update(Request $request, $id)
{
$account = Accounts::findOrFail($id);
$input = $request->all();
$account->fill($input)->save();
return 'success';
}
And I am getting the error MethodNotAllowedHttpException when I click the update button. Is it because my action is equal to nothing? If yes, what is the correct way of routing there?
This is due to your action url which is not correct routing url. Use the following
(1) First define a route in your route.php file
Route::post('profile/{username}/edit', array('as' => 'profile.update', 'uses' => 'AccountController#update'));
(2) Change your action attribute from your form tag
action="{{ URL::route('profile.update', [$username]) }}"
here $username variable will be passed from your AccountController#edit method.

Resources