I have used this function in controller to store the records of the users with validations. The data is not stored if the validation is not met but it doesnot show any validation error message.
public function store()
{
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if ($validation->passes())
{
User::create($input);
return Redirect::route('users.index');
}
return Redirect::route('users.create')
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
I have the model:
<?php
namespace App;
class User extends BaseModel{
protected $fillable = [
'name', 'email', 'password', 'phone'
];
protected $hidden = [
'password', 'remember_token',
];
public static $rules = array(
'name' => 'required|min:5',
'email' => 'required|email');
}
users.create view file:
#extends('layouts.user')
#section('main')
<h1>Create User</h1>
{{ Form::open(array('route' => 'users.store')) }}
<ul>
<li>
{{ Form::label('name', 'Name:') }}
{{ Form::text('name') }}
</li>
<li>
{{ Form::label('username', 'Username:') }}
{{ Form::text('username') }}
</li>
<li>
{{ Form::label('password', 'Password:') }}
{{ Form::password('password') }}
</li>
<li>
{{ Form::label('password', 'Confirm Password:') }}
{{ Form::password('password_confirmation') }}
</li>
<li>
{{ Form::label('email', 'Email:') }}
{{ Form::text('email') }}
</li>
<li>
{{ Form::label('phone', 'Phone:') }}
{{ Form::text('phone') }}
</li>
<li>
{{ Form::submit('Submit', array('class' => 'btn')) }}
</li>
</ul>
{{ Form::close() }}
#stop
Taken from Laravels Documentation
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Related
I'm new in laravel and i'm trying to get model post from view in controller method. I used Collective\Html\FormFacade for a view. My view code is below;
{!! Form::model($employee,array("url" => "employee/edit" , "method"=>"POST")) !!}
<ul>
<li>
{{ Form::label('Name', 'Name') }}
</li>
<li>
{{ Form::text('Name') }}
</li>
</ul>
<ul>
<li>
{{ Form::label('Job', 'Job') }}
</li>
<li>
{{ Form::text('Job') }}
</li>
</ul>
<ul>
<li>
{{ Form::label('Salary', 'Salary') }}
</li>
<li>
{{ Form::text('Salary') }}
</li>
</ul>
{{ Form::submit('Update Employee!') }}
{!! Form::close() !!}
My question is how can i get this post data as model in controller ?
The easiest way is to instantiate a new model with it.
$employee = new Employee($request->all())
If you are updating the record then retrieve it from the database using the id
public function update(Request $request, $id) {
$employee = Employee::find($id);
$employee->fill($request->all());
$employee->save();
// ...
}
I suggest reading https://laravel.com/docs/5.2/eloquent#basic-updates
Hi guys able to simple validate the login details and signup details but unable to insert and fetch database of phpmyadmin using laravel 4.2.
Can you help with this?
Following is my code for models
class Website extends Eloquent implements UserInterface, RemindableInterface
{
use UserTrait,RemindableTrait;
protected $table = 'websites';
protected $hidden = array('password', 'remember_token');
protected $protected = array();
protected $fillable = array('name',email','username','password','confirm_password','phone');
}
here is code for view file
here is view file
signup.blade.php
{{ Form::open(array('url' => 'register')) }}
#if($errors->has('name'))
{{ $errors->first('name') }}
#endif
{{ Form::label('name', 'Name:') }}
{{ Form::text('name') }}
#if($errors->has('username'))
{{ $errors->first('username') }}
#endif
{{ Form::label('username', 'Username:') }}
{{ Form::text('username') }}
#if($errors->has('password'))
{{ $errors->first('password') }}
#endif
{{ Form::label('password', 'Password:') }}
{{ Form::password('password') }}
#if($errors->has('password'))
{{ $errors->first('confirm_password') }}
#endif
{{ Form::label('password', 'Confirm Password:') }}
{{ Form::password('confirm_password') }}
#if($errors->has('email'))
{{ $errors->first('email') }}
#endif
{{ Form::label('email', 'Email:') }}
{{ Form::text('email') }}
#if($errors->has('phone'))
{{ $errors->first('phone') }}
#endif
{{ Form::label('phone', 'Phone:') }}
{{ Form::text('phone') }}
</div>
{{ Form::submit('Create', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
here is login.blade.php
{{ Form::open(array('url' => 'login')) }}
<table>
<tr>
<td width="150px" class="table-responsive">
{{ Form::label('username', 'Username:') }}
{{ Form::text('username') }}
#if($errors->has('username'))
<label> {{ $errors->first('username') }} </label>
#endif
</td>
</tr>
<tr>
<td width="150px">
{{ Form::label('password', 'Password:') }}
{{ Form::text('password') }}
#if($errors->has('password'))
<label> {{ $errors->first('password') }} </label>
#endif
</td>
</tr>
<tr>
<td width="150px">
<div class="field">
<input type="checkbox" name="remember" id="remember">
<label for="remember">
Remember me
</td>
</tr>
</div>
<tr>
<td>
{{ Form::submit('Login', array('class' => 'btn btn-primary')) }}
</td>
</tr>
</table>
#if( $errors->count() > 0 )
<p>The following errors have occurred:</p>
<ul id="form-errors">
{{ $errors->first('username', '<li>:message</li>') }}
{{ $errors->first('password', '<li>:message</li>') }}
{{ $errors->first('password_confirmation', '<li>:message</li>') }}
</ul>
#endif{{ Form::close() }}
here is my code for controller for function
WebsiteController.php
<?php
class WebsiteController extends BaseController {
public function index()
{
return View::make('websites.index',compact('websites'));
}
public function login()
{
return View::make('websites.login');
}
public function signup()
{
return View::make('websites.signup');
}
public function show()
{
$input=Input::all();
$messages = array('username.required' => 'Please enter your username','password.required' => 'You have to set a password');
$rules = array( 'username' => 'required|alpha-num','password' => 'required');
$validator = Validator::make($input,$rules,$messages);
if ($validator->fails()) {
print_r($input);
echo 1;
return Redirect::to('login')->withErrors($validator);
}
else
{
return Redirect::to('websites')
->with('message', 'Your account has been created, please login'); print_r($input);
}
}
public function store()
{
$input=Input::all();
$messages = array(
'name.required' => 'Please enter your name',
'email.required' => 'your email address required',
'username.required' => 'Please enter your username',
'password.required' => 'You have to set a password',
'confirm_password.required' => 'Write again your password',
'confirm_password.matchpass' => 'The two passwords does not match');
$rules = array(
'name'=>'required|alpha',
'username' => 'required|alpha-num',
'password' => 'required',
'confirm_password'=>'required',
'email' => 'required|email',
'phone'=>'required|numeric'
);
$validator = Validator::make($input,$rules,$messages);
if ($validator->fails()) {
print_r($input);
echo 1;
return Redirect::to('register')->withErrors($validator)->withInput();
}
else
{
$user = new Website();
$user->username =$input['username'];
$user->email = $input['email'];
$user->password = $input['password'];
$user->name = $input['name'];
$user->phone =$input['phone'];
return Response::json(array('success' => true), 200);
return Redirect::to('login')
->with('global', 'Your account has been created! We have sent an email to activate your account');
// }
}
}
}
?>
You just forgot to save the data in the store method.
Add:
$user->save();
To:
$user = new Website();
$user->username =$input['username'];
$user->email = $input['email'];
$user->password = $input['password'];
$user->name = $input['name'];
$user->phone =$input['phone'];
Final result:
$user = new Website();
$user->username =$input['username'];
$user->email = $input['email'];
$user->password = $input['password'];
$user->name = $input['name'];
$user->phone =$input['phone'];
$user->save();
To get all the result in your index method you need to add this code before the return:
$websites = Website::all();
Final result:
public function index()
{
$websites = Website::all();
return View::make('websites.index',compact('websites'));
}
Plus you have an error in your model, at this line you're missing an apostrophe, this is correct:
protected $fillable = array('name','email','username','password','confirm_password','phone');
I am working on a shopping cart app.
The products created are stored in the database and this works fine.
However, the products are not updating to the database.
I have looked over this and cannot figure out why because I am saving the product info to the db in my postEdit function.
Here is my ProductsController.php
public function postEdit() {
$product = Product::find(Input::get('id'));
if ($product) {
$image = Input::file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$path = public_path('img/products/' . $filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
$product->image = 'img/products/'.$filename;
$product->save();
$product->update(Input::except('image'));
return Redirect::to('admin/products/index')
->with('message', 'Product Updated');
}
return Redirect::to('admin/products/index')
->with('message', 'Something went wrong, please try again');
}
Here is my Product.php model
<?php
class Product extends Eloquent {
protected $fillable = array('category_id', 'title', 'description', 'price', 'availability', 'image');
public static $rules = array(
'category_id'=>'required|integer',
'title'=>'required|min:2',
'description'=>'required|min:20',
'price'=>'required|numeric',
'availability'=>'integer',
'image'=>'required|image|mimes:jpeg,jpg,bmp,png,gif'
);
public function category() {
return $this->belongsTo('Category');
}
}
Here is my index.php for products view
#extends('layouts.main')
#section('content')
<div id="admin">
<h1>Products Admin Panel</h1><hr>
<p>Here you can view, edit, delete, and create new products.</p>
<h2>Products</h2><hr>
<ul>
#foreach($products as $product)
<li>
{{ HTML::image($product->image, $product->title, array('width'=>'50')) }}
{{ $product->title }} -
{{ Form::open(array('url'=>'admin/products/destroy', 'class'=>'form-inline', 'files'=>true)) }}
{{ Form::hidden('id', $product->id) }}
{{ Form::submit('delete', array('onclick'=>'return checkDelete()')) }}
{{ Form::close() }} -
{{ Form::open(array('url'=>'admin/products/toggle-availability', 'class'=>'form-inline', 'files'=>true))}}
{{ Form::hidden('id', $product->id) }}
{{ Form::select('availability', array('1'=>'In Stock', '0'=>'Out of Stock'), $product->availability) }}
{{ Form::submit('Update') }}
{{ Form::close() }}
{{ Form::open(array('url'=>'admin/products/edit', 'files'=>true, 'class'=>'form-inline', 'files'=>true))}}
{{ Form::hidden('id', $product->id) }}
{{ Form::label('title') }}
{{ Form::text('title', $product->title) }}
{{ Form::label('description') }}
{{ Form::textarea('description', $product->description) }}
{{ Form::label('price') }}
{{ Form::text('price', $product->price, null, array('class'=>'form-price')) }}
{{ Form::label('image', 'Choose an image') }}
{{ Form::file('image') }}
{{ Form::submit('edit') }}
{{ Form::close() }}
</li>
#endforeach
</ul>
<h2>Create New Product</h2><hr>
#if($errors->has())
<div id="form-errors">
<p>The following errors have occurred:</p>
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div><!-- end form-errors -->
#endif
{{ Form::open(array('url'=>'admin/products/create', 'files'=>true)) }}
<p>
{{ Form::label('category_id', 'Category') }}
{{ Form::select('category_id', $categories) }}
</p>
<p>
{{ Form::label('title') }}
{{ Form::text('title') }}
</p>
<p>
{{ Form::label('description') }}
{{ Form::textarea('description') }}
</p>
<p>
{{ Form::label('price') }}
{{ Form::text('price', null, array('class'=>'form-price')) }}
</p>
<p>
{{ Form::label('image', 'Choose an image') }}
{{ Form::file('image') }}
</p>
{{ Form::submit('Create Product', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
</div><!-- end admin -->
#stop
Here is my database table info:
public function up()
{
Schema::create('products', function($table){
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->string('title');
$table->text('description');
$table->decimal('price', 6, 2);
$table->boolean('availability')->default(1);
$table->string('image');
$table->timestamps();
});
}
I think you need to swap the update and save functions around:
$product->update(Input::except('image'));
$product->save();
I'm running on Lavarel and crossing this code:
{{ link_to_route('users.edit', 'Edit', array($user->id), array('class' => 'btn btn-info')) }}
This is the ./app/views/users/edit.blade.php
#extends('users.scaffold')
#section('main')
<h1>Edit User</h1>
{{ Form::model($user, array('method' => 'patch', 'route' => array('users.update', $user->id))) }}
<ul>
<li>
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</li>
<li>
{{ Form::label('password', 'Password: ') }}
{{ Form::text('password') }}
</li>
<li>
{{ Form::label('email', 'Email: ') }}
{{ Form::text('email') }}
</li>
<li>
{{ Form::label('phone', 'Phone: ') }}
{{ Form::text('phone') }}
</li>
<li>
{{ Form::label('name', 'Name: ') }}
{{ Form::text('name') }}
</li>
<li>
{{ Form::submit('Update', array('class' => 'btn btn-info')) }}
{{ link_to_route('users.show', 'Cancel', $user->id, array('class' => 'btn')) }}
</li>
</ul>
{{ Form::close() }}
#if (($errors->any()))
<ul>
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
#endif
#stop
The above code in the template file edit.blade.php, and when users click to the Edit button, it should pass the user id $user->id to the controller UsersController#edit where edit action is defined,
public function edit($id)
{
$user = User::find($id);
if(is_null($user)) {
return 'Not found: '.$id;
// return Redirect::route('users.index');
}
return Redirect::route('users.edit', compact('user'));
}
The problem here is that $id is not what passing from link_to_route() function.
Can anyone help me find out where the problem is? Thanks.
Here the DOM result:
Edit
This is the routes.php
Route::resource('users', 'UsersController');
This is result after clicking Edit button:
Not found: {"id":1,"username":"john","password":"johndoe","email":"johndoe#gmail.com","phone":"123456","name":"John","created_at":"2013-06-07 08:13:28","updated_at":"2013-06-07 08:13:28"}
Based upon your info - it looks like the problem is not with link_to_route() - you can see the error is further in your code.
It looks like you have bound the route to the model, so in your edit function $id is actually the $user already populated from the database.
You can tell that is the case - because your custom "not found" error gives you the user data.
If you change your code to this - does it work?
public function edit(User $user)
{
return Redirect::route('users.edit', compact('user'));
}
So i am trying to do a Login and Registration system using Laravel Framwork. I dont get any errors when i run it but when i am trying to create a user it wont go into the database.
Here is my function.
public function postRegister()
{
$input = Input::all();
$rules = array("username" => "required|unique:users|email", "password" => "required");
$v = Validator::make($input, $rules);
if($v->passes())
{
$user = new User();
$password = $input["password"];
$password = Hash::make($password);
$user = new User();
$user->username = $input["username"];
$user->email = $input["email"];
$user->password = $password;
$user->save();
return Redirect::to("login");
echo 'Succes, now you can log in!';
} else{
return Redirect::to("register");
echo 'Someting went wrong, try again!';
}
}
And here you can see my register.blade
<!doctype html>
<html>
<head>
<title>Register</title>
</head>
<body>
{{ Form::open(array('url' => 'register')) }}
<h1>Register</h1>
<!-- if there are login errors, show them here -->
#if (Session::get('registerError'))
<div class="alert alert-danger">{{ Session::get('registerError') }}</div>
#endif
<p>
{{ $errors->first('username') }}
{{ $errors->first('email') }}
{{ $errors->first('password') }}
</p>
<p>
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username'), array('placeholder' => 'Choose Username')) }}
</p>
<p>
{{ Form::label('email', 'Email Address') }}
{{ Form::text('email', Input::old('email'), array('placeholder' => 'awesome#awesome.com')) }}
</p>
<p>
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
</p>
<p>{{ Form::submit('Register') }}</p>
<p>{{HTML::link("login", "Back to login")}}</p>
{{ Form::close() }}
</body>
</html>
Maybe I am just missing something!