Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在laravel-5模型中使用多个表_Php_Laravel 4_Laravel 5_Laravel 5.1 - Fatal编程技术网

Php 如何在laravel-5模型中使用多个表

Php 如何在laravel-5模型中使用多个表,php,laravel-4,laravel-5,laravel-5.1,Php,Laravel 4,Laravel 5,Laravel 5.1,我有一个活动菜单,它有三个子菜单。这些子菜单对于“活动”是通用的,但具有不同的表。所以我想在一个模型类下使用这三个表,如何将操作个体保存到彼此之间 我的代码如下: <?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; use DB; class Activity extends Model { protected $table1 = 'ta

我有一个活动菜单,它有三个子菜单。这些子菜单对于“活动”是通用的,但具有不同的表。所以我想在一个模型类下使用这三个表,如何将操作个体保存到彼此之间

我的代码如下:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use DB;


class Activity extends Model {

    protected $table1 = 'task_management';
    protected $table2 = 'call_management';
    protected $table3 = 'event_management';
}

您可以创建另一个构建在您的模型之上的类,甚至可以使用@Peeje提到的普通SQL查询。该模型适用于Laravel中的单个表格-我认为没有一种简单的方法可以使其适用于不同的表格。

我建议您遵循雄辩的方法

您应该创建3个模型并执行以下操作

这很简单,就像

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CallManagement extends Model
{
    /**
     * Get the task record associated with the user.
     */
    public function task()
    {
        return $this->hasOne('App\TaskManagement');
    }
}

如何在多个表条件中使用模型类的对象执行保存操作检查现有答案:您不能规范化表并为每个实体创建模型吗?
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class CallManagement extends Model
{
    /**
     * Get the task record associated with the user.
     */
    public function task()
    {
        return $this->hasOne('App\TaskManagement');
    }
}