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'));
}
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 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 trying to create a form to delete a row in my db:
#foreach ($CostStatuses as $CostStatus)
<tr>
<td class="table-text">
<div>
<a href="/costStatus/{{ $CostStatus->id }}/edit">
{{ $CostStatus->status_name }}
</a>
</div>
</td>
<td>
{{ Form::open(array('url' => 'CostStatus/' . $CostStatus->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete this Nerd', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
</td>
</tr>
#endforeach
But the html shows the form inside the {{ }} as a text to the user.
That's because {{}} does not execute the contents inside. Use {!! !!}.
LAready fixed:
Just change {{ for {!!.
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();