Undefined variable in view laravel - 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');
}

Related

Error message "The GET method is not supported for this route. Supported methods: POST." in laravel 8

Following is my controller. Method ProjectsView is to view all the listings. The second method BackendProjectSearch is for searching of projects. The first page of search result is displayed properly, but when we click on next page it gives the error "The GET method is not supported for this route. Supported methods: POST."
What should I do ?
public function ProjectsView(){
$projects = projects::orderBy('id','ASC')->paginate(15);
return view('backend.projects.projects_view',compact('projects')); }
public function BackendProjectSearch(Request $request){
$request->validate(["search" => "required"]);
$item = $request->search;
$projects = Projects::where('project_name','LIKE',"%$item%")->paginate(15);
return view('backend.projects.projects_view',compact('projects')); }
Following are the routes for both the methods :
Route::post('/backend/project/search', [ProjectsController::class, 'BackendProjectSearch'])->name('backend.project.search');
Route::get('/view', [ProjectsController::class, 'projectsView'])->name('projects.view');
View code :
<div class="col-md-8">
<div class="header-navsearch">
<form class="form-inline mr-auto" method="post" action="{{route('backend.project.search')}}">
#csrf
<div class="nav-search">
<input type="search" name="search" class="form-control header-search" placeholder="Search projects…" aria-label="Search">
<button class="btn btn-primary" type="submit"><i class="fa fa-search"></i></button>
</div>
</form>
</div>
</div>
The route for this request should be post, not get.
Example
Route::post(-----);
Also, make sure to insert a CSRF token while making the request
it means you now cannot do something like the following.
Instead, you have to do something like...
<form action="{{ route('example') }}" method="POST">
#csrf {{-- According to the version of laravel --}}
{{-- Your other codes --}}
<button type="submit" class="">Submit</button>
</form>
You have to use the below code
return redirect()->route('projects.view')->with(['projects' => $projects]);
Your route might be little change
Route::get('/view/{projects?}', [ProjectsController::class, 'projectsView'])->name('projects.view');
and In your controller, you have to change function like this
public function ProjectsView($projects = null){
if($projects!=null){
return view('backend.projects.projects_view',compact('projects'));
}
else{
$projects = projects::orderBy('id','ASC')->paginate(15);
return view('backend.projects.projects_view',compact('projects'));
}

use patch method to update model in laravel

i want to update a customer using patch methodn here is my route
Route::patch('/customers/updateCustomer', 'CustomerController#update')
->name('customers.update');
and here is my form :
<form method="POST" action="{{ route('customers.update') }}">
#csrf
#method('PATCH')
how to pass the id of the customer , the update method requires two parameters, when the first one is the data from the from , the second should be the id of the customer
You should send the customer id with the route() helper function.
Make sure to send the customer object during the form rendering.
<form method="POST" action="{{ route('customers.update', $customer['id']) }}">
#csrf
#method('PATCH')
And modify the route slightly.
Route::patch('/customers/updateCustomer/{customerId}', 'CustomerController#update')->name('customers.update');
add customer id to the route like
Route::patch('/customers/updateCustomer/{id}', 'CustomerController#update')->name('customers.update');
and then your form action should be like
<form method="POST" action="{{ route('customers.update',$customer->id) }}">
you have to send the customer object from where you are returning to the form.
and finally your update function
public function update(Request $request, $id)
{
//your things to do
}
You have to use like this
<form method="POST" action="{{ route('customers.update',['id'=>$customer->id]) }}">
And your route should be
Route::patch('/customers/updateCustomer/{id}', 'CustomerController#update')->name('customers.update');
And your controller should be
public function update(Request $request, $id){
//your code
}

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

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();

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.

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.

Resources