How to use table without model in hasMany
Asked Answered
C

2

7

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.

Coats answered 11/12, 2017 at 9:22 Comment(3)
No, it's not possible if you want to use it in a relationship.Segalman
No, because the relations are part of Eloquent, which is built on models.Prine
I'm sorry i don't have time to elaborate right now but please checkout belongsToMany relationships. This method takes table as a second argument.Asbestosis
G
7

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

Gotha answered 11/12, 2017 at 9:35 Comment(0)
P
13

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.

Prine answered 11/12, 2017 at 9:27 Comment(0)
G
7

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

Gotha answered 11/12, 2017 at 9:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.