Kikolce Kikolce - 3 months ago 11
PHP Question

Specific role relations

I work on one project with Laravel 5.2 and Entrust package for ACL.
In this project I need for one Role ('venue_owner') in which venue is owner. I have also table called

venue
and I have no idea how to make this relations, because table
users
is general for all type of users.

How to make this relations to know what
user
from role
venue_owner
is owner of what
venues
?

Answer

Have you created your Migrations yet by running: php artisan enthrust:migration? if not it, run it and then inside the file that is generated, add your own tables like below within the up() Method of the Enthrust Migration File:

<?php

    public function up() {
        // SOME OTHER TABLE CREATION CODES...

        Schema::create('venue_owner', function (Blueprint $table) {
            $table->increments('id');
            $table->integer("user_id")->unsigned();
            $table->timestamps();
            // CREATE THE ASSOCIATION/RELATIONSHIP USING FOREIGN KEY
            $table->foreign('id')
                  ->references('id')
                  ->on('venue')
                  ->onDelete('cascade');
        });

        Schema::create('venues', function (Blueprint $table) {
            $table->increments('id');
            $table->integer("venue_owner_id")->unsigned();
            $table->string("venue");
            $table->timestamps();
            // CREATE THE ASSOCIATION/RELATIONSHIP USING FOREIGN KEY
            $table->foreign('venue_owner_id')
                  ->references('id')
                  ->on('venue_owner');
        });           
    }

    public function down() {
        // OTHER DROP COMMAND CODES...
        Schema::drop('venue_owner');
        Schema::drop('venues');
    }

Then in your Eloquent Model Class you can explicitly set the $this->hasMany() like so:

<?php

    namespace App;
    use Illuminate\Database\Eloquent\Model;

    class VenueOwner extends Model {
        /**
         * GET ALL THE venues FOR THE venue_owner .
         */
        public function venues()    {
            return $this->hasMany('App\Venues'); 
        }

        /**
         * GET ALL THE user FOR THE venue_owner .
         */
        public function user()    {
            return $this->hasOne('App\User'); 
        }

And in your Venues Eloquent Model Class, you do something like:

<?php

    namespace App;
    use Illuminate\Database\Eloquent\Model;

    class Venues extends Model {
        /**
         * GET THE venue_owner FOR venue(s).
         */
        public function venueOwner()    {
            return $this->belongsTo('App\VenueOwner'); 
        }

Last but not least in your Users Eloquent Model Class, you do something like:

<?php

    namespace App;
    use Illuminate\Database\Eloquent\Model;

    class Users extends Model {
        /**
         * GET THE user Information FOR venue_owner.
         */
        public function venueOwner()    {
            return $this-> hasOne('App\VenueOwner'); 
        }

Now, you can get all information about the venue_owner and his venues and roles & permissions using the user_id.