Laravel:将2个文本字段分成1个数据库记录

时间:2015-06-01 11:32:31

标签: php mysql laravel

我有一个用公司编辑用户的表单。 我与公司(用户被添加到)的disabled fieldaccount id的字段。

现在我希望将tag + id添加到同一列account_id

例如:

John为Invis工作。

enter image description here

INV-012345将上传到数据库列account_id

目前它只会将012345上传到数据库。

现在的问题是,我不知道如何将这两个字段上传到account_id列。

HTML /刀片

<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>

控制器

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');
}

1 个答案:

答案 0 :(得分:3)

如果我理解你要做的事情,那就是这样。

您需要细分保存的内容并连接两部分数据。

首先在你的刀片中将它们作为单独的表单字段,例如

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

运行更新用户的功能时使用此功能

$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();