title: Accessing data from another table having one to many relationship in blade laravel
body: <p>I have one to many relationship of Product with Book.</p>
  
  <p>Function in my controller:</p>
  
  <pre><code>public function editProduct($p_id)
  {
     $books = Product::findOrFail($p_id);
     return view('master.editProduct')-&gt;with(['books'=&gt;$books]);
  }
  </code></pre>
  
  <p>editProduct.blade.php</p>
  
  <pre><code>&lt;!--Returns Name from product table--&gt; 
  &lt;div class="form-group"&gt;
    &lt;label&gt;Book Title&lt;/label&gt;
    &lt;input type="text" class="form-control" name="name" value = "{{ $books-&gt;name }}"&gt;
  &lt;/div&gt;
  
  &lt;!--Should return author from books table--&gt;
  &lt;div class="form-group"&gt;
    &lt;label&gt;Author&lt;/label&gt;
    &lt;input type="text" class="form-control" name="author" " value = "{{ $books-&gt;books-&gt;author  }}"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I also tried: </p>
  
  <ul>
  <li><code>$books->books</code> which returned data of all column of books table for p_id</li>
  <li><code>$books->books->author</code> to get only author from books table but returned error. i get error: Property [author] does not exist on this collection instance.
  How can i get the value of author from books table in my view?</li>
  </ul>
  
  <p>Product Model</p>
  
  <pre><code>&lt;?php
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Product extends Model
  {
     protected $tables= "products";
     protected $primaryKey = 'product_id';
     public $incrementing = false;
  
     protected $fillable = ['product_id','product_type','name', ...,];
  
     protected $hidden =['created_at', 'updated_at'];
  
     public function books(){
      return $this-&gt;hasMany('App\Book', 'product_id');
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Book Model</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Book extends Model
  {
    protected $tables= "books";
  
    protected $fillable = ['product_id','author', 'edition',...];
  
    protected $hidden =['created_at', 'updated_at'];
  
    public function products(){
      return $this-&gt;belongsTo('App\Product');
    }
  
  }
  </code></pre>
  
  <p>EditForm fields</p>
  
  <ul>
  <li>Title</li>
  <li>Author</li>
  <li>Publication
  .
  .
  .</li>
  </ul>
  
  <p>Title is stored in Products table which i am able to access in my form as</p>
  
  <pre><code>&lt;div class="form-group"&gt;
    &lt;label&gt;Book Title&lt;/label&gt;
    &lt;input type="text" class="form-control"  name="name" value = "{{ $books-&gt;name }}"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>In the same form author is not accessed.</p>