2015-06-09 20:01
浏览 80


Using Laravel 5.1, I have created an Eloquent Model:

class Divisions extends Model

I have a table and a view in my database which are relevant to this model.

  • divisions_info - The table, it will have INSERT and UPDATE queries run, but will never have a SELECT run on it.
  • divisions_view - The view, it will have SELECT queries run only (obviously).

The reason for the segmentation is that the divisions_view view joins with data from the divisions_info table and from another relevant, read-only table in an adjacent database.

I would like to utilize one model for everything that deals with divisions, but direct any UPDATE/INSERT queries on the divisions_info table and all SELECT queries on the divisions_view view.

The Laravel documentation allows you to override the assumed table name, like so:

protected $table = 'divisions_info';

However, this takes precedence over the entire model. I would like to be able to utilize all the built in methods that come with using the Eloquent model, but have it split accordingly across SQL statements.

Is this possible? If so, what is the right way to achieve it?

If it is not possible, without diving into opinion, what is the most logical/Laravel-esque way to work with the same data in two different locations?

图片转代码服务由CSDN问答提供 功能建议

使用Laravel 5.1,我创建了一个Eloquent模型:

  class Divisions扩展Model 

我的数据库中有一个表和一个与该模型相关的视图。 \ n

  • divisions_info - 该表,它将运行 INSERT UPDATE 查询,但永远不会有 运行 SELECT
  • divisions_view - 视图中,它只会运行 SELECT 查询(显然 )。

    分段的原因是 divisions_view 视图与< code> divisions_info 表和相邻数据库中另一个相关的只读表。

    我想将一个模型用于处理分区的所有内容,但是指向 divisions_info 上的任何 UPDATE / INSERT 查询 表和 divisions_view 视图中的所有 SELECT 查询。


      protected $ table  ='divisions_info'; 

    但是,这优先于整个模型。 我希望能够利用Eloquent模型带来的所有内置方法,但是它会在SQL语句中进行相应的拆分。

    这可能吗? 如果是这样,实现它的正确方法是什么?

    如果不可能,不考虑意见,使用相同数据的最合乎逻辑/ Laravel式的方法是什么 在两个不同的位置?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumen1883 2015-06-09 20:22

    This might be a solution:

    use Illuminate\Database\Eloquent\Model;
    class Division extends Model {
        public function scopeFromView($query)
            return $query->from('divisions_view');

    Just call the fromView() method in any part of the query like this:

    解决 无用
    打赏 举报

相关推荐 更多相似问题