How to redirect back to preivous template with both input and collections of Eloquent in Laravel? - 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.

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 insert into mysql db from a controller function

I am trying to submit records into a mysql table with many fields by using a Laravel .blade.php view ( php form)
I made some trials but I get
FatalErrorException in loginController.php line 54: Class 'App\Http\Controllers\Input' not found.
Mysql
create table users(id int primary key auto_increment,username varchar(20),password varchar(20),createDate timestamp );
My controller function
public function formSubmit()
{
if (Input::post())
{
$username = Input::get('username');
$password = Input::get('password');
DB::table('users')->insert(array ('username' => $username,'password' => $password));
return View::make('view2')->with(array('username' =>$username, 'password' => $password));
}
}
view1.blade.php form
<form action="{{url('/view2') }}" method="POST">
{{ csrf_field() }}
<input type ="hidden" name="">
User name: <input type ="text" name="username"> <br/>
Password <input type="password" name="password"> <br/>
<input type="submit" name="formSubmit" value="formSubmit">
</form>
Route
Route::get('/view1', 'loginController#formSubmit');
Add use Input; to the top of your class right after namespace clause.
Or use full namespace:
$username = \Input::get('username');
$password = \Input::get('password');
Or you just could use only() method to get an array from request:
DB::table('users')->insert(request()->only('username', password));
Also, never save raw passwords. Use bcrypt() to encrypt passwords.
Since you're using Laravel 5, use the Request Facade and acces input this way:
Request::input()
instead of using Input();
https://laravel.com/docs/5.0/requests
And, just in case, include it at the top of the Controller with
use Illuminate\Support\Facades\Request;
Referring to Amarnasan answer I used request and include use Illuminate\Http\Request; at the top of my controller.
So I changed my controller method to
public function formSubmit(Request $req)
{
$username =$req->input('username');
$password =$req->input('password');
DB::table('users')->insert(array ('username' => $username,'password' => $password));
return view ('view2')->with(array('username' =>$username, 'password' => $password));
}
}
and I changed my routes according to Shubham pokhriyal commit to:
Route::post('/view2', 'loginController#formSubmit');
Route::get('view1',function()
{
return view('view1');
}
);
and it works fine.

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

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

CodeIgniter update page - Simple CRUD website assistance required

After looking through the forums and starting to try to create a basic CRUD website I am currently struggling to have a page that updates the articles as follows. If someone could kindly tell me where I am going wrong, I will be most greatful. I am getting a 404 error at 'news/input'
model (at news_model.php)
public function update($id, $data)
{
$this->db->where('id', $id);
$this->db->update('news', $data);
}
controller (news.php)
public function update($id){
$data = array(
'title' => $this->input->post('title'),
'slug' => $this->input->post('slug'),
'text' => $this->input->post('text'));
if($this->news_model->exists($id)) {
$this->news_model->update($id, $data);
}
else {
$this->news_model->insert($data);
}
}
html (views/news/input.php)
<h2>Update a news item</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('news/update') ?>
<label for="title">Title</label>
<input type="input" name="title" /><br />
<label for="slug">Slug</label>
<input type="input" name="slug" /><br />
<label for="text">Text</label>
<textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Update an item" />
You get a 404 because your news controller seems to have no method 'input'. Try adding something like this:
public function input(){
// load the form
$this->load->view('/news/input');
}
Note that for updating data you will need to fetch and pass it into the view first, then render the (filled out) form using set_val() and other CI functions.
Currently you're "hardcoding" the HTML form which makes populating and maintaining state (when validation fails) difficult. I suggest you play through the forms tutorial on the CI website.
Edit:
To create a update/insert (upsert) controller change as follows:
Controller:
function upsert($id = false){
$data['id'] = $id; // create a data array so that you can pass the ID into the view.
// you need to differntiate the bevaviour depending on 1st load (insert) or re-load (update):
if(isset($_POST('title'))){ // or any other means by which you can determine if data's been posted. I generally look for the value of my submit buttons
if($id){
$this->news_model->update($id, $this->input->post()); // there's post data AND an id -> it's an update
} else {
$this->news_model->insert($id, $this->input->post()); // there's post data but NO id -> it's an insert
}
} else { // nothing's been posted -> it's an initial load. If the id is set, it's an update, so we need data to populate the form, if not it's an insert and we can pass an empty array (or an array of default values)
if($id){
$data['news'] = $this->news_model->getOne($id); // this should return an array of the news item. You need to iterate through this array in the view and create the appropriate, populated HTML input fields.
} else {
$data['news'] = $this->news_model->getDefaults(); // ( or just array();) no id -> it's an insert
}
}
$this->load->view('/news/input',$data);
}
And amend the $id to the action-url in your view:
<?php echo form_open('news/upsert/'.$id) ?>

Resources