send value from dropdownlist to controller laravel - 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.

Related

Laravel Livewire form, if validation fails, pivots don't work

I have a form made with Livewire in Laravel.
This is the Livewire controller
namespace App\Http\Livewire;
use Livewire\Component;
use App\Rules\Mobile;
class Form extends Component
{
public $mobile;
public $required;
public $fields;
public $showDropdown = true;
public function mount()
{
foreach($this->fields as $field){
$this->required[$field->name] = ($field->pivot->is_required == '1') ? 'required' : 'nullable';
}
}
public function submit()
{
$validatedData = $this->validate([
'mobile' => [$this->required['mobile'] ?? 'nullable', new Mobile()]
]);
}
}
This is the Livewire view
<div>
<div x-data="{ open: #entangle('showDropdown').defer, required: #js($required) }">
<span x-show="open" wire:loading.remove>
<form wire:submit.prevent="submit" style="display: flex; flex-direction: column;">
<div class="fields">
#foreach($fields as $field)
<div class="form-{{$field->name}}">
#if($field->name == 'mobile')
<input name="{{$field->name}}" type="tel" wire:model.defer="{{ $field->name }}" placeholder="{{ __($field->pivot->placeholder ?? $field->name) }}">
#endif
#error($field->name) <span class="error">{{ ucfirst($message) }}</span> #enderror
</div>
#endforeach
</div>
<button class="btn btn-primary">Send</button>
</form>
</span>
</div>
</div>
Problem is here placeholder="{{ __($field->pivot->placeholder ?? $field->name) }}"
When the form is first loaded $field->pivot->placeholder is set, but after I submit the form and the validation fails, it's not set anymore and $field->name is used instead of $field->pivot->placeholder
Have checked this way:
<input name="{{$field->name}}" type="tel" wire:model.defer="{{ $field->name }}" placeholder="{{ __($field->pivot->placeholder ?? $field->name) }}">
{{ var_dump(isset($field->pivot->placeholder)) }}
When the form is first loaded it prints bool(true) under the field, after I send the form it says bool(false)
How can I get over this? Why the pivot does not work after validation fails?
//Edit: Did a workaround, but I would still like to know why it happens
What I did is I used another property $placeholders
In the Livewire controller have added public $placeholders;, in the mount() have added $this->placeholders[$field->name] = $field->pivot->placeholder ?? $field->name;, and then used it in the view like placeholder="{{ __($placeholders[$field->name] ?? $field->name) }}"
I don't know much about livewire , but I know that mount() only called on the initial page load meaning it will only run when you refresh the whole page or visit the page. Instead of only using mount() you should also use hydrate() which will run every subsequent request after the page load.
mount() - Initial Page Load
hydrate() - Every subsequent request
You can implement the hydrate() like how you implement the mount()

MethodNotAllowedHttpException:The GET method is not supported for this route. Supported methods: POST

I am new to laravel and now i am facing an issue. I saw many related answers ,but nothing works for me. My problem is, i have a page and when i update a user i want to redirect on the same page with updated results.When i look into the db table,the updation is happening,but the page shows the above error. I tried many answers which i saw on stack , but nothing works for me.
Thank you
Here is my view:
<form method="POST" action="/updateleaduser">
#csrf
<h6 style = "font-family:Palatino" class="card-text">Assigned To: {{$us->name}}</h6>
<input type="hidden" name="idd" name="idd" value="{{$us->id}}">
<select name="select_user" class="form-control">
#foreach($testusers as $user)
<option value="{{$user->id}}">{{$user->name}}</option>
#endforeach
</select>
<button type="submit" class="form-control" style="background-color: green;color: white;">Update User</button>
Here is my route:
Route::post('/updateleaduser','RequestController#updateuserlead')->name('updateleaduser');
Here is my controller:
public function updateuserlead(Request $request){
$idd = $_POST['idd'];
$select_user = $request->input('select_user');
DB::table('leads')->where('id',$idd)->update(array(
'client_id'=>$select_user,
));
return redirect()->back();
}
Try replacing return redirect()->back(); with return redirect()->to("/updateleaduser")
public function updateuserlead(Request $request){
$idd = $_POST['idd'];
$select_user = $request->input('select_user');
DB::table('leads')->where('id',$idd)->update(array(
'client_id'=>$select_user,
));
return redirect()->to("/updateleaduser");
}

How to pass array from controller to vue.js v-for?

I am trying that from controller pass values (from database column),
to vue.js component (for select option in v-for), but when I send from controller to blade and get as prop in vue.js and put as data, I get every character in json as one select option.
How I can fix that to work properly and to put json object in v-for select?
RolesController.php
$roles = Roles::all('name');
return view('users.create', ['users' => $users]);
And in the blade I pass value to vue component:
<users-add
roles="{{$roles}}"
></users-add>
My UsersAdd.vue:
<select>
<option v-for="role in roles" :value="role">
{{role}}
</option>
</select>
But I get every character in the list from json in the select, instead to get every role name in every select row.
For example I get:
{
"
n
a
m
e
"
:
"
m
o
d
e
r
a
t
o
r
"
Instead
moderator
Assuming you have a function in your RoleController that looks similar to the following:
public function index()
{
$roles = Role::all('name');
$users = User:all(); // or however you're obtaining your users
return view('users.create', compact('roles', 'users'));
}
In your users.create blade template, you want to have the following:
<users-add :roles="{{ $roles }}"></users-add>
Note the colon (:) before the roles prop.
Then in your UserAdd vue component, something like the following should work for you:
<template>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<label for="role">Role</label>
<select id="role" name="role">
<option v-for="role in this.roles" :value="role">
{{ role.name }}
</option>
</select>
</div>
</div>
</div>
</template>
<script>
export default {
props: [
'roles'
]
}
</script>

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

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');
}

Resources