laravel 5.3 old input values always empty - laravel

see docs here about old input
Route::post('/search/all/', function (Request $request) {
//...
$products = $query->paginate(15);
$data = ['products' => $products,
'oldinput' => $request->all()];
return view('inventory.search_products', $data);
});
in the view:
this works:
<input type="text" id="search_all" name="search_all" value="{{ $oldinput['search_all'] }}">
this is always empty:
<input type="text" id="search_all" name="search_all" value="{{ old('search_all') }}">

Just call flush in your controller then you can use old() helper function in your blade.
public function YourController(Request $request){
$request->flash();
return view('yourblade');
}
In blade file:-
<input id="lng" name="lng" value="{{old('lng')}}" type="hidden">

docs says you should flash() then call old() method.
flashing stores the previous request in the session. so it makes sense that old(search_all) doesn't work

I will suggest the following solution:
return view('inventory.search_products', $data)->withInput(\Input::all());
And in blade you can call as well \Input::old('search_all');.

Related

How to redirect back to preivous template with both input and collections of Eloquent in Laravel?

I need to pass both input and collections, that this controller produce, to the previous template. I try to use:
return redirect()->back->withInput()->with('userdata',$userdata);
but get undefined variable when access $userdata in template. This is controller:
public function inquireUpdateProcess(){
$input = request()->all();
$userdata = AuthorityKind::where('authority', $input['authority'])->first();
return redirect()->back->withInput()->with('userdata',$userdata);
}
And this is template of view:
<label for="text-authority-change">name of authority:</label>
<input type="text" name="authority_name_change" class="form-control"
value="{{$userdata->authority_name}}" />
I use the following instead then it works. But the outcome is couldn't pass the input data and collection in the same time, I know there must be a way to use return redirect()->back()... and get both previous input and the collection in template.
$userdata = AuthorityKind::where('authority', $input['authority'])->first();
$binding = [
'title' => 'Authority management',
'userdata' => $userdata,
];
return view('authority.authView', $binding);
I found out the data put into with() can only get it by session in template of blade like this :
<input type="text" id="text-authority-change" name="authority_name_change" class="form-control"
value="{{session()->get('userdata')['authority_name']}}"
/>
Even the collections of Eloquent are the the same way to access.

how do i pass data value to another page via link in laravel?

i am trying to make a list of locations that you can rent. but to rent the place you need to fill in some information. to fill in this information you excess another page. how do i make it so laravel knows the page belongs to a certain location
this is what ive done now but i keep getting the error:
Call to undefined method App\Reservation::location()
as soon as i have filled in the fields of information
this is the blade file that links to the the create reservation file
#foreach
($locations as $location => $data)
<tr>
<th>{{$data->id}}</th>
<th>{{$data->name}}</th>
<th>{{$data->type}}</th>
<th><a class="btn" href="{{route('Reservation.index', $data->id)}}">rent</a></th>
</tr>
#endforeach
this is the create reservations blade
<form action="{{ route('location.store') }}" method="post">
#csrf
<label>title</label>
<input type="text" class="form-control" name="name"/>
<label>type</label>
<select>
<option value="0">klein</option>
<option value="1">groot</option>
</select>
<button type="submit" class="btn">inschrijven</button>
</form>
this is what the location controller looks like
public function store(Request $request)
{
$location = new Reservation;
$location->name = $request->get('name');
$location->type = $request->get('type');
$location->location()->associate($request->location());
$location->save();
return redirect('/location');
}
and the relationships in my models should also work
class Reservation extends Model
{
public function locations()
{
return $this->belongsTo('Location::class');
}
}
class Location extends Model
{
public function reservations()
{
return $this->hasMany('Registration::class');
}
}
ive been stuck at this all day and i really dont know where to look anymore
The error you are getting is because of the wrong function name, you are calling location, while it is locations.
public function locations(){}
&
$location->location()->associate($request->location());
and you can pass the variable as a query parameter, you'll need to pass this data as an array in your blade file.
Web.php
Route::get('/somewhere/{id?}, function(){
//do something
})->name('test');
Blade
route('test', ['id' => $id]);
Controller Method
public function store(Request $request, $id) //Adding the query parameter for id passed in Route.
{
$location = new Reservation;
$location->name = $request->get('name');
$location->type = $request->get('type');
$location->location()->associate($id);
$location->save();
return redirect('/location');
}

