user2070364
user2070364

Reputation: 88

Why would a laravel route work, passing parameters, when used as a link but not when used in a redirect, generating a missing argument 1 error?

Routes - I have 2 routes related to this

Route::resource('items', 'ItemsController');
Route::get('process/{process}/items', 'ItemsController@index');

When I use the 2nd, the index function (in the controller mentioned above) picks up the process id and runs without a hitch.

This is the link on a separate view which uses the 2nd route listed above:

{{ HTML::link('process/'.$process->id.'/items', 'Manage Items', array('class' =>'btn btn-primary')) }}

When I use a redirect from the update function in the same controller I get

"Missing argument 1 for ItemsController::index()"

which is the function that accepts the parameter so it can display all the items with that id.

It doesn't seem to matter what I use. Here are some of the statements I've tried in order to redirect to the index function:

  • return Redirect::route('items.index', array($data['process_id']));

  • return Redirect::action('ItemsController@index', array($data['process_id']));

I've also tried using "with(...)"

The following (using either route or action) gives me a "route not defined" error:

return Redirect::action('process/'.$data['process_id'].'/items');

I don't think its good practice to recreate the view as in the index function. I should just be able to redirect and have done with it.

What am I doing wrong?

The model relatioships are as follows: 1 project hasmany items 1 item hasmany attributes 1 attribute hasmany extensions

controller source code

<?php

class ItemAttributesController extends \BaseController {

    /**
     * Display a listing of itemattributes
     *
     * @return Response
     */
    public function index($item_id)
    {
        return $this->makeIndex($item_id);
    }

    /**
     * Show the form for creating a new itemattribute
     *
     * @return Response
     */
    public function create()
    {
        return View::make('item_attributes.create');
    }

    /**
     * Store a newly created itemattribute in storage.
     *
     * @return Response
     */
    public function store()
    {
        $validator = Validator::make($data = Input::all(), Itemattribute::$rules);

        if ($validator->fails())
        {
            return Redirect::back()->withErrors($validator)->withInput();
        }

        $attribute = Itemattribute::create($data);

        // add created attribute id to sequence in the item for this attribute
        $item = Item::findOrFail($attribute->item_id);

        // get the sequence data
        // append the attribute id
        if (isset($item->attribute)){
            $item->attribute = $item->attribute.', '.$attribute->id;
        } else {
            $item->attribute = $attribute->id;
        }

        $item->save();

        return $this->makeIndex($data['item_id']);  

    }

    /**
     * Display the specified itemattribute.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $itemattribute = Itemattribute::findOrFail($id);

        return View::make('item_attributes.show', compact('itemattribute'));
    }

    /**
     * Show the form for editing the specified itemattribute.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $itemattribute = Itemattribute::find($id);

        return View::make('item_attributes.edit', compact('itemattribute'));
    }

    /**
     * Update the specified itemattribute in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $itemattribute = Itemattribute::findOrFail($id);

        $validator = Validator::make($data = Input::all(), Itemattribute::$rules);

        if ($validator->fails())
        {
            return Redirect::back()->withErrors($validator)->withInput();
        }

        $itemattribute->update($data);

        return $this->makeIndex($data['item_id']);  
    }

    /**
     * Remove the specified itemattribute from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $attribute = Itemattribute::findOrFail($id);

        //find the item
        $item = Item::findOrFail($attribute->item_id);

        // get the sequence string
        if (isset($item->attribute)){
            // convert to array
            $arr=explode(",",$item->attribute); 
            // remove item
            if(($key = array_search($id, $arr)) !== false) {
                unset($arr[$key]);
            }
            // convert back to string and replace initial string
            $item->attribute = implode(",", $arr);
            // save
            $item->save();
        }

        ItemAttribute::destroy($id);

//      return Redirect::route('item_attributes.index');
        return $this->makeIndex($attribute->item_id);   
    }

    private function makeIndex($item_id){
        $item = Item::findOrFail($item_id);
        $project = Project::findOrFail($item->project_id);
        $item_attributes = DB::table('item_attributes')->where('item_id', $item->id)->get();

        return View::make('item_attributes.index',  compact('item_attributes', 'item', 'project'));
//      return Redirect::to('item_attributes', );
    }
}

routes source code

Route::get('/', function()
{
    return View::make('home');
});

Route::resource('projects', 'ProjectsController');
Route::resource('items', 'ItemsController');
Route::resource('item_attributes', 'ItemAttributesController');
Route::resource('attribute_extentions', 'AttributeExtensionsController');


Route::get('project/{projects}/items', 'ItemsController@index');
Route::get('project/{projects}/item/create', 'ItemsController@create');

Route::get('item/{items}/item_attributes', array('as' => 'item_attributes', 'uses' => 'ItemAttributesController@index'));
Route::get('item/{items}/attribute_extentions', 'AttributeExtensionsController@index');

Upvotes: 0

Views: 1490

Answers (2)

user2070364
user2070364

Reputation: 88

Fixed it!

The solution was to use

return Redirect::to('item/'.$item->id.'/item_attributes');

This correctly passed the parameter in way that it could be picked up as a parameter by the function.

Thanks for all your help!

Upvotes: 0

David Nguyen
David Nguyen

Reputation: 8508

You are not using the route action correctly, it should point to the controller@function.

return Redirect::action('ItemsController@index', array('item_id' => 1));

Upvotes: 0

Related Questions