0

I will explain first the tables and how code works

Tables

I have:

  • projects with fields: id, slug, order, public, pathheader and pathhome.
  • project_translations with fields: id, locale, project_id, title and caption.
  • clients with fields: id, name, slug and priority.
  • client_project with fields: id, client_id and project_id

How the code works

When I create a project, I create two project translation too (one for each locale, ex: 'es', 'en'). and then I select a client and this makes the relation client_project.

When I delete the project, I delete at the same time the project_translations which have the same project_id and the client_project row where project_id is the same.

What I want

When I delete the client, delete the row which field client_id have the same value (this is working) and then, delete the projects and projects_translations of the projects which have relation with the client i deleted.

How my function looks for the moment

public function destroyClient($id) //Destruir cliente y todo lo relacionado de la bbdd
    {
        $cliente = Client::find($id); 
        $cliente->delete(); //delete the client
        DB::table('client_project')->where('client_id',$id)->delete(); //delete the client_project relations which field client_id is the same that the client i just deleted.

        return redirect()->route('admin.clients');
    }
7
  • So, problem is client is deleted successfully but record in client_project and project_transalation doesn't deleted ? Commented Aug 8, 2017 at 11:33
  • have you tried $cliente->projects()->delete(); ? Commented Aug 8, 2017 at 11:34
  • With this code for the moment i just delete the client and the client_project relations. I need to delete the project and project_translation @SagarGautam Commented Aug 8, 2017 at 11:36
  • you need to make database optimized. Make correct relations and cascading it will be easier to delete records on multiple tables Commented Aug 8, 2017 at 11:39
  • Yes @MisaGH and isn't working. Commented Aug 8, 2017 at 11:41

3 Answers 3

2

Hope this can help you

public function destroyClient($id) //Destruir cliente y todo lo relacionado de la bbdd
    {
        $cliente = Client::find($id);
        $cliente_project = DB::table('client_project')->where('client_id', $id)->first();
        $project_id = $cliente_project->project_id;
        $cliente->delete(); //delete the client
        DB::table('client_project')->where('client_id',$id)->delete(); //delete the client_project relations which field client_id is the same that the client i just deleted.

        DB::table('projects')->where('id',$project_id)->delete();
        DB::table('project_translations')->where('project_id',$project_id)->delete();

        return redirect()->route('admin.clients');
    }

Maybe a better way is using foreing keys

Sign up to request clarification or add additional context in comments.

Comments

2

I think you can try this :

public function destroyClient($id) //Destruir cliente y todo lo relacionado de la bbdd
{
    $cliente = Client::find($id); 
    $cliente->delete(); //delete the client
    $project = DB::table('client_project')->where('client_id',$id)->first();
    DB::table('client_project')->where('client_id',$id)->delete(); 


    DB::table('projects')->where('id',$project->project_id)->delete();

    DB::table('project_translations')->where('project_id',$project->project_id)->delete(); 


    return redirect()->route('admin.clients');
}

Hope this work for you !!!

Comments

1

Following code will first get all the projects related to client. And then will delete all projects of a client through loop.

public function destroyClient($id) 
{
    $cliente = Client::find($id); 
    $cliente->delete(); //delete the client

    // get list of all projects of client
    $projects = DB::table('client_project')->where('client_id',$id)->get();

    DB::table('client_project')->where('client_id',$id)->delete(); 

    // delete all projects of client
    foreach($projects as $project)
    {
        DB::table('projects')->where('id',$project->id)->delete();

        DB::table('project_translations')->where('project_id',$project->id)->delete(); 
    }    

    return redirect()->route('admin.clients');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.