Route on Laravel is wrong

I can't to do this route works...
My Controller:
public function profissionais(Request $request, $id){
$profissionais = Vinculo::where('unidade_id', '=', $id)->get();
$profissionais = $id;
return view('relatorios.profissionais', compact('profissionais'));
}
My Form:
<form method="GET" action="{{route('relatorios.profissionais', 'id')}}">
<select class="js-example-basic-single" name="id" required>
#foreach($unidades as $unidade)
<option value="{{$unidade->id}}">{{$unidade->descricao}}</option>
#endforeach
</select>
<span class="input-group-btn">
<button class="btn btn-primary" type="submit">Listar</button>
</span>
</form>
web.php:
Route::get('/relatorios/profissionais/{id}', 'RelatorioController#profissionais')->name('relatorios.profissionais');
I like my route like this: /relatorios/profissionais/4 (4 is the ID) and the number 4 will the $id variable.
But the uri is like: relatorios/profissionais/id?id=4
Any help?
The second parameter of the route helper should be the value of the parameter, not the key.
{{route('relatorios.profissionais', 4)}}
Now, because you're setting this value from the form, you either need to use the request input instead of a route parameter or use javascript to modify the form action using a listener on the change event of your select element.
The reason you have /id is because of 'id' being the 2nd argument. The reason you have ?id=4 is because it is a form value, not a route parameter.
You're also overwriting $profissionais immediately after retrieving the collection
$profissionais = $id;
You can do this like:
Route::get('/relatorios/profissionais/{id?}', 'RelatorioController#profissionais')->name('relatorios.profissionais');

Insert a value to hidden input Laravel Blade

How to pass a value to hidden input ?
Create form :
#if (isset($id))
{!! Form::hidden('edition', $id) !!}
#endif
I got the form id by url like this :
Add Journal
( when I click Add Journal button it will shows a create form with edition id at the url)
and the controller is :
$id = $request->get('edition');
$journal = Edition::findOrFail($id)->journal()->create($input);
The result gave me this error "No query results for model [App\Edition]."
Usually, this is used in Blade templates.
Just pass the name and value to the method.
{{ Form::hidden('invisible', 'secret') }}
This creates a very simple element which looks like the following.
<input name="invisible" type="hidden" value="secret">
To add other attributes, pass a third argument to the method. This third argument must be an array.
{{ Form::hidden('invisible', 'secret', array('id' => 'invisible_id')) }}
Now the input has an id attribute.
<input id="invisible_id" name="invisible" type="hidden" value="secret">
Check out : Creating a Hidden Input Field
If still not work check you project have Laravel Collective installed
In controller method check
public function store(Request $request)
{
$name = $request->input('name');
}
you can do this with the help of blade
<input type="hidden" value="{{$user->id}}" name="user_id">

Update batch codeigniter

I'm trying to update the database by editing a form with multiple valuse based on 1 id.
I'm trying to use $this->db->update_batch('table', $data, 'id');
But i think i'm not getting the correct parse to insert the data in batch mode to the database.
My question is if this way is the correct:
<input type="text" name="item_id[]" value="">
<input type="text" name="rep_id[]" value="">
<input type="text" name="ean[]" value="">
$update_item_id = $this->input->post('item_id');
$update_rep_id = $this->input->post('rep_id');
$update_ean = $this->input->post('ean');
$update_items = array(
'id' => $update_item_id,
'rep_id' => $update_rep_id,
'ean' => $update_ean
);
if($this->form_validation->run() == true && $this->model_m->editbatch($rep_id, $update_items)){
this->session->set_flashdata('message', "All is ok");
redirect("controller", 'refresh');
}
Model:
public function editbatch($id, $update_items)
{
$this->db->update('table', $update_items, 'id');
}
Any help is appreciated.
you don't use correctly the update, try like this:
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Use update_batch instead of update
public function editbatch($id, $update_items)
{
$this->db->update_batch('table', $update_items, 'id');
}

Resources