Leguam
Leguam

Reputation: 1212

Laravel: 2 text fields into 1 database record

I have a form to edit users with a company. I got a disabled field with the company (which the user is added to) and a field with the account id.

Now I want that the tag + id is added to the same column account_id

So for example:

John works for Invis.

enter image description here

INV-012345 will be uploaded to the database column account_id

As for now it will only upload 012345 to the database.

Now the problem is that I have no clue how both these fields can be uploaded to the account_id column.

HTML/Blade

<div class="form-group  {{ $errors->has('account_id') ? 'has-error' : '' }}">
    {!! Form::label('account_id', trans('common.account_id'), ['class' => 'form-label col-sm-3 control-label text-capitalize']) !!}
    <div class="col-sm-2">
        {!! Form::text('account_id', $user->company->abbreviation, ['class' => 'form-control col-sm-3', 'id' => 'disabledInput', 'disabled']) !!}
    </div>
    <div class="col-sm-4">
        {!! Form::text('account_id', null, ['class' => 'form-control  col-sm-9', 'placeholder' => trans('common.account_id') ])  !!}
        {!! $errors->first('account_id', '<span class="help-block">:message</span>') !!}
    </div>
</div>

Controller

class UserController extends Controller
{
private $user;

public  function  __construct(User $user,CompaniesController $companies, UserTypeController $userType)
{
    $cid = Auth::user()->company_id;

    if(Auth::user()->usertype_id == 7)
    {
        $this->user = $user;
    }
    else
    {
        $array_company_ids = $companies->getCompany_ids($cid);
        $this->user = $user->whereIn('company_id', $array_company_ids);
    }

}

public function index()
{
    $users = $this->user->with('company', 'usertype')->get();

    return view('user.index', compact('users'));
}

public function show()
{
    //
}

public function create(CompaniesController $companies,  UserTypeController $userType)
{
    $companies = $companies->getCompanies(Auth::user()->company_id);
    $usertypes = $userType->getUsertypes();

    return view('user.create', ['usertypes' => $usertypes,'companies' => $companies]);
}

public function store(CreateUserRequest $request, User $user)
{
    $user->fill($request->all());
    $user->password = Hash::make($request->input('password'));
    $user->save();
    return redirect()->route('user.index');
}

public function edit($user_id, CompaniesController $companies,  UserTypeController $userType)
{
    $user = $this->user->with('company')->findOrFail($user_id);
    $companies = $companies->getCompaniesName(Auth::user()->company_id);
    $usertypes = $userType->getUsertypes();

    return view('user.edit', ['user' => $user, 'usertypes' => $usertypes, 'companies' => $companies]);
}

public function update($id, CreateUserRequest $request)
{
    $user = $this->user->find($id);

    $password = $request->input('password');

    if($password)
    {
        $user->fill($request->input())->save();
        $user->password = Hash::make($request->input('password'));

        $user->save();
    }
    else
    {
        $user->fill($request->only('account_id', 'email',  'firstname',
            'lastname', 'middlename', 'usertype_id', 'active',
            'company_id','title','btw_nr','kvk_nr','bic','iban',
            'birthday','mobile','telephone','country','city','postal_code',
            'house_nr','street'));

        $user->save();
    }

    if ($request->input('usertype_id') <= Auth::user()->usertype_id)
    {
        $user->usertype_id = $request->input('usertype_id');

        $user->save();
    }

    $user->update();

    return redirect()->route('user.index');
}


public function destroy($user)
{
    $user->delete();
    return redirect('user');
}

Upvotes: 1

Views: 212

Answers (1)

Chris Townsend
Chris Townsend

Reputation: 2716

This is if I understand what you are trying to do.

You need to break down what you are saving and concatenate the two parts of data.

Firstly in your blade have them as separate form fields e.g.

{!! Form::text('account_id') !!}
{!! Form::text('tag') !!}

Use this when running a function to update a user

$user = $this->user->find($id);

$user->account_id = $request['tag']."-".$request['account_id'];

//Insert all other fields here e.g.
$user->email = $request['email'];

//To save the data use
$user->save();

Upvotes: 3

Related Questions