I want to send data from a Controller to an other one. From Client Crud to Reservation Crud. With this form i want send to booking method in ReservationController to associate a room with the client.
<form action="{{Route('reservation.booking', $room->id, $client->id)}}" method="POST">
#csrf
#method('get')
<select class="roomname" name="roomname">
<option selected>Elige habitacion</option>
<option value="{{$rooms[0]->id}}">ESTANÇA DEL PUIGRACIÓS</option>
<option value="{{$rooms[1]->id}}">ESTANÇA DE SANT CRISTÒFOL</option>
<option value="{{$rooms[2]->id}}">ESTANÇA (DÚPLEX) DEL ROCACENTELLA</option>
</select>
<input type="submit" value="Reservar habitación" class = "btn btn-primary">
</form>
public function booking(Room $room, Client $client)
{
//dd($client);
$room->client()->associate($client);
$room->save();
return redirect(route('reservation.create'));
}
The route definition:
Route::get('/reservation/{reservation}/booking','ReservationController#booking')
->name('reservation.booking');
And I got this message:
SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a
default value (SQL: insert into rooms (client_id, updated_at,
created_at) values (?, 2020-05-10 16:21:19, 2020-05-10 16:21:19))
Related
I am trying to pass a value from the <option> of my <select> input to my livewire controller component and echo the value.
Livewire Blade View Component:
{!! Form::select('goals',$goals_plucked,null,[
'placeholder' => trans('classes.backend.forms.select_goals'),
'class' => 'custom-select',
'id' => 'goals',
'wire:change' => "goals(this.val)",
]) !!}
This get's me an output of null in my Livewire Controller Component
Livewire Controller Component
public $goals;
public function goals($goals)
{
dd($goals);
}
After watching some tutorials. I also tried using 'wire:change' => "goals($event.target.value)", which gave me an error of Undefined variable $event, obviously because it was not defined in main controller. I am not sure what am I doing wrong here.
What I am trying to do: Trying to create a flow just like select country then select state and then select city. via livewire. Before selecting a country the inputs of the select state and city won't be visible
I tried below code and it worked for me well. Just had to make sure I use normal html form inputs and dynamically add options to it by foreach loop. Also used mount() function for getting getting values and id's for select dropdowns.
Livewire Blade View Component:
<select wire:model="goal" name="goal" class="form-control" required>
<option value="">
{!! trans('classes.backend.forms.select_goals') !!}
</option>
#foreach ($goals as $goal)
<option value="{{ $goal->id }}">{{ $goal->goals }}</option>
#endforeach
</select>
Livewire controller component
public $goals;
public $goal;
public function mount()
{
$this->goals = Goals::all()->isActive();
}
public function updatedGoal($value)
{
dd($value);
}
just give wire:model="variable_name" to select in front end.
and in livewire controller there should be a public variable with same name. it will automatically get the value on select value change.
below is the example of same
<select class="custom-select border-0 shadow-none" id="enquiry_for" wire:model="enquiry_for">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
</select>
I have a table Organizations which have the following attributes
id
name
acronym
And reformat the organization name by concatenate the name and acronym attributes in the Organization Model using Accessor
public function getOrganizationNameAttribute()
{
if($this->acronym != null)
{
return $this->name. ' ('.$this->acronym.') ';
}
else{
return $this->name;
}
}
The created form where user have to select organization on typing using datalist
<label class="col-form-label">Organization <span class="star">{{$star ?? ''}}</span></label>
<input list="organizations" type="text" name="organization" class="form-control #error('organization_id') is-invalid #enderror" value="{{old('organization',$old ?? '')}}" placeholder="Ex. TGNP Mtandao, Vodacom" {{$req ?? ''}}/>
<datalist id="organizations">
#foreach($organizations as $organization)
<option value="{{$organization->organization_name}}"/>
#endforeach
</datalist>
When a user select a particular organization the system will search for the organization_name and get its unique ID if not exists the system will create new value and return the ID
Example have a value in organization table
id:1
name:United Republic of Tanzania
acronym: URT
Then form return Organization name as
United Republic of Tanzania (URT)
Here is My problem:
when a user select United Republic of Tanzania (URT) and submit the form i would expect the system to return ID 1 but instead return null. How do i solve this?
in your html you should use
first add these links to your head
<link href="https://cdn.jsdelivr.net/npm/select2#4.0.13/dist/css/select2.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/select2#4.0.13/dist/js/select2.min.js"></script>
then use this select
<select name="organizations" id="myorgselect" class="form-control" multiple="multiple">
#foreach($organizations as $organization)
<option value="{{$organization->organization_id}}">
{{$organization->organization_name}}
</option>
#endforeach
</select>
and add this to your javascrict
$("#myorgselect").select2({
tags: true
})
see link below for use tagging
https://select2.org/tagging
I am working on this ASP.NET Core MVC where I have this DropDownLisit which gets its values from Controller using ViewBag.DishTypes. However, upon submitting the form, the POST method is not getting the value of the option selected in the DropDownList. The code snippets are as follows:
Controller: GET Method
var allDishTypes = _context.DishType
.ToList()
.Select(dt => new SelectListItem { Value = dt.DishTypeId.ToString(), Text = dt.DishTypeName.ToString() }).ToList();
ViewBag.DishTypes = allDishTypes;
return View();
View
<form asp-controller="Home" asp-action="AddMenuItems">
<div class="row">
<label class="my-1 mr-2" for="inlineFormCustomSelectPref">Dish Type</label>
<div class="input-group">
<div class="fg-line form-chose">
<label asp-for="DishTypeId" class="fg-labels" for="DishTypeId">Dish Type</label>
<select asp-for="DishTypeId" asp-items="ViewBag.DishTypes" class="form-control chosen" data-placeholder="Choose Dish Type" required name="dishtype" id="dishtype">
<option value=""></option>
</select>
</div>
</div>
....
Controller: POST Method
[HttpPost]
public IActionResult AddMenuItems([Bind("DishTypeId, DishName, Cost")] Dishes dishesObj)
{
....
}
the POST method is not getting the value of the option selected in the DropDownList
Note that you specified a name=dishtype within your code. By this way, the field name is
always the same as this name attribute, i.e, dishtype instead of DishTypeId, which will not be recognized by ASP.NET Core by default.
To fix that issue, simply remove that attribute such that it uses asp-for to generate the name attribute automatically:
<select asp-for="DishTypeId" asp-items="ViewBag.DishTypes"
class="form-control chosen" data-placeholder="Choose Dish Type" required
name="dishtype" id="dishtype"
>
<option value=""></option>
</select>
i want to download file using dropdown, dropdown is looping from database like this :
this is controoler DataController method index() for showing filename from database:
public function index()
{
$data = Document::select('filename')->get();
return view('testdata', compact('data'));
}
this is download function for download file deppendds filename, when i select Burger.jpg so downloaded file should be Burger.jpg
public function download(Request $request)
{
$data = Document::find($request->filename);
return response()->download(public_path($data));
return redirect()->back();
}
but when i select Burger.jpg, downloaded file is sow.docx, i select carbon.png, downloaded file also sow.docx, alwasy sow.docx,
this is my form action:
<form
method="get"
action="{{route('download')}}"
accept-charset="UTF-8"
enctype="multipart/form-data">
<div class="form-group">
<select class="custom-select" name="filename" id="input-filename">
<option value="">
Select data
</option>
#foreach($data as $datas)
<option value="{{$datas->id}}">{{$datas->filename}}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Download</button>
</form>
this is my database view :
how to download based on id ?
when i select Burger.jpg so downloaded Burger.jpg, when i select sow.docx, downloded is sow.docx???
how to be like that ??
thanks
I cannot manage to capture a value from a combo from my login view, to use it in the user registry. I am trying to use the HTTP routes methods as described in the laravel 5.1 documentation, but without any success.
Here is the combo cboFranquicias in the view of login.blade.php, whose value I want to capture to pass it to the user registry view:
<form method="POST" action="{{ url('/auth/login') }}">
{!! csrf_field() !!}
<div class="row margin">
<div class="input-field col s12">
<i class="mdi-social-person-outline prefix"></i>
<input type="text" name="usuario" id="usuario" value="{{ old('usuario') }}" required>
<label for="usuario" class="center-align">Usuario</label>
</div>
</div>
<div class="row margin">
<div class="input-field col s12">
<i class="mdi-action-lock-outline prefix"></i>
<input type="password" name="password" id="password" required>
<label for="password">Contraseña</label>
</div>
</div>
<!--COMBO TO LIST FRANQUICIAS -->
<br><div class="input-field col m12 s12" style="right:-1%">
{!!Form::select('Franquicias', $combo_franquicias, null, ['placeholder'=>'--Selecciona--','id'=>'cboFranquicias','name'=>'cboFranquicias'])!!}
{!!Form::label('Franquicias')!!}
</div><!--END COMBO -->
<div class="row">
<!--ACOMODADO TAMAÑO DEl BOTON ENTRAR-->
<div class="input-field col s12 m12">
<button type="submit" class="btn waves-effect waves-light col s12 m12 19">Entrar</button>
</div>
</div>
</form>
Later, in routes.php, I declare a route that takes by the method the values entered in the login, to the function that will return the user registry view:
Route::get('registrar', 'Auth\AuthController#VistaRegistraUsuario');
Route::post('auth/login', 'Auth\AuthController#VistaRegistraUsuario');
And here is the function that returns to the user registry view in the AuthController.php file:
public function VistaRegistraUsuario(Request $request)
{
$id_franquicia = $request->input('cboFranquicias');
//dd($id_franquicia);
$cargos = cargos_usuarios::lists('descripcion', 'id');
return view('auth.register', compact('cargos','id_franquicia'));
}
However, the value never reaches the user registry view, which should pass to fill my hidden input in that view register.blade.php:
{!!Form::input('Id_franquicia', $id_franquicia, null,
['type'=>'hidden','name'=>'id_franquicia', 'id'=>'id_franquicia'])!!}
How do I capture the value of the franquicias combo in the login view and save it to a global variable, to pass that value to the user registration view?
I have solved the problem using the session method, thus leaving the solution:
Right after submitting for the login, I captured the selected value of my franquicia combo:
`protected function handleUserWasAuthenticated(Request $request, $throttles)
{
if ($throttles) {
$this->clearLoginAttempts($request);
}
if (method_exists($this, 'authenticated')) {
return $this->authenticated($request, Auth::user());
}
$id_franquicia = $request->input('franquicia_id');//CAPTURO LA ID DEL COMBO DE FRANQUICIAS
Session::set('id_franquicia', $id_franquicia);// GUARDO EN UNA VARIABLE GLOBAL LA ID CAPTURADA, POR MEDIO DE SESSION
return redirect()->intended($this->redirectPath());
}`
I capture the value of the id with request->input, and I save it in a variable that I then declare global with session :: set, to then use it in my user registration view with session :: get
//VISTA PARA EL REGISTRO DE USUARIO
public function VistaRegistraUsuario()
{
$id_franquicia_usuario = Session::get('id_franquicia');//ID DE LA FRANQUICIA
$cargos = cargos_usuarios::lists('descripcion', 'id'); //COMBO DE LOS CARGOS
return view('auth.register', compact('cargos', 'id_franquicia_usuario'));
}// FIN VISTA
You might want to change your select reference, the first parameter to the Form::select method is the name of the field. So try,
{!!Form::select('cboFranquicias', $combo_franquicias, null, ['placeholder'=>'--Selecciona--','id'=>'cboFranquicias'])!!}
In your Controller you are using $request->input('cboFranquicias')
So no need to pass the name as part of the options, from the method signature if the options['name'] is not set it will default to the $name
Update to use session logic
You could do something like the following if you want to keep the same endpoint for a post after login and then a get to the same endpoint:
public function VistaRegistraUsuario(Request $request)
{
// Set the session on the auth/login post request
if ($request->has('cboFranquicias')) {
session(['id_franquicia' => $request->input('cboFranquicias')]);
}
$cargos = cargos_usuarios::lists('descripcion', 'id');
return view('auth.register', [
'cargos' => $cargos,
'id_franquicia' => session('id_franquicia'),
]);
}