Laravel: Soft Delete an item











up vote
-1
down vote

favorite












I would like to delete an item only on my page "overview" however I will wish to keep the element on my database. Is it possible ?



Do you have an idea of the procedure ?



I don't know how to do softdelete, in my Controller I have that



public function destroy(Devis $devis)
{
$devis->delete();
return redirect('/admin');
}


In my route I have that



Route::group(['middleware' => ['auth']], function () {
Route::get('/admin', 'AdminController@getHome');
Route::get('/valider/{devis}', 'AdminController@destroy');
});


In my file overview I have that



@foreach($devis as $item)
<tr>
<td> {{$item->firstname}}</td>
<td> {{$item->lastname}} </td>
<td> {{$item->phone}} </td>
<td> {{$item->start_adress}} -- {{$item->start_floor}}</td>
<td> {{$item->end_adress}} -- {{$item->end_floor}}</td>
<td> {{$item->type_intervention}}</td>
<td> {{$item->email}} </td>
<td> {{$item->remarks}} </td>
<td> <a href="/valider/{{$item->id}}" class="btn btn-danger"><span class="left ion-close"></span></a></td>
</tr>
@endforeach









share|improve this question
























  • here is some great explaintion: stackoverflow.com/questions/22426165/laravel-soft-delete-posts
    – Naveed Ali
    Nov 7 at 10:56










  • Try to use google before asking on SO unless you don't understand how it works and need a different explanation. Don't just ask your questions without searching on google.
    – killstreet
    Nov 8 at 12:00















up vote
-1
down vote

favorite












I would like to delete an item only on my page "overview" however I will wish to keep the element on my database. Is it possible ?



Do you have an idea of the procedure ?



I don't know how to do softdelete, in my Controller I have that



public function destroy(Devis $devis)
{
$devis->delete();
return redirect('/admin');
}


In my route I have that



Route::group(['middleware' => ['auth']], function () {
Route::get('/admin', 'AdminController@getHome');
Route::get('/valider/{devis}', 'AdminController@destroy');
});


In my file overview I have that



@foreach($devis as $item)
<tr>
<td> {{$item->firstname}}</td>
<td> {{$item->lastname}} </td>
<td> {{$item->phone}} </td>
<td> {{$item->start_adress}} -- {{$item->start_floor}}</td>
<td> {{$item->end_adress}} -- {{$item->end_floor}}</td>
<td> {{$item->type_intervention}}</td>
<td> {{$item->email}} </td>
<td> {{$item->remarks}} </td>
<td> <a href="/valider/{{$item->id}}" class="btn btn-danger"><span class="left ion-close"></span></a></td>
</tr>
@endforeach









share|improve this question
























  • here is some great explaintion: stackoverflow.com/questions/22426165/laravel-soft-delete-posts
    – Naveed Ali
    Nov 7 at 10:56










  • Try to use google before asking on SO unless you don't understand how it works and need a different explanation. Don't just ask your questions without searching on google.
    – killstreet
    Nov 8 at 12:00













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I would like to delete an item only on my page "overview" however I will wish to keep the element on my database. Is it possible ?



Do you have an idea of the procedure ?



I don't know how to do softdelete, in my Controller I have that



public function destroy(Devis $devis)
{
$devis->delete();
return redirect('/admin');
}


In my route I have that



Route::group(['middleware' => ['auth']], function () {
Route::get('/admin', 'AdminController@getHome');
Route::get('/valider/{devis}', 'AdminController@destroy');
});


In my file overview I have that



@foreach($devis as $item)
<tr>
<td> {{$item->firstname}}</td>
<td> {{$item->lastname}} </td>
<td> {{$item->phone}} </td>
<td> {{$item->start_adress}} -- {{$item->start_floor}}</td>
<td> {{$item->end_adress}} -- {{$item->end_floor}}</td>
<td> {{$item->type_intervention}}</td>
<td> {{$item->email}} </td>
<td> {{$item->remarks}} </td>
<td> <a href="/valider/{{$item->id}}" class="btn btn-danger"><span class="left ion-close"></span></a></td>
</tr>
@endforeach









