khaldoun masri khaldoun masri - 3 months ago 8
MySQL Question

Passing a parameter sent by a form to the controler in Laravel 5.2

I am new to Laravel programming, and now I want to delete from 2 tables (projects and pictures) in plain php i can do this:

delete the project from projects:

$sql1 = delete * from projects where project_id = 1;

delete all his photos

$sql2 = delete * from pictures where project_id = 1;

I want to do this in my route.php here is my code

Route::delete('/project/{project}', function (Project $project) {
DB::table('pics')->where('projectId', '=', $project)->delete();
$project -> delete();
return redirect('/');
});


i am able to delete the project but not its picture?

what can i do?

Answer

You're using type hinting:

... (Project $project) { ...

So when you do this:

DB::table('pics')->where('projectId', '=', $project)->delete();

You're really saying "where projectId is equal to this object".

You have two options. Either 1) don't use type hinting:

Route::delete('/project/{project}', function ($project) {
    DB::table('pics')->where('projectId', '=', $project)->delete();
    $project -> delete();
    return redirect('/');
});

Or 2) use type hinting properly:

Route::delete('/project/{project}', function (Project $project) {
    DB::table('pics')->where('projectId', '=', $project->id)->delete();
    $project -> delete();
    return redirect('/');
});