Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use table without model in hasMany

is that possible to use direct table name in hasMany. for example

public function Permissions()
{
    return $this->hasMany('TABLE NAME');
}

I have table named PERMISSION but don't have a model of this table, but i want to use this table in hasmany.

like image 329
Muhammad Kazim Avatar asked Dec 11 '17 09:12

Muhammad Kazim


2 Answers

Relations in Laravel are a part of Eloquent ORM which is built on the use of models.
So, no, it is not possible to use relations without making models for them.

like image 159
Jerodev Avatar answered Sep 19 '22 07:09

Jerodev


Just create a model for that table

php artisan make:model Permisson

then add the table name to the model class

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Permisson extends Model
{
    protected $table = "table_name";
}

and add the relation

public function permissions()
{
    return $this->hasMany(Permisson::class)
}

I hope it helps

like image 30
larsbadke Avatar answered Sep 22 '22 07:09

larsbadke