Laravel not throwing error saving

Published 2 months ago by ArthurDiniz

Hi, im developing my first project, and when i try to save a service it does not save. It just refresh the create page.

Create View


{!! Form::open(['route' => 'services.store','method' => 'post']) !!}
                    {{ Form::label('name','Name:')}}
                    {{ Form::text('name',null,array('class' => 'form-control','required' => '', 'maxlenght' => '50'))}}

                    {{ Form::label('users_id','User:',['style'=>'margin-top: 20px;'])}}
                    {{ Form::text('users_id',$user->id,['disabled'=>'disabled','class' => 'form-control','required' => '', 'maxlenght' => '10'])}}
                    

                    {{ Form::label('phone','Phone:',['style'=>'margin-top: 20px;'])}}
                    {{ Form::text('phone',null,array('class' => 'form-control','required' => '', 'maxlenght' => '10'))}}

                    {{ Form::label('razsocial','Raz Social:')}}
                    {{ Form::text('razsocial',null,array('class' => 'form-control','required' => '', 'maxlenght' => '50'))}}

                    {{ Form::label('seller','Seller:')}}
                    {{ Form::text('seller',null,array('class' => 'form-control','required' => '', 'maxlenght' => '50'))}}     

                    {{ Form::label('sellerphone','Seller Phone:')}}
                    {{ Form::text('sellerphone',null,array('class' => 'form-control','required' => '', 'maxlenght' => '10'))}}                     


                    {{ Form::submit('Create Service',array('class' => 'btn btn-success btn-lg btn-block','style' => 'margin-top: 20px;'))}}
                    {!! Form::close() !!}

ServiceController


public function create()
    {
        $user = Auth::user();
        return view('services.create')->withUser($user);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //Validate de data
        $this->validate($request, array(
                'name'        => 'required|max:45',
                'phone'       => 'required|max:10',
                'users_id'    => 'required|integer',
                'razsocial'   => 'required|max:50',
                'seller'      => 'required|max:50',
                'sellerphone' => 'required|max:10',

            ));
      
  
        //Store in the Database
        $service = new Service;

        $service->name = $request->name;
        $service->phone = $request->phone;
        $service->users_id = $request->users_id;
        $service->razsocial  = $request->razsocial;
        $service->seller = $request->seller;
        $service->sellerphone = $request->sellerphone;

        $service->save();

        Session::flash('success','The service was succesfully save!');

        //Redirect to Another Page
        return redirect()->route('services.show', $service->id);
    }

vanderb

There are lots of missing information:

Maybe you missed the csrf-token-field?

{{ csrf_field() }}
ArthurDiniz

What u mean by missing information?

im sorry my english is poor

vanderb

@ArthurDiniz Information like, how you registered your routes etc. But I think you use resource-routes, so check if the missing csrf-field will do it.

ArthurDiniz

How do i implement csrf-field? Do i put at the end of the form? Yes i used resource-routes

ArthurDiniz

@vanderb solved it! Thx! the problem was in this line

Old

 {{ Form::text('users_id',$user->id,['disabled'=>'disabled','class' => 'form-control','required' => '', 'maxlenght' => '10'])}}

I deleted the 'disabled'

Now

{{ Form::text('users_id',$user->id,['class' => 'form-control','required' => '', 'maxlenght' => '10'])}}

But how can i block the field so users can not edit?

spodlogar

I would remove it. you should not expose the user's id

spodlogar

Do you have user authentication?

ArthurDiniz

But i want to save on the database. How can i pass it without the form? Yes i have

spodlogar

Instead of this

$service->users_id = $request->users_id;

do this

$service->users_id = $request->user()->id;
ArthurDiniz

And in the view i delete the user form?

spodlogar

Yes delete this part

{{ Form::text('users_id',$user->id,['class' => 'form-control','required' => '', 'maxlenght' => '10'])}}

It should still work fine

ArthurDiniz

Thx it work also deleted it

'users_id'    => 'required|integer',
spodlogar

Awesome

ArthurDiniz

@spodlogar can u explain somethin? do i need to use

    $user = Auth::user();
        return view('services.create')->withUser($user);

in every function , controller?

wickley

The 'disabled' attribute on an html input field will not send the data through for the field when the form is submitted. Use the 'readonly' attribute in place of that instead and it will send the data through but not allow a user to edit.

However, id recommend removing that field completely from the form, unless you have a good reason for users to see these id. If these user id's are associated with other users than who's logged in, make it a hidden field.

If this user_id is always from the logged in user, then do something like this in the controller:

 $service->users_id = Auth::user()->id;

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