title: Laravel Controller : How to use function store and create
body: <p>I am currently new to laravel and I am working on a basic project where a service provider (SP) is able to post service details by filling up a form.</p>
  
  <p><img src="https://i.stack.imgur.com/NT7qj.jpg" alt="Service details form"></p>
  
  <p>I am trying to check if SP has uploaded a featured image. If yes, then rename file by using </p>
  
  <pre><code>$featured = $request-&gt;featured;
  $featured_new_name = time().$featured-&gt;getClientOriginalName();
  $featured-&gt;move('uploads/services', $featured_new_name);
  </code></pre>
  
  <p>and saving it to the <code>uploads/services/</code> directory.</p>
  
  <p>My <code>ServiceController.php</code> looks like this</p>
  
  <pre><code>public function store(Request $request)
  {
        $this-&gt;validate($request, [
        'name' =&gt; 'required|max:255',
        'address' =&gt; 'required|max:255',
        'city' =&gt; 'required|max:255',
        'state' =&gt; 'required|max:255',
        'zipcode' =&gt; 'required|integer|digits:5',
        'category_id' =&gt; 'required',
        'workingday' =&gt; 'required',
        'content' =&gt; 'required'
        ]);
  //checking if featured image (logo file) is uploaded or not 
  
      if($request-&gt;hasfile('featured'))
    {
      $featured = $request-&gt;featured;
      $featured_new_name = time().$featured-&gt;getClientOriginalName();
      $featured-&gt;move('uploads/services', $featured_new_name);
    }
    else {      }
  
       $featured-&gt;move('uploads/services', $featured_new_name);
  
        $service = Service::create([
        'name' =&gt; $request-&gt;name,
        'content' =&gt; $request-&gt;content,
        'address' =&gt; $request-&gt;address,
        'city' =&gt; $request-&gt;city,
        'state' =&gt; $request-&gt;state,
        'zipcode' =&gt; $request-&gt;zipcode,
        ]);
  
        if($request-&gt;hasfile('featured'))
        {
          $service = Service::create([
          'name' =&gt; $request-&gt;name,
          'content' =&gt; $request-&gt;content,
          'featured' =&gt;'uploads/services/'.$featured_new_name,
          'address' =&gt; $request-&gt;address,
          'city' =&gt; $request-&gt;city,
          'state' =&gt; $request-&gt;state,
          'zipcode' =&gt; $request-&gt;zipcode,
      ]);
   }
      $service-&gt;workingdays()-&gt;attach($request-&gt;workingday);
      $service-&gt;categories()-&gt;attach($request-&gt;category_id);
      $service-&gt;user_id=auth()-&gt;id();
      $service-&gt;save();
  
    Session::flash('success','Service created successfully');
    return view('sp.services.create')-&gt;with('categories', Category::all())-&gt;with('workingday', Workingday::all());
  }
  </code></pre>
  
  <p>In the migration file: </p>
  
  <pre><code>public function up()
  {
      Schema::create('services', function (Blueprint $table) {
          $table-&gt;increments('id');
          $table-&gt;string('name');
          $table-&gt;string('address');
          $table-&gt;string('city');
          $table-&gt;string('state');
          $table-&gt;string('zipcode');
          $table-&gt;integer('routes_id');
          $table-&gt;string('featured')-&gt;default('/uploads/services/default.png');
          $table-&gt;text('content')-&gt;nullable();
          $table-&gt;string('slug');
          $table-&gt;integer('user_id');
          $table-&gt;softDeletes();
          $table-&gt;timestamps();
  
      });
  }
  </code></pre>
  
  <p>I am saving the logged in <code>user_id</code> along with all other service details. 
  When the user uploads a logo file, this code stores two records in the database: one with the correct <code>user_id</code> and one with <code>0</code>. When the user does not upload the logo file, only one record is stored with all correct values.</p>
  
  <p><img src="https://i.stack.imgur.com/3XpZv.jpg" alt="created records in view"></p>
  
  <p>Here <code>user_id</code> 2 is a correct entry. Please help me improve this code.</p>