title: Laravel - Repeating entry with unique parameter for different users
body: <p>Using Laravel 5.2.</p>
  
  <p>I'm developing a Phonebook project in Laravel, where you store contact info in a table named <code>Contacts</code>. 
  To create new contacts in this table, you have to be registered, and your info will be recorded in a <code>users</code> table</p>
  
  <p>I created a view to show the <code>Contacts</code> table and in the Controller I came up with a logic to only show the contacts that you created in the table. So, it means you cannot see the contacts another user created in the table.</p>
  
  <p>This logic is to prevent the user losing track of its contacts. For example, as a user I created only 2 contacts, but if my table has over 500 entries, I'll have to dig deep in the table to find my contact.</p>
  
  <p>BUT the contacts, specifically the <code>email</code> and <code>phone</code> entries, have to be unique so we don't have repeated info in the table.
  And this creates an issue, because I can't see the contact info other users create in this table. SO let's say that me and my brother want to store the contact of my sister only one of us will be able to create this info and only who created it will be able to see this info.</p>
  
  <p><strong>Here is my question</strong></p>
  
  <p>How do I "authorize" the identical contact to show up for an user that didn't created it without losing the <code>unique</code> parameter?</p>
  
  <p>Here is some of my code (including for some clarification):</p>
  
  <p><strong>Users Migration</strong></p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class PhonebookUsers extends Migration
  {
      protected $primaryKey = 'name';
      public $incrementing = false;
  
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('users', function (Blueprint $table) {
              $table-&gt;integer('id')-&gt;unsigned()-&gt;autoIncrement();
              $table-&gt;string('user_image');
              $table-&gt;string('name');
              $table-&gt;string('email', 191)-&gt;unique();
              $table-&gt;string('password', 191);
              $table-&gt;timestamp('updated_at');
              $table-&gt;timestamp('created_at');
              $table-&gt;rememberToken();
          });
      }
  </code></pre>
  
  <p><strong>User Validation Parameters</strong></p>
  
  <pre><code>...
  
  public function rules()
      {
          return [
              'user_image' =&gt; 'file',
              'name'       =&gt; 'required|alpha|min:3|max:255',
              'email'      =&gt; 'required|unique:users,email|email',
              'password'   =&gt; 'required|alpha_num|between:6,100',
          ];
      }
  
  ...
  </code></pre>
  
  <p><strong>Contacts Migration</strong></p>
  
  <p>...</p>
  
  <pre><code>public function up()
      {
          Schema::create('contacts', function (Blueprint $table) {
              $table-&gt;integer('id')-&gt;unsigned()-&gt;autoIncrement();
              $table-&gt;integer('user_id')-&gt;unsigned();
              $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
              $table-&gt;string('image');
              $table-&gt;string('name');
              $table-&gt;string('lastname');
              $table-&gt;string('email', 191);
              $table-&gt;string('phone',191);
              $table-&gt;string('address');
              $table-&gt;string('description', 255);
          });
      }
  
  ...
  </code></pre>
  
  <p>Note that the migration includes a column, <code>user_id</code>, to identify, by id, the user who created a contact.</p>
  
  <p><strong>Contacts Validation Parameters</strong></p>
  
  <pre><code>...
  
  public function rules()
      {
          return [
              'image'       =&gt; 'file',
              'name'        =&gt; 'alpha|min:3|max:15|required',
              'lastname'    =&gt; 'alpha|min:3|max:15',
              'email'       =&gt; 'required|email|unique:contacts,email',
              'phone'       =&gt; 'required|alpha_num|between:3,25|unique:contacts,phone',
              'address'     =&gt; 'max:255',
              'description' =&gt; 'max:255'
          ];
      }
  
  ...
  </code></pre>
  
  <p><strong>ContactsController</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App;
  use App\Contact as Contact;
  use App\User as User;
  use DB;
  use Illuminate\Http\Request;
  use App\Http\Controllers\Auth;
  
  use App\Http\Requests\CreateContactRequest;
  use App\Http\Requests\StoreContactRequest;
  
  use Illuminate\Support\Facades\Input;
  use Validator;
  
  class ContactsController extends Controller
  {
      // showing the form in the phonebook.blade.php
      // the form is structured using html and stuff
      public function index()
      {
          return view('phonebook');
      }
  
      // showing the table where I save my contacts
      // simple view stuff
      public function tab()
      {
          if(\Auth::check())
          {
              $contacts = App\Contact::where('user_id', \Auth::user()-&gt;id)-&gt;get();
              return view('contacts', compact('contacts'));
          }
          else
          {
              return view('contacts', compact('contacts'));
          }
      }
  
  public function create(CreateContactRequest $request)
      {
          $contact                = new Contact;            
          $contact-&gt; name         = $request-&gt;get('name');
          $contact-&gt; lastname     = $request-&gt;get('lastname');
          $contact-&gt; email        = $request-&gt;get('email');
          $contact-&gt; phone        = $request-&gt;get('phone');
          $contact-&gt; address      = $request-&gt;get('address');
          $contact-&gt; description  = $request-&gt;get('description');
          $contact-&gt; user_id      = \Auth::user()-&gt;id;
  
          // Checking if input has a file
          if ($request-&gt;hasFile('image')) 
          {            
              // It does, so set name to a random string with 10 chars
              $fileName = str_random(10);
  
              // Get the extension of the file (.jpg, .png...)
              $fileName .= '.' . $request-&gt;file('image')-&gt;getClientOriginalExtension();
  
              // Move the file to the storage
              $request-&gt;file('image')-&gt;move(storage_path('app/public/uploads'), $fileName);      
  
              // Attribute the contact to the image and then to the name of the file
              // one thing "pulling" another
              $contact-&gt;image = $fileName;      
          }
          // new update: echo the same contacts for different users
          // so, the $contact-&gt;save(); line would be an else for
          // really unique contacts 
          $contact-&gt;save();
  
          $contacts = App\Contact::where('user_id', \Auth::user()-&gt;id)-&gt;get();
          return view('contacts', compact('contacts'));
      }
  
      // editing a contact, taking all the info that is in my contacts table according to the id in that row    
      // permeating an edit form similar to the one in the main form using the edit.blade.php
      public function edit($id)
      {
          $contact = Contact::where('user_id', \Auth::user()-&gt;id)-&gt;findOrFail($id);        
  
          return view('edit', compact('contact'));    
      }
  
      // Create the update request for the table
      public function update($id)
      {
          $contact                = Contact::findOrFail($id);
          $contact -&gt; name        = Input::get('name');
          $contact -&gt; lastname    = Input::get('lastname');
          $contact -&gt; email       = Input::get('email');
          $contact -&gt; phone       = Input::get('phone');
          $contact -&gt; address     = Input::get('address');
          $contact -&gt; description = Input::get('description');
  
          $contact -&gt; save();
  
          $contacts = App\Contact::where('user_id', \Auth::user()-&gt;id)-&gt;get();
          return view('contacts', compact('contacts'));   
      }
  
      // deleting an input in my table, called with a link, going to delete.blade.php.
      // the function is called when loading the delete.blade.php
      public function destroy($id)
      {
          // finding the id of my contact and deleting
          // everything under this id which means
          // my entire contact info
          $contact = Contact::where('user_id', \Auth::user()-&gt;id)-&gt;findOrFail($id);
          $contact-&gt;delete();
  
          // this will return the delete view, which is the confirmation that the code works
          return view('delete');
      }
  
      // tests about uploading photos. create a button or link in the table to redirect to the upload page    
      // select a pic using an input form, then use the move method to store my pic in a directory
      // but let's begin with simplicity let's just go to the form that'll request the photo
      public function upload($id)
      {
          // simply return the view upload.blade.php that has the form to input my pic
          $contact = Contact::where('user_id', \Auth::user()-&gt;id)-&gt;findOrFail($id);
          return view('upload', compact('contact'));
      }
  
      // moving the file to a dir
      public function move(Request $request, $id)
      {
          // check if the input has a file
          if ($request-&gt;hasFile('image')) {
  
              // if it does create a name for the file, a random string with 10 characters (default 40)            
              $fileName = str_random(10);
  
              // take this name we just created add the file's extension using the original extension             
              //adiciona extensão da imagem no nome do arquivo
              $fileName .= '.' . $request-&gt;file('image')-&gt;getClientOriginalExtension();
  
              // save image to the right diretory
              // Salva a imagem no diretorio selecionado
              $request-&gt;file('image')-&gt;move(storage_path('app/public/uploads'), $fileName);    
  
              // Attribute the contact, using its id, to the image uploaded and then to the name of the file
              // like in the create method, one thing pulls the other
              $contact        = Contact::findOrFail($id);
              $contact-&gt;image = $fileName;
  
              // then save the contact with everything attributed
              $contact-&gt;save();        
          }
  
          $contacts = App\Contact::where('user_id', \Auth::user()-&gt;id)-&gt;get();
          // finally return the view
          return view('contacts', compact('contacts'));
      }
  }
  </code></pre>
  
  <p>Thanks in advance.</p>