share|improve this question















I would like to delete an item only on my page "overview" however I will wish to keep the element on my database. Is it possible ?



Do you have an idea of the procedure ?



I don't know how to do softdelete, in my Controller I have that



public function destroy(Devis $devis)
{
$devis->delete();
return redirect('/admin');
}


In my route I have that



Route::group(['middleware' => ['auth']], function () {
Route::get('/admin', 'AdminController@getHome');
Route::get('/valider/{devis}', 'AdminController@destroy');
});


In my file overview I have that



@foreach($devis as $item)
<tr>
<td> {{$item->firstname}}</td>
<td> {{$item->lastname}} </td>
<td> {{$item->phone}} </td>
<td> {{$item->start_adress}} -- {{$item->start_floor}}</td>
<td> {{$item->end_adress}} -- {{$item->end_floor}}</td>
<td> {{$item->type_intervention}}</td>
<td> {{$item->email}} </td>
<td> {{$item->remarks}} </td>
<td> <a href="/valider/{{$item->id}}" class="btn btn-danger"><span class="left ion-close"></span></a></td>
</tr>
@endforeach






php laravel soft-delete






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 11:40









Salman Zafar

814619




814619










asked Nov 7 at 10:33









anais_stem

117




117












  • here is some great explaintion: stackoverflow.com/questions/22426165/laravel-soft-delete-posts
    – Naveed Ali
    Nov 7 at 10:56










  • Try to use google before asking on SO unless you don't understand how it works and need a different explanation. Don't just ask your questions without searching on google.
    – killstreet
    Nov 8 at 12:00


















  • here is some great explaintion: stackoverflow.com/questions/22426165/laravel-soft-delete-posts
    – Naveed Ali
    Nov 7 at 10:56










  • Try to use google before asking on SO unless you don't understand how it works and need a different explanation. Don't just ask your questions without searching on google.
    – killstreet
    Nov 8 at 12:00
















here is some great explaintion: stackoverflow.com/questions/22426165/laravel-soft-delete-posts
– Naveed Ali
Nov 7 at 10:56




here is some great explaintion: stackoverflow.com/questions/22426165/laravel-soft-delete-posts
– Naveed Ali
Nov 7 at 10:56












Try to use google before asking on SO unless you don't understand how it works and need a different explanation. Don't just ask your questions without searching on google.
– killstreet
Nov 8 at 12:00




Try to use google before asking on SO unless you don't understand how it works and need a different explanation. Don't just ask your questions without searching on google.
– killstreet
Nov 8 at 12:00












5 Answers
5






active

oldest

votes

















up vote
0
down vote



accepted










Laravel already did it look here so you add deleted_at in your model and
as docs said your model is like:






  namespace App;

use IlluminateDatabaseEloquentModel;
use IlluminateDatabaseEloquentSoftDeletes;

class Flight extends Model
{
use SoftDeletes;

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
}








