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
Related
I have a symfony page composed of 3 parts ( and i'm using tags to display each part) by navigation menu here is the navigation menu.
<ul class="nav nav-tabs centered">
<li class="active">
{{ 'fiche_pharmacie'|trans }}
</li>
<li>
{{ 'Mon équipe'|trans }}
</li>
<li>
{{ 'Configuration_automate'|trans }}
</li>
{# <li>
{{ 'Services'|trans }}
</li> #}
</ul>
After that i call 3 twig pages to display each page content.
<div class="tab-pane" id="fiche_patient">
{{ include(':prof/Entreprise:indexdetails.html.twig') }}
</div>
<div class="tab-pane" id="my_team2">
{{ include(':prof/Entreprise:my-team.html.twig') }}
</div>
<div class="tab-pane" id="config">
{{ include(':prof/Entreprise:config.html.twig') }}
</div>
My probleme is after submit i would like to display config.html.twig page.
In my controler i tried :
return $this->redirect($this-> generateUrl('entreprise_index'.'#config'));
but it doesn't work
Any one of you have an idea ?
The correct syntax for what you are trying to do there is:
return $this->redirect($this->generateUrl('entreprise_index').'#config');
You can use redirectToRoute()
$yourRouteParams = array('param1'=>1);
return $this->redirectToRoute('entreprise_index', $yourRouteParams);
You can send the tab name as a parameter to twig. And, in the template, set the active tab according to that parameter.
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'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 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
I would like to check if is last post from loop. I am using pyroCMS. But problem is that if i am using helper:count two times it is not working correctly. How can i assign helper:count to variable and use later variable. Is this possible?
{{ blog:posts limit="5" order-by="title" order-dir="desc" }}
{{ if { helper:count mode="subtract" } == blog:all_posts}}
<li>
<a href="{{ url }}" title="Read more about: {{ title }}">
<span class="naslovna_datum_novice">{{ helper:date format="d.m.Y" timestamp=created_on }} - </span>
{{ title }}
</a>
</li>
{{ elseif { helper:count mode="subtract" } == 5 }}
<li>
<a href="{{ url }}" title="Read more about: {{ title }}">
<span class="naslovna_datum_novice">{{ helper:date format="d.m.Y" timestamp=created_on }} - </span>
{{ title }}
</a>
</li>
{{ else }}
<li class="pikce_spodaj">
<p>
<a href="{{ url }}" title="Read more about: {{ title }}">
<span class="naslovna_datum_novice">{{ helper:date format="d.m.Y" timestamp=created_on }} - </span>
{{ title }}
</a>
</p>
</li>
{{ endif }}
{{ /blog:posts }}
So how to assign {{ helper:count mode="subtract" }} to variable?? How to assign anything to variable?
There's actually a simpler way; the streams core code adds a last property to the final item in an array - source code - that you can just query with a conditional:
{{ if last }} foo {{ endif }}
(The source code that does that is
Here's a working example for blogs I've just tested:
{{ blog:posts limit="5" order_by="title" }}
<h2>{{ title }}</h2>
[...]
{{ if last }}<p>This is the last item</p>{{ endif }}
{{ /blog:posts }}
Also, as Nick points out, you can have more than one counter.