title: Laravel backpack relationship doubts
body: <p>I am using laravel backpack to build a backoffice administration and im stuck at this, that is maybe a very basic doubt.</p>
  
  <p>Im in a case where i have:</p>
  
  <p><strong>Table Event:</strong></p>
  
  <p>id - name - themeID - more fields...</p>
  
  <p><strong>Table Theme:</strong></p>
  
  <p>id - name</p>
  
  <p>So, a theme can have various events and a event can have various themes, i have some doubts on how to achieve this, what i did:</p>
  
  <p><strong>Theme.php</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  use Backpack\CRUD\CrudTrait;
  
  class Theme extends Model
  {
      use CrudTrait;
  
       /*
      |--------------------------------------------------------------------------
      | GLOBAL VARIABLES
      |--------------------------------------------------------------------------
      */
  
      protected $table = 'themes';
      //protected $primaryKey = 'id';
      // public $timestamps = false;
      // protected $guarded = ['id'];
      protected $fillable = ['name'];
      // protected $hidden = [];
      // protected $dates = [];
  
      /*
      |--------------------------------------------------------------------------
      | FUNCTIONS
      |--------------------------------------------------------------------------
      */
  
      /*
      |--------------------------------------------------------------------------
      | RELATIONS
      |--------------------------------------------------------------------------
      */
  
      public function Theme() {
        return $this-&gt;hasMany('App\Models\Event');
      }
  
      /*
      |--------------------------------------------------------------------------
      | SCOPES
      |--------------------------------------------------------------------------
      */
  
      /*
      |--------------------------------------------------------------------------
      | ACCESORS
      |--------------------------------------------------------------------------
      */
  
      /*
      |--------------------------------------------------------------------------
      | MUTATORS
      |--------------------------------------------------------------------------
      */
  }
  </code></pre>
  
  <p><strong>Event.php</strong></p>
  
  <pre><code>    &lt;?php
  
  namespace App\Models;
  
  use Illuminate\Database\Eloquent\Model;
  use Backpack\CRUD\CrudTrait;
  
  class Event extends Model
  {
      use CrudTrait;
  
       /*
      |--------------------------------------------------------------------------
      | GLOBAL VARIABLES
      |--------------------------------------------------------------------------
      */
  
      protected $table = 'events';
      //protected $primaryKey = 'id';
      // public $timestamps = false;
      // protected $guarded = ['id'];
      protected $fillable = ['name','theme','schedules','themeID','countyID','event_typeID','offered_conditionID','alone_with_typeID','tagID',
      'advertiserID','event_languageID','special_priceID','start_date','end_date','price','photos','space_name','address','description','facebook_url',
      'external_url1','featured_image','available_vacancies','gps_coordinates','external_url2','featured','status','start_featured_date','end_featured_date'];
  
      // protected $hidden = [];
      // protected $dates = [];
  
      /*
      |--------------------------------------------------------------------------
      | FUNCTIONS
      |--------------------------------------------------------------------------
      */
  
      /*
      |--------------------------------------------------------------------------
      | RELATIONS
      |--------------------------------------------------------------------------
      */
  
      public function Event() {
          return $this-&gt;hasOne('App\Models\EventSubType');
          return $this-&gt;hasMany('App\Models\SpecialPrice');
          return $this-&gt;hasMany('App\Models\PriceTypes');
          return $this-&gt;hasMany('App\Models\Tag');
          // return $this-&gt;hasMany('App\Models\County');
          return $this-&gt;hasMany('App\Models\County');
          return $this-&gt;hasMany('App\Models\EventHasSpecialPrice');
      }
  
      public function themes()
      {
          return $this-&gt;belongsToMany('App\Models\Theme');
      }
  
      /*
      |--------------------------------------------------------------------------
      | SCOPES
      |--------------------------------------------------------------------------
      */
  
      /*
      |--------------------------------------------------------------------------
      | ACCESORS
      |--------------------------------------------------------------------------
      */
  
      /*
      |--------------------------------------------------------------------------
      | MUTATORS
      |--------------------------------------------------------------------------
      */
  }
  </code></pre>
  
  <p><strong>EventCrudController.php</strong></p>
  
  <pre><code>    &lt;?php
  
  namespace App\Http\Controllers\Admin;
  
  use Backpack\CRUD\app\Http\Controllers\CrudController;
  
  // VALIDATION: change the requests to match your own file names if you need form validation
  use App\Http\Requests\EventRequest as StoreRequest;
  use App\Http\Requests\EventRequest as UpdateRequest;
  
  class EventCrudController extends CrudController
  {
  
      public function setUp()
      {
  
          /*
          |--------------------------------------------------------------------------
          | BASIC CRUD INFORMATION
          |--------------------------------------------------------------------------
          */
          $this-&gt;crud-&gt;setModel('App\Models\Event');
          $this-&gt;crud-&gt;setRoute(config('backpack.base.route_prefix') . '/events');
          $this-&gt;crud-&gt;setEntityNameStrings('event', 'events');
  
          /*
          |--------------------------------------------------------------------------
          | BASIC CRUD INFORMATION
          |--------------------------------------------------------------------------
          */
  
         /* $this-&gt;crud-&gt;addColumn([
              'label' =&gt; "Theme", // Table column heading
              'type' =&gt; "select_multiple",
              'name' =&gt; 'themes', // the column that contains the ID of that connected entity;
              'entity' =&gt; 'themes', // the method that defines the relationship in your Model
              'attribute' =&gt; "name", // foreign key attribute that is shown to user
              'model' =&gt; 'App\Models\Theme', // foreign key model
              'pivot' =&gt; true,
          ]); */
  
          $this-&gt;crud-&gt;setFromDb();
  
          $this-&gt;crud-&gt;addFields([
          // This is the field im trying to get working
            [
                'label' =&gt; 'Theme',
                'type' =&gt; 'select_multiple',
                'name' =&gt; 'themeID',
                'entity'=&gt; 'Event',
                'attribute' =&gt; 'name', // Column which user see in select box
                'model' =&gt; 'App\Models\Theme' // Model which contain FK
            ],
            [
                'label' =&gt; 'District',
                'type' =&gt; 'select2',
                'name' =&gt; 'countyID',
                'entity'=&gt; 'district',
                'attribute' =&gt; 'name', // Column which user see in select box
                'model' =&gt; 'App\Models\District' // Model which contain FK
            ],
            [
                'label' =&gt; 'Event Type',
                'type' =&gt; 'select2',
                'name' =&gt; 'event_typeID',
                'entity'=&gt; 'EventType',
                'attribute' =&gt; 'name', // Column which user see in select box
                'model' =&gt; 'App\Models\EventType' // Model which contain FK
            ],
            [
                'label' =&gt; 'Offered Conditions',
                'type' =&gt; 'select2',
                'name' =&gt; 'offered_conditionID',
                'entity'=&gt; 'OfferedCondition',
                'attribute' =&gt; 'name', // Column which user see in select box
                'model' =&gt; 'App\Models\OfferedCondition' // Model which contain FK
            ],
            [
                'label' =&gt; 'Alone With Types',
                'type' =&gt; 'select2',
                'name' =&gt; 'alone_with_typeID',
                'entity'=&gt; 'AloneWithType',
                'attribute' =&gt; 'name', // Column which user see in select box
                'model' =&gt; 'App\Models\AloneWithType' // Model which contain FK
            ],
            [
                'label' =&gt; 'Tags',
                'type' =&gt; 'select2',
                'name' =&gt; 'tagID',
                'entity'=&gt; 'Tag',
                'attribute' =&gt; 'name', // Column which user see in select box
                'model' =&gt; 'App\Models\Tag' // Model which contain FK
            ],
            [
                'label' =&gt; 'Advertiser',
                'type' =&gt; 'select2',
                'name' =&gt; 'advertiserID',
                'entity'=&gt; 'Advertiser',
                'attribute' =&gt; 'name', // Column which user see in select box
                'model' =&gt; 'App\Models\Advertiser' // Model which contain FK
            ],
            [
                'label' =&gt; 'Event Language',
                'type' =&gt; 'select2',
                'name' =&gt; 'event_languageID',
                'entity'=&gt; 'Event_language',
                'attribute' =&gt; 'name', // Column which user see in select box
                'model' =&gt; 'App\Models\EventLanguage' // Model which contain FK
            ],
            [
                'label' =&gt; 'Special Price',
                'type' =&gt; 'select2',
                'name' =&gt; 'special_priceID',
                'entity'=&gt; 'SpecialPrice',
                'attribute' =&gt; 'name', // Column which user see in select box
                'model' =&gt; 'App\Models\SpecialPrice' // Model which contain FK
          ]
          ]);
  
          /* $this-&gt;crud-&gt;addField([
                'label' =&gt; "Images",
                'name' =&gt; "photos",
                'type' =&gt; 'image_multiple',
                'upload' =&gt; true,
                'crop' =&gt; true, // set to true to allow cropping, false to disable
                'aspect_ratio' =&gt; 0, // ommit or set to 0 to allow any aspect ratio
            ]); */
  
          // $this-&gt;crud-&gt;addFields($array_of_arrays, 'update/create/both');
          // $this-&gt;crud-&gt;removeField('name', 'update/create/both');
          // $this-&gt;crud-&gt;removeFields($array_of_names, 'update/create/both');
  
          // ------ CRUD COLUMNS
          // $this-&gt;crud-&gt;addColumn(); // add a single column, at the end of the stack
          // $this-&gt;crud-&gt;addColumns(); // add multiple columns, at the end of the stack
          // $this-&gt;crud-&gt;removeColumn('column_name'); // remove a column from the stack
          // $this-&gt;crud-&gt;removeColumns(['column_name_1', 'column_name_2']); // remove an array of columns from the stack
          // $this-&gt;crud-&gt;setColumnDetails('column_name', ['attribute' =&gt; 'value']); // adjusts the properties of the passed in column (by name)
          // $this-&gt;crud-&gt;setColumnsDetails(['column_1', 'column_2'], ['attribute' =&gt; 'value']);
  
          // ------ CRUD BUTTONS
          // possible positions: 'beginning' and 'end'; defaults to 'beginning' for the 'line' stack, 'end' for the others;
          // $this-&gt;crud-&gt;addButton($stack, $name, $type, $content, $position); // add a button; possible types are: view, model_function
          // $this-&gt;crud-&gt;addButtonFromModelFunction($stack, $name, $model_function_name, $position); // add a button whose HTML is returned by a method in the CRUD model
          // $this-&gt;crud-&gt;addButtonFromView($stack, $name, $view, $position); // add a button whose HTML is in a view placed at resources\views\vendor\backpack\crud\buttons
          // $this-&gt;crud-&gt;removeButton($name);
          // $this-&gt;crud-&gt;removeButtonFromStack($name, $stack);
  
          // ------ CRUD ACCESS
          // $this-&gt;crud-&gt;allowAccess(['list', 'create', 'update', 'reorder', 'delete']);
          // $this-&gt;crud-&gt;denyAccess(['list', 'create', 'update', 'reorder', 'delete']);
  
          // ------ CRUD REORDER
          // $this-&gt;crud-&gt;enableReorder('label_name', MAX_TREE_LEVEL);
          // NOTE: you also need to do allow access to the right users: $this-&gt;crud-&gt;allowAccess('reorder');
  
          // ------ CRUD DETAILS ROW
          // $this-&gt;crud-&gt;enableDetailsRow();
          // NOTE: you also need to do allow access to the right users: $this-&gt;crud-&gt;allowAccess('details_row');
          // NOTE: you also need to do overwrite the showDetailsRow($id) method in your EntityCrudController to show whatever you'd like in the details row OR overwrite the views/backpack/crud/details_row.blade.php
  
          // ------ REVISIONS
          // You also need to use \Venturecraft\Revisionable\RevisionableTrait;
          // Please check out: https://laravel-backpack.readme.io/docs/crud#revisions
          // $this-&gt;crud-&gt;allowAccess('revisions');
  
          // ------ AJAX TABLE VIEW
          // Please note the drawbacks of this though:
          // - 1-n and n-n columns are not searchable
          // - date and datetime columns won't be sortable anymore
          // $this-&gt;crud-&gt;enableAjaxTable();
  
          // ------ DATATABLE EXPORT BUTTONS
          // Show export to PDF, CSV, XLS and Print buttons on the table view.
          // Does not work well with AJAX datatables.
          // $this-&gt;crud-&gt;enableExportButtons();
  
          // ------ ADVANCED QUERIES
          // $this-&gt;crud-&gt;addClause('active');
          // $this-&gt;crud-&gt;addClause('type', 'car');
          // $this-&gt;crud-&gt;addClause('where', 'name', '==', 'car');
          // $this-&gt;crud-&gt;addClause('whereName', 'car');
          // $this-&gt;crud-&gt;addClause('whereHas', 'posts', function($query) {
          //     $query-&gt;activePosts();
          // });
          // $this-&gt;crud-&gt;addClause('withoutGlobalScopes');
          // $this-&gt;crud-&gt;addClause('withoutGlobalScope', VisibleScope::class);
          // $this-&gt;crud-&gt;with(); // eager load relationships
          // $this-&gt;crud-&gt;orderBy();
          // $this-&gt;crud-&gt;groupBy();
          // $this-&gt;crud-&gt;limit();
      }
  
      public function store(StoreRequest $request)
         {
  
       /*
        // Setup storage
        $attribute_name = "photos";
        $disk = "uploads";
        $destination_path = "/uploads/projects";
  
          // Then get images from request
          $input = $request-&gt;all();
          $images = $input[$attribute_name];
          $imageArray = [];
          // Now iterate images
        foreach ($images as $value) {
            // Store on disk and add to array
            if (starts_with($value, 'data:image'))
            {
                  // 0. Make the image
                  $image = \Image::make($value);
                  // 1. Generate a filename.
                  $filename = md5($value.time()).'.jpg';
                  // 2. Store the image on disk.
                  \Storage::disk($disk)-&gt;put($destination_path.'/'.$filename, $image-&gt;stream());
                  // 3. Save the path to the database
                  array_push($imageArray, $destination_path.'/'.$filename);
              }
          }
  
          // Update $request with new array
          $request-&gt;request-&gt;set($attribute_name, $imageArray);
          */
              // Save $request
          $redirect_location = parent::storeCrud($request);
          // your additional operations after save here
          // use $this-&gt;data['entry'] or $this-&gt;crud-&gt;entry
          return $redirect_location;
      }
  
      public function update(UpdateRequest $request)
      {
          // Setup storage
          $attribute_name = "images";
          $disk = "uploads";
          $destination_path = "/uploads/projects";
          // Then get images from request
          $input = $request-&gt;all();
          $images = $input[$attribute_name];
          $imageArray = [];
          // Now iterate images
          foreach ($images as $value) {
              // Store on disk and add to array
              if (starts_with($value, 'data:image'))
              {
                  // 0. Make the image
                  $image = \Image::make($value);
                  // 1. Generate a filename.
                  $filename = md5($value.time()).'.jpg';
                  // 2. Store the image on disk.
                  \Storage::disk($disk)-&gt;put($destination_path.'/'.$filename, $image-&gt;stream());
                  // 3. Save the path to the database
                  array_push($imageArray, $destination_path.'/'.$filename);
              } else {
                  array_push($imageArray, $value);
              }
          }
          // Update $request with new array
          $request-&gt;request-&gt;set($attribute_name, $imageArray);
          // your additional operations before save here
          $redirect_location = parent::updateCrud($request);
          // your additional operations after save here
          // use $this-&gt;data['entry'] or $this-&gt;crud-&gt;entry
          return $redirect_location;
      }
  
      /* public function store(StoreRequest $request)
      {
          // your additional operations before save here
          $redirect_location = parent::storeCrud();
          // your additional operations after save here
          // use $this-&gt;data['entry'] or $this-&gt;crud-&gt;entry
          return $redirect_location;
      }
  
      public function update(UpdateRequest $request)
      {
          // your additional operations before save here
          $redirect_location = parent::updateCrud();
          // your additional operations after save here
          // use $this-&gt;data['entry'] or $this-&gt;crud-&gt;entry
          return $redirect_location;
      } */
  }
  </code></pre>
  
  <p>Do i need a pivot table ? What am i missing ? I find little documentation on this.</p>
  
  <p>The error im getting is the following one:</p>
  
  <p>Array to string conversion, he tries to save on the field array('1','2') and the error happens there.</p>