I m new to Laravel and I am trying to pass values from a controller which I have received from a <form> to a view and display the same in text boxes. Although, I have figured out how to do the same using method chaining but I would like to pass the values using an array and show the same into the textboxes in the view.
What I expect to do?
In the controller, instead of method chaining:
return view('showvalues')->with(['name'=>$name, 'address'=>$address]);
Code so far,
controller
public function showvalues(Request $request)
{
$name=$request->get('name');
$address=$request->get('address');
$pass=$request->get('password');
$arr=array("$name","$address","$pass");
return view('showvalues')->with('name',$name)->with('address',$address);
}
Show values in showvalues view:
<html>
<head>
</head>
<body>
<h1>
Show Value Page.
</h1>
<input type="text" name="n1" value="<?php echo $name;?>" /><br>
<input type="text" name="n3" value="<?php echo $address;?>" />
</body>
</html>
Use the compact method as the second argument to view:
public function showvalues(Request $request)
{
$name = $request->get('name');
$address = $request->get('address');
$pass = $request->get('password');
return view('showvalues', compact('name', 'address', 'pass'));
}
The variables will be available in your view file by the same name, you can display them like:
{{ $name }}
{{ $address }}
{{ $pass }}
Pass array as second argument to view():
return view('showvalues', ['name'=>$name, 'address'=>$address]);
Btw, did you try to open Laravel's manual?
You need to return all form inputs with compact function.
Controller
public function showvalues(Request $request)
{
$form = $request->all();
return view('showvalues', compact('form'));
}
View
{{ $form['name'] }}
{{ $form['address'] }}
what about
return view('showvalues')->withName($name);
Or
return view('showvalues')->with(['name'=>$name]);
Related
I'm trying to setup a simple button to update a db column value when clicked. I can't seem to figure out why my route isn't getting passed my value however?
HTML:
<form method="post" action="{{ route('approveResturant') }}">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $resturant->id }}" />
<button class="btn btn-outline-success" type="submit">
Approve
</button>
</form>
Controller:
public function approveResturant($request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
Route:
Route::post('approveResturant'[ResturantController::class,'approveResturant'])->middleware(['auth'])->name('approveResturant');
And finally, the error itself:
Any help appreciated!
Add the Request type-hint to your function:
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
The difference here is that Laravel understands the Request type-hint and knows that it should inject the Request object from the pre-defined services it has in its service container. Otherwise, Laravel doesn't know where that parameter is coming from so assumes you will provide it. Simply naming your parameter $request is insufficient.
Update
Do you know why the function would still not be saving the new approved value to the DB?
A few potential reasons:
You have not removed the dd($request->all()); statement
$resturant = Resturant::find($id); failed to find a record in the database
save is a function not a property so $resturant->save; should be $resturant->save();
To isolate the exact issue you will need to perform some debugging (e.g. either using xdebug or dd statements).
Use Request Class
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
dd($request->all());
$id = $request->id;
$resturant = Resturant::find($id);
$resturant->approved = 1;
$resturant->save;
return redirect()->back()->with('message', 'Resturant Approved Successfully!');
}
<form method="post" action="{{ route('restaurant.approveResturant') }}">
{{ csrf_field() }}
<input type="hidden" name="id" value="{{ $resturant->id }}" />
<button class="btn btn-outline-success" type="submit">
Approve
</button>
</form>
Route::post("/restaurant/store", [RestaurantController::class, "approveResturant"])->name("restaurant.approveResturant");
use Illuminate\Http\Request;
public function approveResturant(Request $request)
{
$restaurant = Restaurant::where("id", $request->input("id"))->update([
"approved" => 1
]);
return redirect()->back()->with('message', 'Restaurant Approved Successfully!');
}
I am making a simple form two sum two values. I want to display the calculated result in an readonly input after the request went through. If I dd() the sum of my two fields, the sum is correct. But I am unable to pass the sum back to the view.
I searched around the web and found nothing promising. Same with the Laravel documentation.
Form:
<p>
<input type="number" name="numberOne" value="{{ old('numberOne') }}">
</p>
<p>
<input type="number" name="numberTwo" value="{{ old('numberTwo')}}">
</p>
<p>
<input type="number" name="calculated" value="{{ isset($calculated) ? $calculated : '' }}" readonly>
</p>
Controller:
class CalculationsController extends Controller
{
public function process(Request $request) {
$numberOne = $request->input('numberOne');
$numberTwo = $request->input('numberTwo');
$calculated = $numberOne + $numberTwo;
dd($calculated);
return redirect('/')->withInput();
}
}
Expected would be the sum of both inputs in the readonly. But after the submit, it's still empty. What am I doing wrong?
When you use the redirect() function the variable $calculated isn't passed to the view. I think the best solution is passing it by Session. Your redirect must be something like that:
return redirect('/')->withInput()->with('calculated', $calculated);
The with() function is used to pass data through the session. To retrieve it in your view, do:
<input type="number" name="calculated" value="{{ session()->has('calculated') ? session('calculated') : '' }}" readonly>
For documentation, check:
https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data
https://laravel.com/docs/5.7/session#retrieving-data
Add calculated value before redirect,
class CalculationsController extends Controller
{
public function process(Request $request) {
$numberOne = $request->input('numberOne');
$numberTwo = $request->input('numberTwo');
$calculated = $numberOne + $numberTwo;
$request->merge(['calculated' => $calculated ]);
// dd($calculated);
return redirect('/')->withInput();
}
}
$request->merge(['calculated' => $caluclated]);
return redirect('/')->withInput($request->all());
<input type="number" name="calculated" value="{{ old('calculated') }}" readonly>
I'm trying to display a query in a view. Each result I want to be a submit button that would submit his value when clicked.
In ListController I have this code:
public function index()
{
$tables = DB::table('tables')->select('name')->get();
return view('welcome', compact('tables'));
}
In welcome.blade.php I have:
<form action="{{ route('get_table') }}" method="POST">
{{ csrf_field() }}
#foreach($tables as $data)
<input type="submit" name="tables[]" value="{{ $data }}">
#endforeach
</form>
In routes web.php I have:
Route::get('/', 'ListController#index');
And I get this error:
"htmlspecialchars() expects parameter 1 to be string, object given (View:...welcome.blade.php)"
What am I doing wrong?
$data is an object, despite you only selecting one column. You have two options:
Use the name property for $data:
<input type="submit" name="tables[]" value="{{ $data->name }}">
Or use pluck to retrieve a collection of single names.
$tables = DB::table('tables')->pluck('name');
I have a simple input form with a text input field and a submit button. I am trying to get the value from the input field to be displayed again on the same page after the submit button is clicked. So far laravel always throws an error that the variable is undefined.
Route:
Route::get('/find/names', "FindController#get_name")->name('names');
Controller
public function get_name(){
$name = Input::get('name_by_user');
return $name;
}
view
<form role="form" method="GET">
<div class="row">
<div class="form-group">
<div class="input-group">
<input type="text" name="name_by_user"/>
<span class="input-group-btn">
<button class="btn search-button" type="submit">
<span aria-hidden="true">
<span>Search</span>
</button>
</span>
</span>
</div>
</div>
</div>
</form>
display name after submitting: {{$name}}
I would do something like this
Route
Route::name('names')->get('/find/names', "FindController#get_name");
Controller
public function get_name(){
$collection = Input::all();
$name = $collection->pluck('name_by_user');
return view('view_file_in_resources', compact('name'));
}
Now you will have a $names collection in your view.
But if you only want to fetch result from one row, you controller should look like this:
public function get_name($name){
$name = Input::where('name_by_user', $name)->get();
return view('view_file_in_resources', compact('name'));
}
And your routes file
Route::name('names')->get('/find/names/{name}', "FindController#get_name");
When generating a view inside a controller for a route, you can do the following in a function to return a view with data depending on whether it exists.
public function showNameView() {
if(is_null(Input::get('name_by_user'))
{
return view('my.view')->with(['name' => Input::get('name_by_user')]);
}
else
{
return view('my.view')->with(['name' => Input::get('name_by_user')]);
}
}
You need to return the same view:
public function get_name(Request $request)
{
return view('same.view', ['name' => $request->name]);
}
Or you can redirect back:
return redirect()->back()->with('name', $request->name);
And display name like using session data:
#if (session()->has('name'))
{{ session('name') }}
#endif
I am using laravel 5.2. I want to print the database content that is stored in my database dynamically on the desired page. I tried but an error appears everytime i.e;( undefined variable:). I just want to print whatever content I store in my database table dynamically.
My code is here:
My model name is:gallery
My routes:
Route::get('/gallery/list' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/gallery/save' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);
My controller:
public function viewgallerylist()
{
$galleries = gallery::all();
return view('gallery')->with('galleries', $galleries);
}
public function savegallery(Request $request)
{
$gallery1=$request['gallery_name'];
$gallery=new gallery();
$gallery->name=$gallery1;
$gallery->save();
return redirect()->route('viewgallery');
}
My desired page:
<form method="post" action="{{ route('savegallery') }}">
<input class="form-control" type="text" name="gallery_name">
<button type="submit" class="btn btn-primary" id="upl">Create+</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
#foreach($galleries as $gallery)
<p>{{ $gallery->name }}</p>
#endforeach
Most model Classes will have a capital letter. Are you sure your Model isn't called Gallery instead of gallery?
which means that you need to call Gallery::all() in your controller and make sure use App\Gallery; is at the top of your page.
public function viewgallerylist()
{
$galleries = Gallery::all();
return view('gallery')->with('galleries', $galleries);
}
The problem might be with your model.... please provide a larger view
Route::get('/' ,[
'uses'=>'gallerycontroller#viewgallerylist',
'as'=>'viewgallery'
]);
Route::post('/' ,[
'uses'=>'gallerycontroller#savegallery',
'as'=>'savegallery'
]);