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

  laravel

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.

free_download table
 
id  |  name | images


free_download_files table

 id | free_download_id | images 

** Controller **

/**
     * Remove the specified resource from storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpJsonResponse
     */
    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);
        }
    }

Source: Laravel

Leave a Reply