0

I have an activity menu that have three sub-menu. These sub-menu are common to activity but have different table. So I want to use these three table under one model class, how can I do save operation individual to each other?

My code is as follows:

<?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';
}
Panda
  • 6,955
  • 6
  • 40
  • 55
dhamo dharan
  • 138
  • 2
  • 11

3 Answers3

0
use DB;

I have preferred using the DB query whenever i had to work with more than one table, below is a sample query.
$data = DB::table('base table name')
              ->join('join table name', 'join table name id', '=', 'master table name id')
              ->select('column 1', 'column 2', 'column 3')
              ->where('column1', $val);
Peeje
  • 445
  • 2
  • 9
0

You can create another class that is built on top of your models or even just uses plain SQL queries as @Peeje mentioned. Model is meant to work with single table in Laravel - I don't think there is an easy way to make it work with different tables.

Ivan Yarych
  • 1,931
  • 17
  • 15
0

I would recommend you to follow the Eloquent way,

You should create 3 models and do relationship

It would be simple like

<?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');
    }
}

Which allows you to retrive like $task = User::find(1)->TaskManagement; Note : You should create and customize your model according to your need.

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63