title: Laravel: How to get easily values from table with polymorphic relations eloquent model?
body: <p>I have employees list in my database table:</p>
  
  <p><strong>Table:</strong> Employees</p>
  
  <p><a href="https://i.stack.imgur.com/qPuK9.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qPuK9.jpg" alt="enter image description here"></a></p>
  
  <p><strong>Employee model code:</strong></p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>public function workplace()
  {
      return $this-&gt;morphTo();
  }
  </code></pre>
  
  <p><strong>Company model code:</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function workers()
  {
      return $this-&gt;morphMany('App\Employee', 'company');
  }
  </code></pre>
  
  <p><strong>Branch model code:</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function workers()
  {
      return $this-&gt;morphMany('App\Employee', 'company');
  }
  </code></pre>
  
  <p>Now I get user worklaces in sample different form:</p>
  
  <pre class="lang-php prettyprint-override"><code>$userWorkplaces = Employee::where('user_id', Auth::id())-&gt;get();
  $workplace_info = [];
  
  foreach ($userWorkplaces as $workplace) {
      if($workplace-&gt;company_type == "App\Company") {
          $company = Company::findOrFail($workplace-&gt;company_id);
          $type = 'Company';
      } elseif($workplace-&gt;company_type == "App\Branch") {
          $company = Branch::findOrFail($workplace-&gt;company_id);
          $type = 'Branch';
      }
  
      $workplace_info[] = [
          'company' =&gt; $company-&gt;name,
          'type' =&gt; $type
      ];
  }
  
  dump($workplace_info);
  </code></pre>
  
  <p><strong>Result:</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>array:3 [
    0 =&gt; array:2 [
      "company" =&gt; "Mahorat"
      "type" =&gt; "Company"
    ]
    1 =&gt; array:2 [
      "company" =&gt; "Babolo"
      "type" =&gt; "Company"
    ]
    2 =&gt; array:2 [
      "company" =&gt; "Филиал Махорат"
      "type" =&gt; "Branch"
    ]
  ]
  </code></pre>
  
  <p>How I can get easyly sample result by creating laravel relationships in models?</p>