Laravel Registration System not feeding data - php

I have a login/registration system I am looking to build in Laravel. Currently I am having trouble pulling the information from the form and inputting it into the table. When I submit the form it submits, but the data goes nowhere, I am admittedly new to Laravel.
This is how the form is written:
{{ Form::open() }}
#if (Session::get("error"))
{{ Session::get("error") }}<br />
#endif
{{ Form::label("first_name", "First Name") }}
{{ Form::text("first_name") }}
{{ $errors->first("first_name") }}<br />
{{ Form::label("email", "Email") }}
{{ Form::text('email', Input::old('email')) }}
{{ $errors->first("email") }}<br />
{{ Form::label("password", "Password") }}
{{ Form::password("password") }}
{{ $errors->first("password") }}<br />
{{ Form::label("password_confirmation", "Confirm") }}
{{ Form::password("password_confirmation") }}
{{ $errors->first("password_confirmation") }}<br />
{{ Form::submit("register") }}
{{ Form::close() }}
The user.register (part of a larger controller) which this POSTs to and GETs from is as follows:
public function register()
{
return View::make("user/register");
$validation = Validator::make(Input::all(), User::$rules);
if ($validation->fails())
{
return Redirect::to('register')->withErrors($validation)->withInput();
}
$users = new User;
$users->first_name = Input::get('first_name');
$users->email = Input::get('email');
$users->password = Hash::make(Input::get('password'));
if ($users->save())
{
Auth::loginUsingId($users->id);
return Redirect::to('profile');
}
return Redirect::to('register')-withInput();
}
}
Currently when I submit I get no errors simply a blank redirect to the registration page and nothing ends up in my DB. I am wondering if there is something wrong with my paths? The only other page that is similar to this that I have previously worked on (inputting data to a database) is a page to reset passwords (which works great), but that used a slightly different system through the Auth extension of Laravel.
This I am not familiar with. Can someone point me in the right direction? I have been compiling the knowledge I have gained from guides online but keep ending up in the same place!
Thanks a lot in advance,
Anything else you need (models, routes, etc. I just didn't think they were necessary) lemme know!

Change
return Redirect::to('register')-withInput();
to
return Redirect::to('register')->withInput();
edit:
oh - here is the problem:
public function register()
{
return View::make("user/register");
$validation = Validator::make(Input::all(), User::$rules);
...
remove the "return" function - it should be
public function register()
{
$validation = Validator::make(Input::all(), User::$rules);
...

Related

Symfony: How to check the data sent by form

When I submitted the form with the smyfony4 app, I got the following error.
Since it is an error of the entire form, it is not possible to identify the cause.
I want to debug the value of the submitted form.
What should I do?
I'm sorry for the question like a beginner.
Error
shop => Not a valid value.
{{ form_start(form) }}
{{ #The part that issued the error #}}
{{ form_errors(form) }}
<div class='formGroup'>
{{ form_label(form.tel, 'TEL') }}
{{ form_widget(form.tel) }}
{{ form_errors(form.tel) }}
</div>
.
.
.
{{ form_end(form) }}
$form = $this->createForm(ShopType::class, $shop, array(
"method" => "PUT",
"action" => $this->generateUrl("app_shop_shop_update"),
"em" => $this->getDoctrine()->getManager(),
));
if ($request->isMethod('PUT')) {
if ($form->handleRequest($request)->isValid()) {
// save
$this->get("admin.shopService")->save($shop);
$this->get('session')->getFlashBag()->add('success', 'I saved my shop profile.');
return $this->redirect($this->generateUrl('ahi_sp_admin_shop_shop_edit'));
} else {
$this->get('session')->getFlashBag()->add('error', 'The shop profile could not be saved. Please check the input contents.');
}
}
The function getData()
if ($form->isSubmitted() && $form->isValid()) {
dump ($form->getData());
}
Symfony's developer tools were showing the value I was passing when saving, which was the cause of the error.
When saving the basic data of the shop, validation did not work well, and when I tried to update it again, an error occurred with the basic data that is not in the form, so an error message for each error of the form item It seems that there was no display of.

How to add request data to support email using Illuminate\Http\Request;

I created a "Ask for support" contact form (using a modal) in my app. What would be the best/cleanest way to add/attach a dump of the $request variable? (PHP's global variables, session data, ...) Because I believe this data can help me a lot to debug.
What I tried:
SupportController:
public function send(Request $request)
{
Mail::send('emails.support', ['request' => $request], function ($message) use ($request) {
$message->from($request->user()->email, $request->user()->name);
$message->subject(trans('Support request'));
});
$request->session()->flash('flash_message', __('Message sent!'));
return redirect()->back();
}
emails.support.blade
{{ print_r($request) }}
But I get a memory size exhausted error message (even after I changed the limit to 1GB).
So there might be a better way to do this. Maybe also a more readable way.
Don't dump the entire request object, instead pick and choose what you find necessary to be helpful for debugging. For example:
All:
#foreach($request->all() as $key => $val)
{{ $key }} = {{ $val }}
#endforeach
<hr>
Route Name: {{ $request->route()->getName() }}
Route Action: {{ $request->route()->getAction() }}
Route Method: {{ $request->route()->getMethod() }}
<hr>
Headers:
#foreach($request->headers->all() as $key => $val)
{{ $key }} = {{ $val }}
#endforeach
Etc, etc..
Or you can use Guzzle's str method to serialize a request or response object.

Symfony 2.8 : Rendering Controller (Form Errors Not Showing UP)

I have an issue trying to render a controller which returns a template with formView.
I understood about the sub-request, but I am having difficult time to show any kind of errors.
I think the problem is that after it sees the form is invalid it redirectsToRoute and it looses the POST Request.
If I don't say redirectTo it just renders the view.
base.html.twig
{{ render(controller('AppBundle:Utility:renderSignUpWizard'), {request: app.request}) }}
Utility Controller
/**
* #Route("/registration/wizard/", name="registration.wizard")
*/
public function renderSignUpWizardAction(Request $request)
{
/** #var $user User */
$user = $this->getUser();
$form = $this->createForm(SignUpWizardType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
// save changes to user
$this->persistAndSave($user);
// redirect to profile
return $this->redirectToRoute('profile');
}
else if($form->isSubmitted() && !$form->isValid())
{
return $this->redirectToRoute('home');
}
return $this->render('partials/signup-wizard.html.twig', array
(
'form' => $form->createView(),
));
}
If you could show the twig file where you put the form I could have given a clearer answer. Check the twig file you tell your controller to render and add the following:
Simple way to generate your form(doesn't include errors):
{{ form_start(form) }}
{{ form_widget(form) }
{{ form_end(form) }}
Add:
{{ form_errors(form) }}
if you want erors for a specific field:
{{ form_errors(form.name) }}

Laravel 5.2 correct way to use variables in blade

So I know about passing variables via the controller for instance if its a query array I will do
public function index()
{
$query = Request::get('q');
if ($query) {
$users = User::where('username', 'LIKE', "%$query%")->get();
}
return view('view', compact('users'));
}
And when on the blade I will do
#if( ! empty($users))
#foreach($users as $user)
{{ $user->username }}
#endforeach
#endif
Now my question is how do I set a variable using a variable from the foreach? at the moment I am using PHP inside of the blade template file but I feel this is messy, here is what I have
#if( ! empty($users))
#foreach($users as $user)
<?php
$lastOnline = \Carbon\Carbon::createFromTimeStamp(strtotime($user->last_online))->diffForHumans();
$fiveMinsAgo = \Carbon\Carbon::now()->subMinute(5);
?>
{{ $user->username }}
#if ($user->last_online <= $fiveMinsAgo)
{{ $lastOnline }}
#else
Online Now
#endif
#endforeach
#endif
found a solution to my issue if anyone else is ever looking for it.
public function getLastOnlineAttribute($value)
{
$fiveMinsAgo = \Carbon\Carbon::now()->subMinute(5);
$thirtMinsAgo = \Carbon\Carbon::now()->subMinute(30);
$lastOnline = \Carbon\Carbon::createFromTimeStamp(strtotime($value))->diffForHumans();
if ($value <= $fiveMinsAgo) {
echo 'Last Active: '.$lastOnline.'';
}
else {
echo 'Online Now';
}
}
Basically add this into your model for the variable (eg, if its a $user->last_online it would go into the user model) , it is called a eloquent mutator if you are ever looking for more info, https://laravel.com/docs/master/eloquent-mutators
It grabs your data for the variable for instance {{ $user->last_online }}
Note that the Underscore is transformed into a CamelCase in the function name, the output is set at $value, you can then set variables inside of the function and mould the output however you wish, then in the blade you can get rid of all the extra crap and just use {{ $user->last_online }}

Laravel 4 - Username not being passed on in URL

I am trying to get into Laravel 4 and am having a problem with editing users I have created. So far I have controllers, views, and routes to show a user and then edit the user by clicking the "Edit" button but when I click the submit button I keep getting a NotFoundHttpException and the "Crash/Error" orange and white Laravel screen. I did, however, notice that the URL changes from showing the username (ex - public/users/av1/edit - with av1 being the username) to only saying {username} (ex -public/users/{username}/edit). I am still new to Laravel but my thought is that I'm not passing the username along properly but I know that it could also be the Controller or route as well. I have tried removing and changing sections of code and have found that if I remove the code from the Controller I still get a URL with {username} but I at least don't get the "Crash/Errors" screen. If anyone could help explain where I am going wrong it would be very much appreciated!
Here is my view:
#extends('layout.main')
#section('content')
{{ Form::model($user, array('route'=>'user-edit-post')) }}
<div>
{{ Form::label('username', 'Username:')}}
{{ Form::text('username') }}
#if($errors->has('username'))
{{ $errors->first('username') }}
#endif
</div>
<div>
{{ Form::label('password', 'Password:')}}
{{ Form::password('password') }}
#if($errors->has('password'))
{{ $errors->first('password') }}
#endif
</div>
<div>
{{ Form::label('password_confirmation', 'Confirm Password:')}}
{{ Form::password('password_confirmation') }}
#if($errors->has('password_confirmation'))
{{ $errors->first('password_confirmation') }}
#endif
</div>
<div>
{{ Form::submit('Edit User') }}
</div>
#stop
My User Controller functions that relates to editing:
public function getEdit($username){
$user = User::where('username', '=', $username);
if($user->count()) {
$user = $user->first();
return View::make('users.edit')
->with('user', $user);
} else {
return App::abort(404);
}
}
public function postEdit($username){
$validator = Validator::make(Input::all(),
array(
'first_name' => 'required|max:20',
'last_name' => 'required|max:20',
'email' => 'required|max:50|email',
'username' => 'required|max:20|min:3',
'password' => 'required|min:6',
'password_confirmation' => 'required|same:password'
)
);
if($validator->fails()){
return Redirect::route('user-edit')
->withErrors($validator);
} else {
/*Edit User*/
$user = User::whereUsername($username)->first();
$password = Input::get('password');
$user->password = Hash::make($password);
$user->first_name = Input::get('first_name');
$user->last_name = Input::get('last_name');
$user->email = Input::get('email');
$user->username = Input::get('username');
/*password is the field $password is the variable that will be used in the password field*/
if($user->save()){
return Redirect::route('home')
->with('global', 'The password has been changed.');
}
return Redirect::route('home')
->with('global', 'The password could not be changed.');
}
}
And lastly my Routes:
/*Edit users (GET)*/
Route::get('users/{username}/edit', array(
'as' => 'user-edit',
'uses' => 'UserController#getEdit'
));
/*Edit Order (POST)*/
Route::post('/orders/{orders}/edit', array(
'as' => 'order-edit-post',
'uses' => 'OrderController#postEdit'
));
Change
{{ Form::model($user, array('route'=>'user-edit-post')) }}
To
{{ Form::model($user, array('route'=>array('user-edit-post', $user->username))) }}
Your route need additional parameters, so you need supply your parameters with your route name when binding model to form.
Yep, after reading the comment. I found the reason of your trouble here is the redirect in the postEdit function:
Change:
if($validator->fails()){
return Redirect::route('user-edit')
->withErrors($validator);
}
Into
if($validator->fails()){
return Redirect::route('user-edit', $username)
->withErrors($validator);
}
Again, your route need parameters. When the validation fails, you have been redirected to a wrong URL.

Categories