hariprasatn
4 months ago
213
7
Laravel

Can't save fetched login user name to relationship

Posted 4 months ago by hariprasatn

i creating a form to user once login, so they have to insert all in empty field except for the user name because i fetch from registered username. the database table for this from and registered user is different, so i create relationship for login user and this form. i don't know where is the error, with this code once i fill-up and submit the form it didn't show any error

my blade

<form method="POST" action="{{ route("admin.customers.store") }}" enctype="multipart/form-data">
    <h2 style="text-align: center"> User Details Form</h2> <br>
    <div class="row">
        @csrf
        <div class="col-md-6">
            <label for="name">{{ trans('cruds.customer.fields.user') }}</label>
            <input class="form-control select2" type="text" name="name" value="{{ auth()->user()->name }}">
        </div>

        <div class="col-md-6 {{ $errors->has('company_name') ? 'has-error' : '' }}">
            <label class="required" for="company_name">{{ trans('cruds.customer.fields.company_name') }}</label>
            <input class="form-control" type="text" name="company_name" id="company_name" value="{{ old('company_name', '') }}" required>
            @if($errors->has('company_name'))
                <span class="help-block" role="alert">{{ $errors->first('company_name') }}</span>
            @endif
            <span class="help-block">{{ trans('cruds.customer.fields.company_name_helper') }}</span>
        </div>

controller


    public function store(StoreCustomerRequest $request)
    {
        $customer = Customer::create($request->all());

        $attributes = request()->validate([
            'name' => 'required'
        ]);
        auth()->user()->save($attributes);
        
        return redirect()->route('admin.customers.index');
    }

Or

If i use the below code it works perfectly but the login user can select other users too, i dont want the login user preview or select other users name.

maybe if you can help me solve this part then the top part i no need to do.

<div class="form-group {{ $errors->has('user') ? 'has-error' : '' }}">
    <label for="user_id">{{ trans('cruds.customer.fields.user') }}</label>
    <select class="form-control select2" name="user_id" id="user_id">
        @foreach($users as $id => $user)
            <option value="{{ $id }}" {{ old('user_id') == $id ? 'selected' : '' }}>{{ $user }}</option>
        @endforeach
    </select>
    @if($errors->has('user_id'))
        <span class="help-block" role="alert">{{ $errors->first('user_id') }}</span>
    @endif
    <span class="help-block">{{ trans('cruds.customer.fields.user_helper') }}</span>
</div>

Please sign in or create an account to participate in this conversation.