mardi 20 février 2018

Laravel: "override" request object?

I have a function where I save a group. I want to "access" it from the page (when the user makes a new group with a form) and from a controller, too (when a process makes a new group, for example when I create a new tenant)

My code is so far:

.
.
.

$this->saveGroup($tenantId, 'Default group');

.
.
.

public function saveGroup(Request $request, $tenantId = 0, $nameFromFunction = ''){
    if(!empty($request)){
        $name = $request -> name;
    } elseif($nameFromFunction != ''){
        $name = $nameFromFunction;
    } else {
        $name = '';
    }

    if($tenantId > 0 && $name != ''){
        $group = new ConversationGroup;
        $group -> group_name = $name;
        $group -> tenant_id = $tenantId;

        $group->save();
    }

    if($nameFromFunction != ''){
        return $group -> id; //if a function calls it
    } else {
        return redirect("/{$tenantId}/groups"); //if the new group was made from the page
    }
}

If I test it with the page's group creation form it works fine, but when I run it from the controller I got the following error:

GroupController::saveGroup() must be an instance of Illuminate\Http\Request, integer given

What I must change if I want to earn this logic?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire