Unable to bind Model with Laravel Form - php

I have the following route entry:
Route::get('admin/user/edit/{id}', 'AdminController#editUser');
And given is controller Method:
public function editUser($id)
{
$user = User::where('id',1);
return View::make('admin.edit_user')
->with('user',$user);
}
All I want to bind model in my edit form which looks like this:
#extends('layouts.admin_master')
#section('content')
<div>
{{ Form::model($user) }}
{{ Form::label('first_name', 'First Name') }}
{{ Form::text('first_name') }}
{{ Form::close() }}
</div>
#stop
I can see text box but value is not being populated. first_name is column in my table users
I was trying to do what mention here

Try this..
public function editUser($id)
{
$user = User::where('id',1)->first();
return View::make('admin.edit_user')
->with('user',$user);
}
#extends('layouts.admin_master')
#section('content')
<div>
{{ Form::model($user) }}
{{ Form::label('first_name', 'First Name') }}
{{ Form::text('first_name',$user->first_name) }}
{{ Form::close() }}
</div>
#stop

Related

MethodNotAllowedHttpException laravel 4.2 error

here is my ContactController.php:
public function destroy($id){
$contact = Contact::find($id);
$contact->delete();
return Redirect::to('http://localhost:8000/contactsview');
}
Here is my rountes.php
Route::delete('/contactsview/destroy/{id}', array('uses'=>'ContactController#destroy'));
Here is my index.blade.php:
{{ Form::open(array('url'=>'/contactsview/delete/'.$contact->id, 'method'=>'DELETE', 'style'=>'display:inline;')) }}
<!-- {{ Form::hidden('id', $contact->id) }} -->
{{ Form::submit('Delete') }}
{{ Form::close() }}
What did I do wrong?
Try the form with this instead, passing in the $contact->id as a param rather than directly in the URL:
{{ Form::open(array('method' => 'DELETE', 'action' => array('ContactController#destroy', $contact->id )) }}

Getting a NotFoundHttpException() Error in laravel

I just have a very simple product category creation form in laravel , like so:
{{ Form::open(array('url'=>'admin/category/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
For the create method i have the following code:
public function postCreate() {
$validator = Validator::make(Input::all() , Category::$rules);
if($validator->passes()) {
$category = new Category;
$category->name = Input::get('name');
$category->save();
return Redirect::to('admin/categories/index')
->with('message' , 'Category created');
}
return Redirect::to('admin/categories/index')
->with('message' , 'something went wrong')
->withError($validator)
->withInput();
}
Now when i click on the submit button, i get the following error:
C:\xampp\htdocs\ecomm\bootstrap\compiled.php
if (!is_null($route)) {
return $route->bind($request);
}
$others = $this->checkForAlternateVerbs($request);
if (count($others) > 0) {
return $this->getOtherMethodsRoute($request, $others);
}
throw new NotFoundHttpException();
}
protected function checkForAlternateVerbs($request)
You can see the error more visvually HERE.
What am i doing wrong ?
Instead of
{{ Form::open(array('url'=>'admin/category/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
try this:
{{ Form::open(array('route'=>'post.homes')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
In routes.php:
Route::post('aboutus', array('as' => 'post.homes', 'uses' => 'HomeController#postContactUs'));

NULL defaults not consistently working, & updates changing NULL to empty string Laravel4

I have a number of fields which are NULL by default in MySQL. For example, in the code snips below, name is required and not null, but name_abbrev, email_general and description are all nullable and set to NULL by default in the database. But data entry via tables in Laravel mostly is not working. If I insert a row with only name and everything else left blank, name of course is entered correctly, and NULL is entered for email_general, but the other two nullable fields post the empty string instead of NULL. If I then update the row, say by changing the name, the update will also change the NULL for email_general to the empty string. And if I manually change the values in the database row for the nullable fields from empty string to NULL, when I update the row (still leaving all the empty fields empty), it changes all the NULL fields to empty string. I can't find anything I'm doing wrong. Why won't it enter NULL (except in one field which is coded exactly the same as the others), and why does update change even that field to the empty string?
Controller:
public function store()
{
$component = new Component;
$component->name = Input::get('name');
$component->name_abbrev = Input::get('name_abbrev');
$component->email_general = Input::get('email_general');
$component->description = Input::get('description'); ...
$component->save();
return Redirect::route('components.index');
}
public function update($id)
{
$component = $this->component->find($id);
$component->name = Input::get('name');
$component->name_abbrev = Input::get('name_abbrev');
$component->email_general = Input::get('email_general');
$component->description = Input::get('description'); ...
$component->save();
return Redirect::route('components.index');
}
create.blade.php:
{{ Form::open(['route' => 'components.store']) }}
<div class="required">
{{ Form::label('name','Name:') }}
{{ Form::text('name') }}
{{ $errors->first('name') }}
</div>
<div>
{{ Form::label('name_abbrev','Abbreviation:') }}
{{ Form::text('name_abbrev', NULL) }}
</div>
<div>
{{ Form::label('email','General Email:') }}
{{ Form::text('email',NULL) }}
</div>
<div>
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',NULL,['size' => '26x3']) }}
</div> ...
<div>
{{ Form::submit('Add New Component', array('class'=>'button')) }}
</div>
{{ Form::close() }}
edit.blade.php:
{{ Form::model($component, array('method'=>'put','route'=>array('components.update', $component->id))) }}
<div class="required">
{{ Form::label('name','Name:') }}
{{ Form::text('name') }}
{{ $errors->first('name') }}
</div>
<div>
{{ Form::label('name_abbrev','Abbreviation:') }}
{{ Form::text('name_abbrev', NULL) }}
</div>
<div>
{{ Form::label('email_general','General Email:') }}
{{ Form::text('email_general', NULL) }}
</div
<div>
{{ Form::label('description','Description:') }}
{{ Form::textarea('description',NULL,['size' => '26x3']) }}
</div> ...
<div>
{{ Form::submit('Update Component', array('class'=>'button')) }}
</div>
{{ Form::close() }}
Thanks very much! This should be easy, but for some reason, it's not.
If you really need null on some column, I would recommend you to use Mutators:
Laravel 3: http://three.laravel.com/docs/database/eloquent#getter-and-setter-methods
Laravel 4: http://laravel.com/docs/eloquent#accessors-and-mutators
So if the new value is an empty string ( ! $value ) overwrite it with NULL.

viewing my database in the view at laravel

sorry if this is a very newbie Q..
but please help me to solve this problem. plus give me the reason about why this error happened..
this is my edit view
new.blade.php
#section('content')
#include('common.show_error')
{{Form::open(array('url'=>'author/update', 'method'=>'PUT'))}}
<p>
{{ Form::label('name', 'Name: ') }}</br>
{{ Form::text('name', $author->name) }}
</p>
<p>
{{ Form::label('bio', 'Biography: ') }}</br>
{{ Form::textarea('bio', $author->bio) }}
</p>
{{ Form::hidden('id', $author->id) }}
<p>{{ Form::submit('Edit Data') }}</p>
#stop
this is my show view
show.blade.php
#extends('layouts.default')
#section('content')
<h1>{{ $author->name }}</h1>
<p>{{ $author->bio }}</p>
<p>{{ $author->updated_at }}</p>
<span>
{{ HTML::linkRoute('authors', 'Home') }} |
{{ HTML::linkRoute('edit_author', 'Edit', array($author->id)) }} |
{{ Form::open(array('url'=>'author/destroy', 'method'=>'DELETE', 'style'=>'display: inline;')) }}
{{ Form::hidden('id', $author->id) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
</span>
#stop
this is my controller
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
Member::where('id','=',$id)->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated');
}
}
the case: when I try to edit data using edit button. it said:
"Trying to get property of non-object laravel"
and when I check at the error log. it refers to
<h1>{{ $author->name }}</h1>
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
$author = Member::find($id);
$author->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated')
->with('author', $author);
}
}
Little changes in your controller, try it :) In your code, you are not send variable "author" into your view.

laravel 4 Auth::attempt allways returns true?

Earlier today I had the exact same problem with Auth::attempt always retuning false. I realized that Auth checks for a hashed password, so by doing so I was able to get it to return true, but now it always does. Even if I type asdadfasdfaf in my form, the if statement loads the page. Any suggestions?
Controller:
class userController extends \BaseController
{
public function login()
{
$user = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if(Auth::attempt($user))
{
return Redirect::route('home');
}
else
{
return View::make('login');
}
}
}
Form
{{ Form::open(array('url' => 'home' )) }}
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}
The Routes file:
Route::post('home', 'userController#login');
No matter what I enter it always directs me to my "home" page?
The action url should be login
{{ Form::open(array('url' => 'login' )) }}
^^^^ as you used Auth::attempt to this URL
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}

Categories