add status when entering data to database

Published 7 months ago by wardaddy

I want to create a status when entering data into the database, if the data is successfully created then the status appears successfully created, and if failed appear unsuccessful, I try like this

UserController

public function store(Request $request)
{
        $this->validate(request(), [
            'name' => 'required|string|min:3|max:40',
            'email' => 'required|string|email|max:40|unique:users',
            'username' => 'required|string|min:6|max:20|alpha_dash|unique:users',
            'password' => 'required|string|min:6|max:40',
        ]);

    $created = User::create([
                    'type_id' => request('user_type'),
                    'name' => request('name'),
                    'email' => request('email'),
                    'username' => request('username'),
                    'password' => bcrypt(request('password')),
                            ]);

    $success = 'Successfully Created';
    $failed = 'Not Successfully Created';

    if ($created) {
        return back()->with('status', $success);
    } else {
        return back()->with('status', $failed);
    }
}

Users.blade.php

@if (session('status'))
    <script>
        $(document).ready(function() {
            M.toast({
                html: '{{ session('status') }}',
                displayLength: '10000'
            });
        });
    </script>
@endif

when the data successfully entered into the database, the status appears successfully created as I expected, but if the data is not successfully entered into the database it does not display any status, what should I do?

Best Answer (As Selected By wardaddy)
wardaddy

solved

UserController

    public function store(Request $request)
    {
        $errors = $this->validate(request(), [
            'name' => 'required|string|min:3|max:40',
            'email' => 'required|string|email|max:40|unique:users',
            'username' => 'required|string|min:6|max:20|alpha_dash|unique:users',
            'password' => 'required|string|min:6|max:40',
        ]);

        User::create([
            'type_id' => request('user_type'),
            'name' => request('name'),
            'email' => request('email'),
            'username' => request('username'),
            'password' => bcrypt(request('password')),
        ]);

        return back()->with('status', 'Successfully Created');
    }

users.blade.php

    <script>
        @if ($errors->any())
        $(document).ready(function() {
              M.toast({
                html: 'Not Successfully Created',
                displayLength: '10000'
              });
            });
        @elseif (session('status'))
            $(document).ready(function() {
              M.toast({
                html: '{{ session('status') }}',
                displayLength: '10000'
              });
            });
        @endif
    </script>

thanks for the advise, maybe try and catch is a great way but I am still confused and have never known it before, so the above code is enough for now

ChiragJatapara

Hello,

You should use try and catch Exceptions Handler.Hope this may help you. Happy coding !!

petrit

If validation is passed the user creation will be executed. If validation is not passed the user create method will not be executed. All of the validation errors will automatically be flashed to the session, and you will be able to see them on blade

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif 

Or, if you are looking for a flash massages I suggest you this package https://github.com/laracasts/flash

wardaddy

@petrit Your code is the same as the code below, right? I have it but I put it in a modal, so if the user has an input error he should open the modal first to know whether there is a mistake or not, therefore I want to bring up the Materialize Toast to display the error instead of the specific error such as error generated by validator but only displays "not successfully created" so that the user is not confused when the data he input does not enter the database but the error does not appear on the screen even though it has actually been displayed on the modal.

<div class="input-field">
    <input type="text" name="name" value="{{ old('name') }}" required>
    <label>Name</label>
    @if ($errors->has('name'))
        <span>{{ $errors->first('name') }}</span>
    @endif
</div>
<div class="input-field">
    <input type="email" name="email" value="{{ old('email') }}" required>
    <label>Email</label>
    @if ($errors->has('email'))
        <span>{{ $errors->first('email') }}</span>
    @endif
</div>
<div class="input-field">
    <input type="text" name="username" value="{{ old('username') }}" required>
    <label>Username</label>
    @if ($errors->has('username'))
        <span>{{ $errors->first('username') }}</span>
    @endif
</div>
<div class="input-field">
    <input type="password" name="password" required>
    <label>Password</label>
    @if ($errors->has('password'))
        <span>{{ $errors->first('password') }}</span>
    @endif
</div>

what I want to ask is, what is the right condition on if?

if ( condition??? ) {
    return back()->with('status', $success);
} else {
    return back()->with('status', $failed);
}
Snapey
Snapey
7 months ago (995,145 XP)

If you get past validation, WHY would your record not be created?

As suggested, if you have any doubt that a record will be created then you should wrap the create in a try-catch block.

Otherwise, if it fails to create then an error will be thrown and you will never reach your if(condition??? )

wardaddy

solved

UserController

    public function store(Request $request)
    {
        $errors = $this->validate(request(), [
            'name' => 'required|string|min:3|max:40',
            'email' => 'required|string|email|max:40|unique:users',
            'username' => 'required|string|min:6|max:20|alpha_dash|unique:users',
            'password' => 'required|string|min:6|max:40',
        ]);

        User::create([
            'type_id' => request('user_type'),
            'name' => request('name'),
            'email' => request('email'),
            'username' => request('username'),
            'password' => bcrypt(request('password')),
        ]);

        return back()->with('status', 'Successfully Created');
    }

users.blade.php

    <script>
        @if ($errors->any())
        $(document).ready(function() {
              M.toast({
                html: 'Not Successfully Created',
                displayLength: '10000'
              });
            });
        @elseif (session('status'))
            $(document).ready(function() {
              M.toast({
                html: '{{ session('status') }}',
                displayLength: '10000'
              });
            });
        @endif
    </script>

thanks for the advise, maybe try and catch is a great way but I am still confused and have never known it before, so the above code is enough for now

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