Hi all.
I am pretty close to a solution but am stuck. I have a category page where the category is created and deleted on the same page. I am adding a edit function, edit link, and making it to where the user can edit the category on the same page instead of creating another static page and repeating the same code.
Here is my controller function for update
public function postEdit() {
$category = Category::find(Input::get('id'));
if ($category) {
$category->update();
return Redirect::to('admin/categories/index')
->with('message', 'Category Updated');
}
return Redirect::to('admin/categories/index')
->with('message', 'Something went wrong, please try again');
}
Here is the category page where a user can view, create, edit, and delete the categories listed I am trying to have a conditional that states to show create form as default and only show edit form when edit button is clicked.
@extends('layouts.main')
@section('content')
<div id="admin">
<h1>Categories Admin Panel</h1><hr>
<p>Here you can view, delete, create, and edit new categories.</p>
<h2>Categories</h2><hr>
<ul>
@foreach($categories as $category)
<li>
{{ $category->name }} -
{{ Form::open(array('url'=>'admin/categories/destroy', 'class'=>'form-inline')) }}
{{ Form::hidden('id', $category->id) }}
{{ Form::submit('delete') }}
{{ Form::close() }}
{{ Form::open(array('url'=>'admin/categories/edit', 'class'=>'form-inline'))}}
{{ Form::hidden('id', $category->id) }}
{{ Form::submit('edit') }}
{{ Form::close() }}
</li>
@endforeach
</ul>
<h2>Create New Category</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
<php
if(isset(url=>'admin/categories/destroy')) {
{{ Form::open(array('url'=>'admin/categories/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
}else{
{{ Form::open(array('url'=>'admin/categories/edit')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
}
?>
</div><!-- end admin -->
@stop
Please give me an example of how I can accomplish this on the same view page. I know I somehow have to pass in a variable as the value in the form to fill the category selected.
Thank you for the help.
I decided to use a javascript onclick event and grab the link id to show the form when the button is clicked. However, for some reason I cannot get it to display the form still.
Here is the updated category view file
@extends('layouts.main')
@section('content')
<div id="admin">
<h1>Categories Admin Panel</h1><hr>
<p>Here you can view, delete, create, and edit new categories.</p>
<h2>Categories</h2><hr>
<ul>
@foreach($categories as $category)
<li>
{{ $category->name }} -
{{ Form::open(array('url'=>'admin/categories/destroy', 'class'=>'form-inline')) }}
{{ Form::hidden('id', $category->id) }}
{{ Form::submit('delete') }}
{{ Form::close() }}
{{ HTML::Link('#', 'edit', array('class'=>'form-inline', 'id'=>'button', 'onclick'=>'showEditForm(this)'))}}
</li>
@endforeach
</ul>
<h2>Create New Category</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/categories/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
</div><!-- end admin -->
@stop
<script>
function showEditForm(id) {
id.innerHTML="
{{ Form::open(array('url'=>'admin/categories/edit')) }}{{ Form::hidden('id', $category->id) }}
{{ Form::label('name') }}
{{ Form::text('name') }}{{ Form::submit('Edit Category', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
";
}
</script>
Although you can be using different view files, even this approach is okay and can be as simple as passing a variable :
So, for example you access this page via route : GET /admin/category
( View : category.index) :
public function index()
{
$categories = Category::all();
return View::make('admin.category.index', compact('categories'));
}
And to create a new category `POST /admin/category' :
public function create()
{
Category::create(Input::all());
return Redirect::back()->withSuccess('New category created.');
}
Now, to show form to edit a category use GET /admin/category/{category_id}/edit
public function edit($id)
{
$categories = Category::all();
return View::make('admin.category.index', ['id_to_edit' => $id]);
}
And the last one to make the edit by PUT /admin/category/{category_id}/edit
public function update($id)
{
Category::find($id)->update(Input:all());
return Redirect::back()->withSuccess("Category $id updated.");
}
Now just make a conditional juggle in your view.
admin/category/index.blade.php
...
@foreach ($categories as $category)
//...show category index
@endforeach
if( isset( $id_to_edit ) ) {
//... show form to edit category
} else {
//... show form to create new category.
}
...
Also, in real world scenario there is a lot you have to do. Like validation and stuff...
Thank you @mcraz. I was missing the foreach loop logic in my view. This is what I needed.
Your logic looks right, I am having a hard time implementing it with what I already have. I know that I will have to have a link or button that will call the edit function in my view. Then I will have a condition in view that shows if id is set or clicked on then show edit form I am not sure how to call the id in the form and what id it is. Here is what I have so far. I think the edit and update function are correct
<?php
class CategoriesController extends BaseController {
public function __construct() {
parent::__construct();
$this->beforeFilter('csrf', array('on'=>'post'));
$this->beforeFilter('admin');
}
public function getIndex() {
return View::make('categories.index')
->with('categories', Category::all());
}
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')
->withErrors($validator)
->withInput();
}
public function postDestroy() {
$category = Category::find(Input::get('id'));
if ($category) {
$category->delete();
return Redirect::to('admin/categories/index')
->with('message', 'Category Deleted');
}
return Redirect::to('admin/categories/index')
->with('message', 'Something went wrong, please try again');
}
public function postEdit($id) {
$categories = Category::all();
return View::make('admin.category.index', ['id_to_edit' => $id]);
}
public function postUpdate($id) {
Category::find($id)->update(Input::all());
return Redirect::back()->with('message','Category $id updated.');
}
}
Here is my view for categories
extends('layouts.main')
@section('content')
<div id="admin">
<h1>Categories Admin Panel</h1><hr>
<p>Here you can view, delete, create, and edit new categories.</p>
<h2>Categories</h2><hr>
<ul>
@foreach($categories as $category)
<li>
{{ $category->name }} -
{{ Form::open(array('url'=>'admin/categories/destroy', 'class'=>'form-inline')) }}
{{ Form::hidden('id', $category->id) }}
{{ Form::submit('delete') }}
{{ Form::close() }}
{{ Form::open(array('url'=>'/admin/category/{category_id}/edit')) }}
{{ Form::hidden('id', $category->id) }}
{{ Form::submit('edit') }}
{{ Form::close() }}
</li>
@endforeach
</ul>
@if( isset( $id_to_edit ) ) {
//... show form to edit category
} else {
//... show form to create new category.
}
@endif
<h2>Create New Category</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/categories/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
</div><!-- end admin -->
@stop
Here is my routes
Route::get('/', array('uses'=>'StoreController@getIndex'));
Route::controller('admin/categories', 'CategoriesController');
Route::controller('admin/products', 'ProductsController');
Route::controller('store', 'StoreController');
Route::controller('users', 'UsersController');
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community