首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Laravel 4:雄辩的软删除和关系

Laravel 4:雄辩的软删除和关系
EN

Stack Overflow用户
提问于 2013-11-20 17:24:33
回答 2查看 7.7K关注 0票数 12

我有两个表,客户端和项目,一个项目与一个客户端相关联。出于存档的原因,客户端和项目都实现了软删除以维护关系,即即使我删除了客户端,项目仍然会附加客户端信息。

我的问题是,当我删除客户端时,该引用将无法从项目中访问,并引发异常。我想做的是软删除客户端,但是保留项目关系中的客户端数据。

我的刀片代码如下:

代码语言:javascript
运行
复制
@if ($projects->count())
<table class="table table-striped table-bordered">
    <thead>
        <tr>
            <th>Name</th>
            <th>Client</th>
        </tr>
    </thead>

    <tbody>
        @foreach ($projects as $project)
            <tr>
                <td>{{{ $project->name }}}</td>
                <td>{{{ $project->client->name }}}</td>
                <td>{{ link_to_route('projects.edit', 'Edit', array($project->id), array('class' => 'btn btn-info')) }}</td>
                <td>
                    {{ Form::open(array('method' => 'DELETE', 'route' => array('projects.destroy', $project->id))) }}
                        {{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
                    {{ Form::close() }}
                </td>
            </tr>
        @endforeach
    </tbody>
</table> @else There are no projects @endif

以下是迁移情况:

代码语言:javascript
运行
复制
        Schema::create('clients', function(Blueprint $table) {

        // Table engine
        $table->engine = 'InnoDB';

        // Increments
        $table->increments('id');

        // Relationships

        // Fields
        $table->string('name');

        // Timestamps
        $table->timestamps();

        // Soft deletes
        $table->softDeletes();

    });


        Schema::create('projects', function(Blueprint $table) {

        // Table engine
        $table->engine = 'InnoDB';

        // Increments
        $table->increments('id');

        // Relationships
        $table->integer ('client_id');

        // Fields
        $table->string('name');

        // Timestamps
        $table->timestamps();

        // Soft deletes
        $table->softDeletes();

        // Indexes
        $table->index('client_id');


    });

非常感谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-11-21 13:41:58

在定义模型中的关系时,使用了withTrashed()方法来解决这个问题。

原始代码:

代码语言:javascript
运行
复制
public function client() {
    return $this->belongsTo('Client');
}

解决方案:

代码语言:javascript
运行
复制
public function client() {
    return $this->belongsTo('Client')->withTrashed();
}

非常感谢你的帮助。

票数 32
EN

Stack Overflow用户

发布于 2014-06-27 19:13:59

在我的例子中,我不能像Wally所建议的那样修改函数client,因为它在其他模型和控制器中被使用,而我不希望它得到客户端->withTrashed()

在这种情况下,我提出了两种解决方案:

在急切加载客户端时指定->withTrashed()

代码语言:javascript
运行
复制
$projects = Project::with(['client' => function($query){ $query->withTrashed(); }])->get();

或者创建一个新的client函数->withTrashed()

代码语言:javascript
运行
复制
public function client() {
    return $this->belongsTo('Client');
}

// The new function
public function client_with_trash() {
    return $this->belongsTo('Client')->withTrashed();
}

当现在急于加载时:

代码语言:javascript
运行
复制
$projects = Project::with(['client_with_trash'])->get();
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20102696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档