title: Problem with displaying data with many to many relationship in Laravel
body: <p>I have many to many relationship between users and products table, and I set up pivot table product_user with product_id, user_id columns. When I try and upload some file it uploads successfully to the database and it shows data on page, but it doesn't show column 'owner of the product' which is column where is that relationship. Also when I manually update my database table product_user and set correct ids and refresh page then 'owner of the product' column shows correct information. I would like to display that information when I upload file dynamically without manually updating the database. I tried with attach method but for some reason it won't work. Any help is appreciated Here is my code:</p>
  
  <p>HomeController:</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\Product;
  use Illuminate\Http\Request;
  use Illuminate\Support\Facades\DB;
  
  class HomeController extends Controller
  {
      public function store(Request $request)
      {
          if ($file = $request-&gt;file('files')) {
              $name = $file-&gt;getClientOriginalName();
              if ($file-&gt;move('products', $name)) {
                  $product = new Product();
                  $product-&gt;files = $name;
                  $product-&gt;save();
                  $product-&gt;users()-&gt;attach($request-&gt;user_id);
  
                  return redirect()-&gt;route('welcome');
              }
              return redirect()-&gt;back();
          }
      }
  }
  </code></pre>
  
  <p>welcome.blade.php:</p>
  
  <pre><code>@if($products)
  &lt;table class="table"&gt;
      &lt;thead&gt;
          &lt;th&gt;#&lt;/th&gt;
          &lt;th&gt;Product Name&lt;/th&gt;
          &lt;th&gt;Owner Of The Product&lt;/th&gt;
          &lt;th&gt;Created At&lt;/th&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      @foreach ($products as $product)
           &lt;tr&gt;
               &lt;td&gt;{{ $product-&gt;id }}&lt;/td&gt;
               &lt;td&gt;{{ $product-&gt;files }}&lt;/td&gt;
               &lt;td&gt;
                   @foreach ($product-&gt;users as $user)
                       {{ $user-&gt;name }}
                   @endforeach
               &lt;/td&gt;
               &lt;td&gt;{{ date('M j, Y', strtotime($product-&gt;created_at)) }&lt;/td&gt;
           &lt;/tr&gt;
      @endforeach
      &lt;/tbody&gt;
   &lt;/table&gt;
   @endif
  </code></pre>
  
  <p>home.blade.php:</p>
  
  <pre><code>&lt;div class="col-md-12"&gt;
      &lt;form action="/store" method="POST" enctype="multipart/form-data"&gt;
          {{ csrf_field() }}
          &lt;div class="form-group"&gt;
              &lt;input type="file" name="files" class="form-control"&gt;
          &lt;/div&gt;
          &lt;div class="form-group"&gt;
              &lt;input type="submit" class="btn btn-success" value="Submit"&gt;
          &lt;/div&gt;
       &lt;/form&gt; 
   &lt;/div&gt;
  </code></pre>
  
  <p>User.php:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use App\Product;
  use Illuminate\Contracts\Auth\MustVerifyEmail;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  use Illuminate\Notifications\Notifiable;
  
  class User extends Authenticatable
  {
      use Notifiable;
  
      protected $fillable = [
      'name', 'email', 'password',
      ];
  
      protected $hidden = [
      'password', 'remember_token',
      ];
  
      protected $casts = [
      'email_verified_at' =&gt; 'datetime',
      ];
  
      public function products()
      {
          return $this-&gt;belongsToMany(Product::class, 'product_user', 'user_id', 'product_id');
      }
  }
  </code></pre>
  
  <p>Product.php:</p>
  
  <pre><code>&lt;?php
  namespace App;
  
  use App\User;
  use Illuminate\Database\Eloquent\Model;
  
  class Product extends Model
  {
      protected $fillable = ['files'];
  
      public function users()
      {
          return $this-&gt;belongsToMany(User::class, 'product_user', 'product_id', 'user_id');
      }
  }
  </code></pre>