I am trying to delete data from table by using forign key in laravel

zubair malik

I am trying to delete data by using a foreign key but unfortunately not deleting from FreeDownloadFiles tables how can I delete data from this table please help me thanks.

Note: data is successfully deleting from freedownload table but not deleting from FreeDownloadFiles tables

free_download table
 
id  |  name | images


free_download_files table

 id | free_download_id | images 

Controller

    public function destroy(Request $request)
    {
        $freedownload = FreeDownload::findOrFail($request->deleteId);
        $freedownloadfiles = FreeDownloadFiles::where('free_download_id', $request->deleteId)->get();

        foreach ($freedownloadfiles as $key => $value) {

            $delete = $value->delete();
        }

        // apply your conditional check here
        if (false) {
            $response['error'] = 'This FreeDownload has something assigned to it.';
            return response()->json($response, 409);
        } else {
            Storage::disk('yourstitchart')->delete($freedownload->icon);
            $response = $freedownload->delete();
            return response()->json($response, 200);
        }
    }
Donkarnash

You can automate the deleting of the images/files associated with the FreeDownloadFiles records by hooking into the model deleting event in the FreeDownloadFiles model definition.


class FreeDownloadFile extends Model
{
     protected static function booted()
     {
        static::deleting(function ($record) {

            /**
             * if images field contains string with comma separated paths to image files
             * convert to array explode(',', $record->images)
             * replace comma with whatever separator is used eg ;
             */
            foreach(explode(',', $record->images) as $image) { 
                Storage::delete($image);
            }                
        });
     }
}

Similarly to automate deleting of FreeDownloadFiles associated with a FreeDownload we can hook into the deleting event for FreeDownload


class FreeDownload extends Model
{
   protected static function booted()
     {
        static::deleting(function ($record) {

            //can also use higher order magic
            //$record->freeDownloadFiles->each->delete();

            foreach($record->freeDownloadFiles as $file) {
                $file->delete();
            }           
        });
     }
}

Then the controller method can become simple as

public function destroy(Request $request)
    {
        $freedownload = FreeDownload::findOrFail($request->deleteId);
        

        // apply your conditional check here
        if (false) {
            $response['error'] = 'This FreeDownload has something assigned to it.';
            return response()->json($response, 409);
        } else {
            Storage::disk('yourstitchart')->delete($freedownload->icon);
            $response = $freedownload->delete();
            return response()->json($response, 200);
        }
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I am trying to get table data from a website using python urllib and beautiful soup but it returns script

I am trying to update user data with role using laravel

I am using this sql statement to delete the total record from a table

I am trying to extract data from class using selenium but it is not working

I am trying to delete data from database, but sometime data is deleted and sometime data is not deleted

How to insert primary key from one table which created by auto increment, into a second table as forign key?

I am trying to delete a row of data using a check box, when I click on delete my php should delete the row

Show Foriegn key data (names etc) instead of just the forign key in HTML table

i am trying to retrieve the data from fire base database using recycler view but i am getting errors in it

i am trying to fetch the whole data based on activity_created_on key from this sample data:

I am trying to insert some data into status table but getting SQL Query Error in Laravel

I am trying to delete items from TODO app using axios and django

I am trying to delete from a multidimensional array using first.last but it returns nil. (ruby)

I am trying to delete a specific task queue from GPC using nodejs code

I am trying to show the data from my ajax response to my jquery data table

I am using crud technique in Laravel, and having trouble in editing and deleting data from multi table through one form

I am trying to create a table with only two foreign key

Fetching Data from another table using foreign key laravel

i am trying to create foreign key constraint in laravel 5.8

How do I delete data using from two database table

I am trying to extract some data from espn as a table and getting it as list

I am trying to delete multiple elements from an array through checkbox i am using vue.js but i am unable to figure out how to do it

I am trying to delete a record in the database using mysql javascript API

I am trying to work out Minutes Per Goal using data from a data frame and the rows are printing wrong

I am trying to check if a value isl Resistant in a table from a Form using dlookup

i am trying to pull text from a table on a jsp using page object selenium

i am trying to display last updated records from table using group by and order by

I am trying to get records from two tables with laravel using WhereAs

I am trying to check the room reservation from start time to end time between two dates using laravel