Insert a value to hidden input Laravel Blade - laravel

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">

Related

how to hide id in url in laravel?

I am creating ecommerce website in Laravel and I want to hide productdetails id in url. What can I change in code plz need solution.
Web.php
Route::get('/productdetail{id}','ProductDetailController#display_products_details');
controller code:
public function display_products_details($id)
{
$data = DB::select('select * from subcategory inner join product_details on subcategory.sub_id=product_details.sub_id where product_details.sub_id = ?',[$id]);
return view('productdetails',['data'=>$data]);
}
link tag code:
{{ $value->name_of_subcategory }}
path I getting:
localhost:8000/productdetail12
But actually I want Localhost:8000/productdetail
You can do this by having the id as part of the request body so it will not be visible in the URL (it will still be visible to anyone inspecting the request body via their browser developer tools).
You can do this by using a form to send a post request:
Route::post('/productdetail','ProductDetailController#display_products_details');
The link will change to a form:
<form method="POST" action="{{ action('ProductDetailController#display_products_details') }}>
#csrf
<input type="hidden" name="id" value="{{ $value->sub_id }}" />
<button type="submit">{{ $value->name_of_subcategory }}</button>
</form>
You may need to style the button to look like a link.
The controller becomes:
public function display_products_details(Request $request)
{
$request->validate([ 'id' => 'required|int' ]);
$id = $request->input('id');
$data = DB::select('select * from subcategory inner join product_details on subcategory.sub_id=product_details.sub_id where product_details.sub_id = ?',[$id]);
return view('productdetails',['data'=>$data]);
}
Note that while this is theoretically possible it does result in very bad user experience. People can't share product links with others for example

Laravel missing parameters to update form in controller

So I have a little project, in it, there's the possibility to upload banners images to be shown on the main page. All the stuff related to the DB are already created and the option to create a banner is working, it creates the banner and then stores the image on the DB for use later. Now I'm trying to work on an edit function so I can change the description under the bannners. I have an Edit route in my controller which returns a view where I edit said banner then it calls the update function on the controller. But no matter what I put here, I'm always getting the Missing Required Parameters error once I try to Save the edit and open my controller through the Update function. Here's the code as it is now:
The route definition:
Route::resource('banner', 'BannerController');
The edit function on my controller:
public function edit($id)
{
return view('admin/edit-banners',['id'=>$id]);
}
The update function has not been implemented because I always start with a dd() function to check if everything is working fine:
public function update(Request $request, $id)
{
dd($request);
}
And here's the form line in my edit view that is trying to call the update route:
<form class="card-box" action="{{ route('banner.update',[$banner]) }}">
I also added this at the beginning of the view to store the data from the DB into a variable:
#php
use\App\Banner;
$banner = Banner::where('id','=',$id)->get();
#endphp
The $banner variable contains all the information on the banner being edited, and I can get the new description at the controller with the $request variable, so I honestly don't know what should I put here as parameters, any ideas?
The $banner variable is not a Model instance, it is a Collection.
Adjust your controller to pass this to the view instead of dong the query in the view:
public function edit($id)
{
$banner = Banner::findOrFail($id);
return view('admin.edit-banners', ['banner' => $banner]);
}
You could also use Route Model Binding here instead of doing the query yourself.
Remove that #php block from your view.
The form should be adjusted to use method POST and spoof the method PUT or PATCH (as the update route is a PUT or PATCH route) and you should adjust the call to route:
<form class="card-box" action="{{ route('banner.update', ['banner' => $banner]) }}" method="POST">
#method('PUT')
If you include $id in your function declaration then when you call the route helper it expects you to give it an id parameter. Try with
<form class="card-box" action="{{ route('banner.update',['id' => $id]) }}">
You should be able to retrieve the form data just fine form the $request variable. More info here.
The code below should be the error source. $banner variable then is an array but the update function accept object or id.
#php
use\App\Banner;
$banner = Banner::where('id','=',$id)->get();
#endphp
You should try to replay this code by this one...
#php
use\App\Banner;
$banner = Banner::find($id);
//you should put a dd here to view the contain of $banner if you like
#endphp
Hop it help...

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.

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

laravel 5.3 old input values always empty

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

Resources