title: Assign User to any Shop in Laravel Relationship?
body: <p>In laravel, I have 3 table</p>
  
  <pre>
   User // for Authentication and Create Another User Once logged in
   Expense
   Shop
  </pre>
  
  <p><b>My Purpose-</b> I want user can register and Also, can create another user when they logged in And Can <code>assign</code> user to another <code>Shop</code> as they want..</p>
  
  <p>And Only <code>User in the Same Shop</code> Can see their <code>Expense</code>..</p>
  
  <p>// My <code>User</code> Table</p>
  
  <pre><code>&lt;pre&gt;
     Schema::create('users', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;unsignedBigInteger('shop_id')-&gt;nullable();
          $table-&gt;unsignedBigInteger('user_id')-&gt;nullable();
          $table-&gt;string('name');
          $table-&gt;string('email')-&gt;unique();
          $table-&gt;timestamp('email_verified_at')-&gt;nullable();
          $table-&gt;string('password');
          $table-&gt;rememberToken();
          $table-&gt;timestamps();
      });
  &lt;/pre&gt;
  </code></pre>
  
  <p>// My <code>Expense</code> Table</p>
  
  <pre><code>&lt;pre&gt;
      Schema::create('expenses', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;unsignedBigInteger('user_id');
          $table-&gt;date('date');
          $table-&gt;string('description');
          $table-&gt;double('amount');
          $table-&gt;timestamps();
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');
      });
  &lt;/pre&gt;
  </code></pre>
  
  <p>// My <code>Shop</code> Table</p>
  
  <pre><code>&lt;pre&gt;
       Schema::create('shops', function (Blueprint $table) {
          $table-&gt;bigIncrements('id');
          $table-&gt;unsignedBigInteger('expense_id')-&gt;nullable();
          $table-&gt;unsignedBigInteger('user_id');
          $table-&gt;string('name');
          $table-&gt;string('description');
          $table-&gt;timestamps();
          $table-&gt;foreign('expense_id')-&gt;references('id')-&gt;on('expenses');         
          $table-&gt;foreign('user_id')-&gt;references('id')-&gt;on('users');         
      });
  &lt;/pre&gt;
  </code></pre>
  
  <p>// My User Model</p>
  
  <pre><code>&lt;pre&gt;
     public function expense()
      {
          return $this-&gt;hasMany(\App\Expense::class);
      }
  
  
      public function shop()
      {
          return $this-&gt;hasMany(\App\Shop::class, 'user_id');
      }
  &lt;/pre&gt;
  </code></pre>
  
  <p>// My Expense Model</p>
  
  <pre><code>&lt;pre&gt;
  class Expense extends Model
  {
      protected $fillable = ['date', 'description', 'amount', 'user_id', 'shop_id'];
  
      public function user()
      {
          return $this-&gt;belongsTo(\App\User::class);  
      }
  
  }
  &lt;/pre&gt;
  </code></pre>
  
  <p>// My Shop Model</p>
  
  <pre><code>&lt;pre&gt;
  class Shop extends Model
  {
      protected $fillable = ['name', 'description', 'expense_id', 'shop_id'];
  
      public function user()
      {
          return $this-&gt;belongsTo(\App\User::class, 'user_id');
      }
  
  }
  &lt;/pre&gt;
  </code></pre>
  
  <p>// Expense Controller </p>
  
  <pre><code>&lt;pre&gt;
   public function index(Request $request)
      {
          $expense = Expense::with(['user'])-&gt;get();
  
          return ExpenseResource::collection($expense);
  
          // dd(auth()-&gt;user());
      }
  
      public function create(Request $request)
      {
          $request-&gt;validate([
              'date' =&gt; 'required',
              'description' =&gt; 'required',
              'amount' =&gt; 'required',
  
          ]);
  
          $expense = new Expense();
          $expense-&gt;user_id = auth()-&gt;user()-&gt;id;
          $expense-&gt;date = $request-&gt;date;
          $expense-&gt;description = $request-&gt;description;
          $expense-&gt;amount = $request-&gt;amount;
          $expense-&gt;save();
  
          return new ExpenseResource($expense);
      }
  &lt;/pre&gt;
  </code></pre>
  
  <p>// in My UserController </p>
  
  <pre><code>&lt;pre&gt;
  public function index()
      {
          $users = User::all();
          $shops = Shop::all();
          return view('user', compact('users', 'shops'));
          // return UserResource::collection($users);
      }
  
      public function create(Request $request)
      {
          $request-&gt;validate([
              'name' =&gt; 'required',
              'email' =&gt; 'required',
              'password' =&gt; 'required',
          ]);
  
          $user = new user();
          $user-&gt;user_id = auth()-&gt;user()-&gt;id;
          $user-&gt;name = $request-&gt;name;
          $user-&gt;email = $request-&gt;email;
          $user-&gt;password = bcrypt($request-&gt;password);
          $user-&gt;save();
  
          return new UserResource($user);
      }
  &lt;/pre&gt;
  </code></pre>
  
  <p>Is it make sense?</p>
  
  <p>Any idea, thanks..</p>