Support the ongoing development of Laravel.io →
Input Views Forms
Last updated 2 years ago.
0

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')) }}
		<p>
			{{ Form::hidden('id', $category->id) }}
			{{ Form::label('name') }}
			{{ Form::text('name') }}
		</p>
		{{ Form::submit('Edit Category', array('class'=>'secondary-cart-btn')) }}
		{{ Form::close() }}

	";

}
</script>

Last updated 2 years ago.
0

anyone?

Last updated 2 years ago.
0

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...

Last updated 2 years ago.
0

Thank you @mcraz. I was missing the foreach loop logic in my view. This is what I needed.

Last updated 2 years ago.
0

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');
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

swgj19 swgj19 Joined 11 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.