2

I have a controller that receives input from a from. My controller handles both the create and the update in different functions.

Is there a way that I can create an array / function of sorts that I can then call in both locations so that I don't have to repeat the same code bellow in the create and update functions?

$client->company_name           = Input::get('company_name');
$client->telephone              = Input::get('telephone');
$client->website                = Input::get('website');
$client->building_name          = Input::get('building_name');
$client->street_address         = Input::get('street_address');
$client->town                   = Input::get('town');

I've created the rules array (below), I just need to try to do the same for the input values.

/**
 * create the validation rules for database input
 *
 * @return array
 */
private function rules()
{

    $rules = array(
        'company_name'      => 'required',
        'telephone'         => 'required',
        'website'           => 'url',
        'building_name'     => '',
        'street_address'    => '',
        'town'              => '',
    );

    return $rules;

}

Thanks all, I'd appreciate any assistance.

2
  • Yes, you can. But was in exactly your problem? What's your question? With what you have a problem? Commented Sep 25, 2014 at 18:31
  • Thanks for the quick response Marcin. I'm trying to prevent having to repeat the same code twice in 2 different functions so I need a method that will allow me to create it once (the first code block above) and then call it in 2 seperate functions. Commented Sep 25, 2014 at 18:42

1 Answer 1

1

If I understand you well, you need to use this function

function modifyClient($client) {

    $client->company_name           = Input::get('company_name');
    $client->telephone              = Input::get('telephone');
    $client->website                = Input::get('website');
    $client->building_name          = Input::get('building_name');
    $client->street_address         = Input::get('street_address');
    $client->town                   = Input::get('town');

}

and if you create new client you can use:

$client = new Client;
modifyClient($client);

and if you modify you can do:

$client = Client::find($id);
modifyClient($client);

And after both of them you can save it into DB using

$client->save();
Sign up to request clarification or add additional context in comments.

Comments

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.