I was trying to send an array through a route to another view, but when i used the function get_defined_vars(), i realized that i was sending a string with the information. Is it possible to do that?
this form from my view should send the array to my route
<form action="/trans" method="POST">
#csrf
<div class="input-group">
<input type="hidden" class="form-control" name="r" value="{{$cooperado}}">
<button type="submit" class="btn btn-primary">
<span>+</span>
</button>
</span>
</div>
</form>
then this route should send the array to the other view
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with(['j'=>$j]);
});
this is the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create()
{
//
return view('movs.create');
}
}
routes.php
Route::post('/trans', 'MovimentacoesController#create');
controller
use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create(Request $request)
{
$j = $request->request->get('r');
return view('movs.create')->with(['j' => $j]);
}
}
Code like this In the form tag:
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
submit this form
then the Input::get('r') will be Array!
I hope it helps you.
Related
how do i send a form POST method with GET route in laravel?
Route
Route::get('domain_detail/{domain_name}','domain_detailController#index');
View domain_detail folder
<form method="post" action="{{url('domain_detail')}}/{{strtolower($domain_detail->domain_name)}}">
<div class="form-group">
<label for="namefamily">namefamily</label>
<input type="text" class="form-control round shadow-sm bg-white text-dark" name="namefamily">
</div>
<div class="form-group">
<label for="mobile">mobile</label>
<input type="text" class="form-control round shadow-sm bg-white text-dark" name="mobile">
</div>
<div class="form-group">
<label for="myprice">myprice</label>
<input type="number" class="form-control round shadow-sm bg-white text-dark" name="myprice">
</div>
<div class="form-group">
<input type="submit" name="send_price" class="btn btn-success" value="submit">
</div>
</form>
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class domain_detailController extends Controller
{
public function index($domain_name)
{
$domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
if ($domain_detail_exist) {
$domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();
return view('domain_detail/index', ['domain_detail' => $domain_detail]);
} else {
return view('404');
}
}
public function create()
{
return view('domain_detail.index');
}
}
At the controller i didn't put any codes in the create function, but when i click on submit button in form i get this error
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods:
GET, HEAD.
Use the index function in your domain_detailController just so return the view.
like this:
public function index($domain_name)
{
return view('domain_detail.index');
}
create a route to return the view:
Route::get('domain_detail/','domain_detailController#index');
Then use the create function to store the domain detail like this:
public function create($domain_name)
{
$domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
if ($domain_detail_exist) {
$domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();
return view('domain_detail/index', ['domain_detail' => $domain_detail]);
} else {
return view('404');
}
}
make a POST route like this:
Route::post('domain_detail/','domain_detailController#create');
Also take a look at the laravel best practices when it comes to naming conventions:
https://www.laravelbestpractices.com/
I'm Trying to solve this error but not getting any proper solution for this as i'm beginner in the laravel, Please help with the code..
error--> 1
Thank You
This is my Web.php file
Route::get('/', function() {
return view('welcome');
});
Route::get('user',function(){
return view('user');
});
Route::get('user/register',['uses'=> 'usercontroller#create']);
Route::post('/user',['uses'=> 'usercontroller#store']);
Register.blade.php
<form method="POST" method="/user">
{{ csrf_field() }}
name<input type="text" name="name">
email<input type="email" name="email">
pass<input type="password" name="password">
<input type="submit" value="register">
</form>
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class usercontroller extends Controller
{
public function create()
{
return view('register');
}
public function store(Request $request)
{
User::create($request->all());
return 'Sucess';
return $request->all();
}
}
You have a mistake in your form tag (the second method should be action):
Change
<form method="POST" method="/user">
to
<form method="POST" action="/user">
I'm learning Laravel and I got stuck trying to get data from a form.
I already am able to get data back with GET, but with POST I've been having a ton of trouble. Here's what I'm working with:
Form:
<form id="forms" method="POST" action="sugestoes" novalidate>
{{ csrf_field() }}
<div class="form-row">
<div class="form-group col-md-12">
<label for="obs">Observações:</label>
<textarea type="text" class="form-control" name="obs" placeholder="Observações" required></textarea>
</div>
</div>
<hr>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
#php
if (isset($_POST["obs"])) {
echo "IN";
}
#endphp
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
$name = $request->input('obs');
return redirect('sugestoes');
//
}
}
Route:
Route::post('sugestoes', 'PostController#store');
The intended behaviour that I'm trying to reach is for the post to be submitted, and then returning to the same page with an empty form. Later on I'll be sending the input data into a database, but for now I just want to get the post to work.
I guess I'm missing something really basic, but I've been following guides and looking online, I've done some progress but I'm really stuck here.
(some more info, this is Laravel 5.4, and I'm using XAMPP)
First, you need to call the model, use App/Your_model_name; then you have to save the data.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Suggest; //Suggest model, let's hope you have suggest table
class PostController extends Controller
{
public function store(Request $request)
{
$suggest = new Suggest; //model
$suggest->name = $request->obs; //name is DB name, obs is request name
$suggest->save(); //save the post to DB
return redirect()->back()->with('success', 'Saved successfully'); //return back with message
}
}
Then if you want to flash the message on the HTML page
#if(session('success'))
<div class="alert alert-warning alert-dismissible" id="error-alert">
<strong style="color: white;">{{session('success')}}</strong>
</div>
#endif
<form id="forms" method="POST" action="{{ route('sugestoes') }}" novalidate>
{{ csrf_field() }}
<div class="form-row">
<div class="form-group col-md-12">
<label for="obs">Observações:</label>
<textarea type="text" class="form-control" name="obs" placeholder="Observações" required></textarea>
</div>
</div>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
Remove the #php tag below the form, then in router.php
Route::post('/sugestoes', 'PostController#store')->name('sugestoes');
Then in Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function store(Request $request)
{
$name = $request->input('obs');
return redirect('/sugestoes'); // you should have GET in Route.php
//
}
}
Add the following code in your action attribute on the form. It will capture the post URL. When you submit the form it will send the form data to the URL end-point.
action="{{ url('sugestoes')}}"
Then die and dump in your controller store function
public function store(Request $request)
{
dd($request->all());
}
Im trying to POST data to my controller but I'm getting a
MethodNotAllowedHttpException in RouteCollection.php line 219:
error message, here are my files.
my route file
<?php
Route::get('/', function () {
return view('welcome');
});
// Authentication routes
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration routes
Route::get('register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::controllers(['password' => 'Auth\PasswordController',]);
Route::get('/home', 'HomeController#index');
// Using A Route Closure
Route::get('profile', ['middleware' => 'auth', function() {
// Only authenticated users may enter...
Route::auth();
}]);
// practicing using forms for sending data to the DB & populating form fields with DB data
Route::get('profile', 'ProfileController#index');
Route::post('profile/update', 'ProfileController#updateProfile');
profile.blade.php
<form method="POST" action="/profile/update/">
<div class="form-group hidden">
<input type="hidden" name="id" value="<?php echo $users[0]->id;?>">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
</div>
<div class="form-group">
<label for="email"><b>Name:</b></label>
<input type="text" name="name" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<label for="email"><b>Email:</b></label>
<input type="text" name="email" placeholder="Please enter your email here" class="form-control"/>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default"> Submit </button>
</div>
</form>
& my ProfileController.php
<?php
namespace App\Http\Controllers;
use Auth;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ProfileController extends Controller
{
/**
* Update user profile & make backend push to DB
**/
public function index()
{
if(Auth::check()) {
// connecting to the DB and accessing
$users = User::all();
//var_dump($users);
return view('profile', compact('users'));
}
return view('auth/login');
}
public function updateProfile(Requests $request) {
return $request->all();
}
}
not sure what the issue is. Thanks for all the help everyone
A couple of issues that we managed to address here:
Over-use of HTTP Verbs
At your view, you have: <form method="POST" but also <input name="_method" type="hidden" value="PATCH"> which may conflict between a POST and a PATCH. Since your routes.php only declares POST, let's remove the patch definition.
Routing Mistake
Still at your view, your action points to action="/profile/update/" while your route is defined as Route::post('profile/update'), notice the extra / at the end in your form. That slash should not be there.
Controllers Request
You have a here: use App\Http\Requests; is probably incorrect because that's a folder within Laravel, not a class. Let's remove that and keep use Illuminate\Http\Request; for now. In the near future, you'll be learning how to create your own Form Requests and you'll probably want a UpdateProfileRequest.php.
I am new to Laravel and I'm having trouble with posting data to a controller. I couldn't find the corresponding documentation. I want to something similar in Laravel that I do in C# MVC.
<form action="/someurl" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>
Controller
[HttpPost]
public ActionResult SomeUrl(string someName)
{
...
}
You should use route.
your .html
<form action="{{url('someurl')}}" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>
in routes.php
Route::post('someurl', 'YourController#someMethod');
and finally in YourController.php
public function someMethod(Request $request)
{
dd($request->all()); //to check all the datas dumped from the form
//if your want to get single element,someName in this case
$someName = $request->someName;
}
This works best
<form action="{{url('someurl')}}" method="post">
#csrf
<input type="text" name="someName" />
<input type="submit">
</form>
in web.php
Route::post('someurl', 'YourController#someMethod');
and in your Controller
public function someMethod(Request $request)
{
dd($request->all()); //to check all the datas dumped from the form
//if your want to get single element,someName in this case
$someName = $request->someName;
}