将用户分配给Laravel关系中的任何商店?
在laravel中,我有3张桌子
In laravel, I have 3 table
User // for Authentication and Create Another User Once logged in
Expense
Shop
我的目的-我希望用户可以注册并且还可以在他们登录时创建另一个用户,并且assign
用户可以根据需要登录到另一个Shop
.
My Purpose- I want user can register and Also, can create another user when they logged in And Can assign
user to another Shop
as they want..
并且只有User in the Same Shop
可以看到他们的Expense
..
And Only User in the Same Shop
Can see their Expense
..
//我的User
表格
<pre>
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('shop_id')->nullable();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
</pre>
//我的Expense
表格
<pre>
Schema::create('expenses', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->date('date');
$table->string('description');
$table->double('amount');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
</pre>
//我的Shop
表格
<pre>
Schema::create('shops', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('expense_id')->nullable();
$table->unsignedBigInteger('user_id');
$table->string('name');
$table->string('description');
$table->timestamps();
$table->foreign('expense_id')->references('id')->on('expenses');
$table->foreign('user_id')->references('id')->on('users');
});
</pre>
//我的用户模型
<pre>
public function expense()
{
return $this->hasMany(\App\Expense::class);
}
public function shop()
{
return $this->hasMany(\App\Shop::class, 'user_id');
}
</pre>
//我的费用模型
<pre>
class Expense extends Model
{
protected $fillable = ['date', 'description', 'amount', 'user_id', 'shop_id'];
public function user()
{
return $this->belongsTo(\App\User::class);
}
}
</pre>
//我的商店型号
<pre>
class Shop extends Model
{
protected $fillable = ['name', 'description', 'expense_id', 'shop_id'];
public function user()
{
return $this->belongsTo(\App\User::class, 'user_id');
}
}
</pre>
//费用控制员
<pre>
public function index(Request $request)
{
$expense = Expense::with(['user'])->get();
return ExpenseResource::collection($expense);
// dd(auth()->user());
}
public function create(Request $request)
{
$request->validate([
'date' => 'required',
'description' => 'required',
'amount' => 'required',
]);
$expense = new Expense();
$expense->user_id = auth()->user()->id;
$expense->date = $request->date;
$expense->description = $request->description;
$expense->amount = $request->amount;
$expense->save();
return new ExpenseResource($expense);
}
</pre>
//在我的UserController中
// in My UserController
<pre>
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->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
]);
$user = new user();
$user->user_id = auth()->user()->id;
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->save();
return new UserResource($user);
}
</pre>
有道理吗?
任何想法,谢谢.
如注释中所述,您需要检查当前User
并将返回的Expense
记录限制为仅1)具有User
和2)与当前User
匹配相同的Store
.这可以在单个whereHas()
子句中完成:
As stated in the comments, you'll need to check the current User
and constrain the returned Expense
records to only those that 1) have a User
and 2) match the same Store
as the current User
. This can be done in a single whereHas()
clause:
public function index(Request $request) {
$user = auth()->user(); // If using default `Auth` logic.
$expenses = Expense::whereHas('user', function($subQuery) use($user){
return $subQuery->where('shop_id', '=', $user->shop_id);
})->with(['user'])->get();
return ExpenseResource::collection($expenses);
}
->whereHas()
的作用是约束获取您的Expense
模型的查询以遵守您传递的逻辑,在这种情况下,该查询仅包括具有user
且具有相同shop_id
的Expense
模型.作为当前登录的User
.
What ->whereHas()
does is constrains the query fetching your Expense
models to respect the logic you pass it, which in this case is only include Expense
models that have a user
that has the same shop_id
as the currently logged in User
.
注意:如果当前User
没有Shop
,它可能会返回意外结果,但是您可以保护路由,只允许带有Shop
的User
访问它,等等.
Note:If the current User
does not have a Shop
, it might return unexpected results, but you could protect the route to only allow a User
with a Shop
to access it, etc.