Saturday, May 12, 2018

Laravel eloquent delete - development

Laravel eloquent delete

Hello looking for assistant, how to understand Laravel Eloquent model. I need to delete User by Id, but I have table Main_info, Personal_info, Holidays, other_info, fm_day, oth_daysoff. On them all I have inserted UserId, and I need to delete selected UserId from all the tables? maybe someone can help me How can I make do this with Laravel Eloquent model?
Here is my view with delete submit button and post method:

<div class="container">
<div class="row">

    <div class="col">
        <div class="card">
            <div class="card-head-man">
                <span>Filter type: </span>&nbsp;
                <a href="javascript:void(0)" class="btn btn-secondary btn-sm">Position</a>&nbsp;
                <a href="javascript:void(0)" class="btn btn-secondary btn-sm">Active/No</a>&nbsp;
                <a href="javascript:void(0)" class="btn btn-secondary btn-sm">Employment type</a>
                <a href="javascript:void(0)" class="btn btn-secondary btn-sm">Location</a>
            </div>
        </div>
    </div>

    <div class="col-12">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">List of employees</h3>
            </div>

            <div class="table-responsive">
                <table class="table card-table table-vcenter text-nowrap">
                    <thead>
                    <tr>
                        <th class="w-1"><a href="#" class="sort-act">ID<span class="caret"></span></a></th>
                        <th><a href="#" class="sort-act">First Name<span class="caret"></span></a></th>
                        <th>Last Name</th>
                        <th>Position</th>
                        <th>Active/no</th>
                        <th>Employment type</th>
                        <th>Location</th>
                        <th></th>
                        <th></th>
                    </tr>
                    </thead>

                    <tbody>
                    @foreach($allData as $data)


                        <tr>
                        <td><span class="text-muted"></span></td>
                        <td></td>
                        <td>
                            
                        </td>
                        <td>
                            
                        </td>
                        <td>
                            
                        </td>
                        <td>
                            <span class="status-icon bg-success"></span> Active
                            <span class="status-icon bg-danger"></span> No
                        </td>
                        <td></td>

                        <td class="text-right">
                            <a href="javascript:void(0)" class="btn btn-secondary btn-sm">Manage</a>
                            <div class="dropdown">
                                <form action="/deleteFromHome" method="post">
                                    
                                    <input style="display: none;" type="text" name="deleteUserId" class="form-control" id="inputName3" value="">
                                    <button type="submit" class="btn btn-secondary btn-sm">Delete</button>
                                </form>
                            </div>
                        </td>
                        <td>
                            <a class="icon" href="javascript:void(0)">
                                <i class="fe fe-edit"></i>
                            </a>
                        </td>
                    </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>

</div>

Here is my controller how I’m doing it now:

public function deleteUserFromHome(Request $req)
{
    $userId = $req->input('deleteUserId');

    OtherInfo::where('id', $userId)->delete();
    OthDaysInfo::where('id', $userId)->delete();
    HolidaysInfo::where('id', $userId)->delete();
    FMdaysInfo::where('id', $userId)->delete();
    PersonalInfo::where('id', $userId)->delete();
    PersonalInfo::where('id', $userId)->delete();
    MainInfo::where('id', $userId)->delete();

    return redirect('home');
}

Maybe there is some better way to delete all data from all tables about selected userId?
I need some example how I can make Laravel Eloquent model to delete with function HasMany or some else?



from Laravel Questions and Answers https://laravelquestions.com/laravel/laravel-eloquent-delete/
via Lzo Media

No comments:

Post a Comment