I am doing simple cms in laravel 4.1 , i created many form and they working fine, but the last form which i create throws error on submit.
Illuminate \ Database \ Eloquent \ MassAssignmentException
_token
The data posted by form also show on error page.
_token KLlDjuFgaEmuGHKMpFjqSrukYT3sawOYYZLPGxnb
name asdf
body asdfasdfa
bio sdfasdf
So its mean the _token is also posted then why i am getting this error.
My form look like this.
{{ Form::open(array('route' => 'admin.teachers.store','files'=>true)) }}
<ul>
<li>
{{ Form::label('image', 'Image:') }}
{{ Form::file('image') }}
</li>
<li>
{{ Form::label('name', 'Name:') }}
{{ Form::text('name') }}
</li>
<li>
{{ Form::label('body', 'Body:') }}
{{ Form::textarea('body',null,array('class'=>'ckeditor')) }}
</li>
<li>
{{ Form::label('bio', 'Bio:') }}
{{ Form::textarea('bio',null,array('class'=>'ckeditor')) }}
</li>
<li>
{{ Form::submit('Submit', array('class' => 'btn btn-info')) }}
</li>
</ul>
{{ Form::close() }}
I see one related question to _token issue on forum but it didn't help me.
Thanks in advance :)
In fact your error is MassAssignmentException, which means that you are using
Model::create($input);
In your controller and not using
protected $fillable = array('columnA', 'name'...);
or
protected $guarded = array();
In your Model, to tell Laravel which fields of your table are mass assignable.
Take a look at the docs: http://laravel.com/docs/eloquent#mass-assignment
Related
I am trying to edit a record in a table. I have created a route and the form, but I can't get past this error. I have figured out the problem but I can't find a fix. Am I correct in thinking that the edit.blade.php file needs the $ad->id passing?
The $ad->id is an ID of a specific add in a List View. The list view has all the tickets displayed from a table, and the below link is meant to edit that one item.
The edit route is accessed using following code:
Edit
I have one route that is supposed to open up the edit view form:
Route::get('/ticket_ads/edit/{ad}', 'TicketAdsController#editTicketAdForm')->name('ticket.edit');
The above route points to this in the controller:
public function editTicketAdForm($id)
{
//$ad = DB::table('ticket_ads')->where('id', $id)->value('id');
return view('Ads.edit')->with('id', $id);
}
This is the view called by the above function:
#extends('Shared.Layouts.MasterWithoutMenus')
#section('title')
Edit a ticket ad
#stop
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h2>Edit your ticket ad</h2></div> <br/>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title', Input::old('title'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('comment', 'Comment') }}
{{ Form::text('comment', Input::old('comment'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
#endsection
This is the line that throws the error
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
The ID displays normally in the URL as ticket_ads/edit/7 for example.
How do I get past this?
Change this line:
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
to this:
{{Form::open(array('route' => array('ticket.edit', $id)))}}
This
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
is wrong. Correct syntax is:
{{ Form::open(['route' => ['ticket.edit', $id]]) }}
also you should safely ditch using array() in favor of [] syntax as Laravel requires PHP 5.4+ anyway (unless you are using ancient version of Laravel, like v4?)
The correct syntax for calling route is.
{{ route('/cardetails', ['121','cars'] ) }}
In URL it will be like this below line.
127.0.0.1:8000/cardetails/121/cars
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
I have form:
<form action="{{ path('book_create') }}" method="post" {{ form_enctype(form) }}>
{{ form_start(form) }}
{{ form_row(form.bookFoto) }}
{{ form_row(form.bookTitle) }}
{{ form_row(form.categories) }}
<p class="new_category">+ Add category</p>
{{ form_row(form.authors) }}
<p>+ Add author</p>
{{ form_end(form) }}
When I click 'Add category' I load with AJAX form for create new Category Entity, and save it with AJAX too.
But I don't understand how can I update entity field type categories without reloading form.
To deal with embedded collection you need to manage your form with JS. Symfony has built-in helper for it. It is called prototype and used for populating form with new rows of embedded collection:
<ul class="tags" data-prototype="{{ form_widget(form.categories.vars.prototype)|e }}">
...
</ul>
You can read more at official documentation:
http://symfony.com/doc/current/cookbook/form/form_collections.html
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'));
}
i'm using Blade template Engine in Laravel. and i want to desing forms with twitter bootstrap.
My sample Form
#section('content')
<div style='margin:20px auto;height:100px;width:300px;background-color: #fff;line-height: 1.5px'>
{{ Form::open(array('route'=>'auth', 'method'=>'post')) }}
{{ Form::text('username', Input::old('username'), array('placeholder'=>'Username', 'id'=>'username')) }}
{{ Form::password('password', array('placeholder'=>'Password', 'id'=>'password')) }}
{{ Form::submit('Login', array('id'=>'submit')) }}
{{ Form::close() }}
</div>
#stop
in this sample. how to setting that for bootstrap?
To apply the Bootstrap style to your forms, you need to pass the class "form-control" into your blade:
{{ Form::text('username', Input::old('username'), array('placeholder'=>'Username', 'id'=>'username', 'class' => 'form-control')) }}
Styling laravel/blade forms with bootstrap:
["class"=>"form-control"]