title: laravel: eloquent example insert data to database
body: <p>I am sorry to ask this but I come from codeIgniter and have a really hard time understanding eloquent model insertion. This is a new way of working with models for me.</p>
  
  <p>i have read <a href="http://daylerees.com/codebright/eloquent-relationships" rel="noreferrer">this article</a> and understand some basics now.</p>
  
  <p>I have the following example. I have a product which has many attributes but the relevant one is brand and product name. (see the following example tables)</p>
  
  <p>products: id(PK),name,description,brand_id
  brands: id(PK),name</p>
  
  <p>Now here comes the problem. I know i can create a new brand and I know how I can create a new product. However I have no qlue how to connect the two together. Here is some code I have right now. I want to create a new product and automatically fill the brand table. This is a part of the controller right now. </p>
  
  <p>In short. I want the brands.id inside the products.brand_id</p>
  
  <pre><code>    $product = new Products;
      $product-&gt;name = "product1";
      $product-&gt;description = "description1";
  
      $brand = new Brands;
      $brand-&gt;name = "brand1"
      $product-&gt;brand()-&gt;associate($brand);
  
      $brand-&gt;save(); 
      $product-&gt;save(); 
  </code></pre>
  
  <p>To make it more clear. I have 2 models. products and brands models. However it is not clear to me what should be in the model. This is my Current products model. I think the brands model only should have a protected $table = "brands"; line inside the model class.</p>
  
  <pre><code> class Products extends Eloquent  {
  
      protected $table = 'products';
  
      public function brand()
      {
         return $this-&gt;hasOne('brands');
      }
    }
  </code></pre>
  
  <p>Can somebody explain to me what i do wrong. I cannot find a good tutorial how to work with inserting data inside eloquent models with relationships. The most tutorials are about displaying data.</p>