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();
Related
I have a problem with resizing the fields I've generated with symfony, my fields take the whole width of the page. Here is my code:
Controller :
$user= new User();
$form=$this->CreateFormBuilder($user)
->add("firstName",TextType::class)
->add("lastName",TextType::class)
->add("login",TextType::class)
->add("password",PasswordType::class)
->add("tel",TextType::class)
->add("email",TextType::class)
->add("address",TextType::class)
->add("send",SubmitType::class)
-> getForm();
$form->handleRequest($req);
if($form->isValid())
{
$em=$this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
}
return $this->render('eplateformeBundle:Default:register.html.twig',['form'=>$form->createView()]);
The view :
<div class="well">
{{ form_start(form ) }}
{{ form_errors(form) }}
{{ form_row(form.firstName) }}
{{ form_row(form.lastName) }}
{{ form_row(form.login) }}
{{ form_row(form.password) }}
{{ form_row(form.tel) }}
{{ form_row(form.email) }}
{{ form_row(form.address) }}
{{ form_widget(form.send, {'attr': {'class': 'btn btn-primary'}}) }}
{{ form_rest(form) }}
{{ form_end(form) }}
</div>
You can transfer own class to inputs fields like this:
<div class="well">
{{ form_start(form ) }}
{{ form_errors(form) }}
{{ form_row(form.firstName,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.lastName,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.login,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.password,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.tel,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.email,{'attr':{'class':'your own class''}}) }}
{{ form_row(form.address,{'attr':{'class':'your own class''}}) }}
{{ form_widget(form.send, {'attr': {'class': 'btn btn-primary'}}) }}
{{ form_rest(form) }}
{{ form_end(form) }}
</div>
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
I have below problem with FOSUserBundle customization.
I was overwritten FOSUser register form, then I changed register_content from
{{ form_widget(form) }}
to:
<div class="col-md-6">
{{ form_label(form.firstname) }}
{{ form_errors(form.firstname) }}
{{ form_widget(form.firstname) }}
{{ form_label(form.lastname) }}
{{ form_errors(form.lastname) }}
{{ form_widget(form.lastname) }}
</div>
<div class="col-md-6">
{{ form_label(form.email) }}
{{ form_errors(form.email) }}
{{ form_widget(form.email) }}
{{ form_label(form.plainPassword) }}
{{ form_errors(form.plainPassword) }}
{{ form_widget(form.plainPassword) }}
{{ form_label(form.plainPassword.second) }}
{{ form_errors(form.plainPassword.second) }}
{{ form_widget(form.plainPassword.second) }}
</div>
But, when I was went to the browser, I got it:
bad form render
The form renders incorrectly. How I can repair this?
try with this
{{ form_label(form.plainPassword.first) }}
{{ form_errors(form.plainPassword.first) }}
{{ form_widget(form.plainPassword.first) }}
{{ form_label(form.plainPassword.second) }}
{{ form_errors(form.plainPassword.second) }}
{{ form_widget(form.plainPassword.second) }}
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'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'));
}