share|improve this answer




























    up vote
    0
    down vote













    When you want to soft delete actually you need a flag. Let's say that in your table you have a column deleted which accepts boolean values.



    When you retrieve the data in your controller to pass it to the view then you will query the table you just put a where clause to your query something like:



    ->where('deleted',0)


    So you get the "undeleted" items.



    Now when you want to softdelete actually you don't delete but you update your column to set it's status to 1.






    share|improve this answer




























      up vote
      0
      down vote













      Use this: Documentation soft deleting
      later add to the conditions for downloading items



      ->whereNull('deleted_at')





      share|improve this answer




























        up vote
        0
        down vote













        Laravel has it's own implementation of soft deleting. Simply add use SoftDeletes to your model, and it starts working. Try reading Eloquent - Soft deletes to learn more about it and its implications on your code.






        share|improve this answer




























          up vote
          0
          down vote













          You must add a field to soft delete when creating the table. It's best to migrate. If it will be and you'll use soft delete then:



          $ item-> delete () - will be softdelete

          $ item-> forcedelete () - will permanently delete





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187693%2flaravel-soft-delete-an-item%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            5 Answers
            5






            active

            oldest

            votes








            5 Answers
            5






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            Laravel already did it look here so you add deleted_at in your model and
            as docs said your model is like:






              namespace App;

            use IlluminateDatabaseEloquentModel;
            use IlluminateDatabaseEloquentSoftDeletes;

            class Flight extends Model
            {
            use SoftDeletes;

            /**
            * The attributes that should be mutated to dates.
            *
            * @var array
            */
            protected $dates = ['deleted_at'];
            }








            share|improve this answer

























              up vote
              0
              down vote



              accepted










              Laravel already did it look here so you add deleted_at in your model and
              as docs said your model is like:






                namespace App;

              use IlluminateDatabaseEloquentModel;
              use IlluminateDatabaseEloquentSoftDeletes;

              class Flight extends Model
              {
              use SoftDeletes;

              /**
              * The attributes that should be mutated to dates.
              *
              * @var array
              */
              protected $dates = ['deleted_at'];
              }








              share|improve this answer























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                Laravel already did it look here so you add deleted_at in your model and
                as docs said your model is like:






                  namespace App;

                use IlluminateDatabaseEloquentModel;
                use IlluminateDatabaseEloquentSoftDeletes;

                class Flight extends Model
                {
                use SoftDeletes;

                /**
                * The attributes that should be mutated to dates.
                *
                * @var array
                */
                protected $dates = ['deleted_at'];
                }








                share|improve this answer












                Laravel already did it look here so you add deleted_at in your model and
                as docs said your model is like:






                  namespace App;

                use IlluminateDatabaseEloquentModel;
                use IlluminateDatabaseEloquentSoftDeletes;

                class Flight extends Model
                {
                use SoftDeletes;

                /**
                * The attributes that should be mutated to dates.
                *
                * @var array
                */
                protected $dates = ['deleted_at'];
                }








                  namespace App;

                use IlluminateDatabaseEloquentModel;
                use IlluminateDatabaseEloquentSoftDeletes;

                class Flight extends Model
                {
                use SoftDeletes;

                /**
                * The attributes that should be mutated to dates.
                *
                * @var array
                */
                protected $dates = ['deleted_at'];
                }





                  namespace App;

                use IlluminateDatabaseEloquentModel;
                use IlluminateDatabaseEloquentSoftDeletes;

                class Flight extends Model
                {
                use SoftDeletes;

                /**
                * The attributes that should be mutated to dates.
                *
                * @var array
                */
                protected $dates = ['deleted_at'];
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 7 at 10:39









                sharif854

                964




                964
























                    up vote
                    0
                    down vote













                    When you want to soft delete actually you need a flag. Let's say that in your table you have a column deleted which accepts boolean values.



                    When you retrieve the data in your controller to pass it to the view then you will query the table you just put a where clause to your query something like:



                    ->where('deleted',0)


                    So you get the "undeleted" items.



                    Now when you want to softdelete actually you don't delete but you update your column to set it's status to 1.






                    share|improve this answer

























                      up vote
                      0
                      down vote













                      When you want to soft delete actually you need a flag. Let's say that in your table you have a column deleted which accepts boolean values.



                      When you retrieve the data in your controller to pass it to the view then you will query the table you just put a where clause to your query something like:



                      ->where('deleted',0)


                      So you get the "undeleted" items.



                      Now when you want to softdelete actually you don't delete but you update your column to set it's status to 1.






                      share|improve this answer























                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote









                        When you want to soft delete actually you need a flag. Let's say that in your table you have a column deleted which accepts boolean values.



                        When you retrieve the data in your controller to pass it to the view then you will query the table you just put a where clause to your query something like:



                        ->where('deleted',0)


                        So you get the "undeleted" items.



                        Now when you want to softdelete actually you don't delete but you update your column to set it's status to 1.






                        share|improve this answer












                        When you want to soft delete actually you need a flag. Let's say that in your table you have a column deleted which accepts boolean values.



                        When you retrieve the data in your controller to pass it to the view then you will query the table you just put a where clause to your query something like:



                        ->where('deleted',0)


                        So you get the "undeleted" items.



                        Now when you want to softdelete actually you don't delete but you update your column to set it's status to 1.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 7 at 10:37









                        pr1nc3

                        3,0732520




                        3,0732520






















                            up vote
                            0
                            down vote













                            Use this: Documentation soft deleting
                            later add to the conditions for downloading items



                            ->whereNull('deleted_at')





                            share|improve this answer

























                              up vote
                              0
                              down vote













                              Use this: Documentation soft deleting
                              later add to the conditions for downloading items



                              ->whereNull('deleted_at')





                              share|improve this answer























                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                Use this: Documentation soft deleting
                                later add to the conditions for downloading items



                                ->whereNull('deleted_at')





                                share|improve this answer












                                Use this: Documentation soft deleting
                                later add to the conditions for downloading items



                                ->whereNull('deleted_at')






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 7 at 10:37









                                Mateusz Czerwiński

                                336




                                336






















                                    up vote
                                    0
                                    down vote













                                    Laravel has it's own implementation of soft deleting. Simply add use SoftDeletes to your model, and it starts working. Try reading Eloquent - Soft deletes to learn more about it and its implications on your code.






                                    share|improve this answer

























                                      up vote
                                      0
                                      down vote













                                      Laravel has it's own implementation of soft deleting. Simply add use SoftDeletes to your model, and it starts working. Try reading Eloquent - Soft deletes to learn more about it and its implications on your code.






                                      share|improve this answer























                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        Laravel has it's own implementation of soft deleting. Simply add use SoftDeletes to your model, and it starts working. Try reading Eloquent - Soft deletes to learn more about it and its implications on your code.






                                        share|improve this answer












                                        Laravel has it's own implementation of soft deleting. Simply add use SoftDeletes to your model, and it starts working. Try reading Eloquent - Soft deletes to learn more about it and its implications on your code.







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 7 at 10:38









                                        Maaike Brouwer

                                        942




                                        942






















                                            up vote
                                            0
                                            down vote













                                            You must add a field to soft delete when creating the table. It's best to migrate. If it will be and you'll use soft delete then:



                                            $ item-> delete () - will be softdelete

                                            $ item-> forcedelete () - will permanently delete





                                            share|improve this answer



























                                              up vote
                                              0
                                              down vote













                                              You must add a field to soft delete when creating the table. It's best to migrate. If it will be and you'll use soft delete then:



                                              $ item-> delete () - will be softdelete

                                              $ item-> forcedelete () - will permanently delete





                                              share|improve this answer

























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                You must add a field to soft delete when creating the table. It's best to migrate. If it will be and you'll use soft delete then:



                                                $ item-> delete () - will be softdelete

                                                $ item-> forcedelete () - will permanently delete





                                                share|improve this answer














                                                You must add a field to soft delete when creating the table. It's best to migrate. If it will be and you'll use soft delete then:



                                                $ item-> delete () - will be softdelete

                                                $ item-> forcedelete () - will permanently delete






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 7 at 11:14









                                                Salman Zafar

                                                814619




                                                814619










                                                answered Nov 7 at 10:37









                                                Łukasz Matejka

                                                113




                                                113






























                                                     

                                                    draft saved


                                                    draft discarded



















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53187693%2flaravel-soft-delete-an-item%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    這個網誌中的熱門文章

                                                    Tangent Lines Diagram Along Smooth Curve

                                                    Yusuf al-Mu'taman ibn Hud

                                                    Zucchini