2

I'm into routing now via use of resource routing

here is my code in router,

Route::resource('item-sales', 'ItemSalesController');

Here is my code in my controller

return View::make('item-sales.create')

When I return the view it does not make the URL I need it shows,

URL - item-sales/

What I need/Expected output of URL is,

URL - item-sales/create


Here's my controller

 public function store()
        {
               $id = Input::get('item_id');
                $new_item = Item::find($id);
                $new_qty = Input::get('item_quantity');
                $total = $new_item->item_price * $new_qty;
                Session::put('added-items', [
                    0 => [
                        'item_id'       => $id,
                        'item_name'     => $new_item->item_name,
                        'item_price'    => $new_item->item_price,
                        'item_category'    => $new_item->category,
                        'item_quantity' => Input::get('item_quantity')
                    ]
                ]);
                $array = Session::get('added-items');
                $total = number_format($total, 2, '.', ',');
                return View::make('item-sales.create')
                ->with('items',$array)
                ->with('total',$total);
    }
6
  • route correspond to the method used in the controller and not the rendered view. You are working in which method/action ? Commented Aug 28, 2014 at 10:49
  • What do you mean ? what would I need is to fix URL to display it as item-sales/create not item-sales, its action is from controller @webNeat Commented Aug 28, 2014 at 10:54
  • 1
    I mean if your code return View::make('item-sales.create') is written in the index method of your controller. The URL will be /item-sales. check this laravel.com/docs/controllers#resource-controllers Commented Aug 28, 2014 at 10:58
  • yes it is in index how can I able to use /item-sales/create ? Commented Aug 28, 2014 at 11:15
  • Please add the code of your controller to your question so that we can help you Commented Aug 28, 2014 at 11:41

2 Answers 2

1

If you need your own custom methods and individual routes with a single route, then use the restful routing.

In Routes.php:

Route::controller('item-sales', 'ItemSalesController');

In ItemSalesController.php:

public function getCreate() {
   $id = Input::get('item_id');
   $new_item = Item::find($id);
   $new_qty = Input::get('item_quantity');
   $total = $new_item->item_price * $new_qty;
   Session::put('added-items', [
             0 => [
                    'item_id'       => $id,
                    'item_name'     => $new_item->item_name,
                    'item_price'    => $new_item->item_price,
                    'item_category'    => $new_item->category,
                    'item_quantity' => Input::get('item_quantity')
                  ]
   ]);
   $array = Session::get('added-items');
   $total = number_format($total, 2, '.', ',');

   return View::make('item-sales.create')
              ->with('items',$array)
              ->with('total',$total);
}

Then in Url: route to: item-sales/create

Sign up to request clarification or add additional context in comments.

Comments

0

Check the Resource Controller in documentation. It's action is mapped to methods using HTTP verbs. So, for example, To show a form for creating a resource the method should be create and request method should be GET and URL should be /item-sales/create.

Verb         Path                         Action    Route Name

GET          /resource                    index     resource.index

// Follow this one

GET          /item-sales/create           create    item-sales.create

POST         /resource                    store     resource.store
GET          /resource/{resource}         show      resource.show
GET          /resource/{resource}/edit    edit      resource.edit
PUT/PATCH    /resource/{resource}         update    resource.update
DELETE       /resource/{resource}         destroy   resource.destroy

To make sure about routes and route name's run php artisan routes from your command prompt (terminal) and use the route names and urls exactly.

The store method is for saving and used POST method/http verb and the URL would be /item-sales. To show a form for creating new resource (By taking user inputs in the form) you should use create method and in this case the http verb would be GET. So your action should be like this:

public function create()
{
    // Return the view (empty form)
    // app/views/item-sales/create.blade.php
    return Vire::make('item-sales.create');
}

To access/invoke this method you should use item-sales/create as URL. Then when you submit the form for saving, use the store method, so, form's action could be something like route('item-sales.store').

7 Comments

Should I declare another route to custom it as item-sales/create ?
No, you should use Laravel's artisan command to create your resource controller, php artisan controller:make YourController and Laravel will create all the methods for you.
I already do that, I have this controller with boiler I needed, but when I tried to return make a view it does fine but the URL seems wrong.
What is the method/action you used to show the form ?
its, return View::make('item-sales.create') @WereWolf
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.