php - 为什么我的 detach() 和 delete() 不起作用?

标签 php mysql laravel laravel-5

我正在使用 Laravel 5,我正在尝试从数据库中删除一些数据

HTML

<form method="post" action="{{route('publications.destroy', $publication->id)}}">
    {{csrf_field()}}
    {{method_field('DELETE')}}
    <button type="submit" class="btn btn-danger btn-sm" dusk="btn-confirmDeletePub">Yes, Delete</button>
</form>

web.php

Route::resource('publications','PublicationController');

Publication.php(模型)

public function users()
{
    return $this->belongsToMany('App\User', 'user_publication');
}

public function topics()
{
    return $this->belongsToMany('App\Topic', 'topic_publication');
}

public function authors()
{
    return $this->belongsToMany('App\Author', 'author_publication');
}

public function details()
{
        /*
        Since we must join the publications table with one of the
        journals/conference/editorship table (based on type column' value)
        to retrieve publication'details,  we "aggregate" the 3 alternatives in this method.

        this method is useful for retrieving from db, 
        for insertions, the 3 methods above ( journal(),conference(),editorship())
        should be used
        */
        switch ($this->type) {
            case 'journal':
                return $this->hasOne('App\Journal');
                break;

            case 'conference':
                return $this->hasOne('App\Conference');
                break;

            case 'editorship':
                return $this->hasOne('App\Editorship');
                break;

        }
 }

PublicationController.php

public function destroy($id)
{
    $publication = Publication::find($id);
    $publication->users()->detach($publication->id);
    $publication->topics()->detach($publication->id);
    $publication->authors()->detach($publication->id);
    //dd($publication);
    $publication->details()->delete();

    //$publication->delete();

    //Redirect('/users')->with('success', 'Publication deleted correctly.');

    return redirect('/users')->with('success', 'Publication deleted correctly.');

}

当我单击 HTML 表单中的“是,删除”按钮时,它会调用 PublicationController 中的 destroy 方法来删​​除出版物具体id。我尝试注释所有代码并仅保留返回重定向以查看该方法是否被调用以及它是否有效。 之后,我删除了对 detach() 函数的注释,但莫名其妙地在数据库中它们没有产生任何结果。最后,我删除了 $publication->details()->delete(); 处的注释,然后我的应用程序崩溃了。

最佳答案

也许您的模型不知道 $this->type 的值是什么?

我不建议在模型中使用开关。模型关系是启动序列的一部分。您必须将其分成 journalDetailsconferenceDetailseditorshipDetails 并在 Controller 中做出正确的选择。

public function journalDetails()
{
  return $this->hasOne('App\Journal');
}
public function conferenceDetails()
{
  return $this->hasOne('App\Conference');
}
public function editorshipDetails()
{
  return $this->hasOne('App\Editorship');
}

关于php - 为什么我的 detach() 和 delete() 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50087623/

相关文章:

PHP,将多维数组中的所有数组递归提取到一个平面 "holder"数组?

PHP/MySQL : Declare variables using array elements from MySQL Query

php - 如何将文件上传到公共(public)html文件夹到服务器并保存到mysql数据库的路径?

mysql - 在 MySql 中的同一个表中加入 2 选择查询

mysql - 如何在单行mysql laravel中搜索多个数据

java - 在Java中是否有一个array_intersect()等效项?

php - 返回最后插入的 id

MySQL 加入 + "WHERE something=MAX(something)"

javascript - 422(使用ajax提交表单时出现不可处理的实体错误

php - 在 Laravel 中设置加密 key 导致注销