I'm currently working on a Laravel project that requires a home owner to log in to the website. Then, he/she is required to fill a form on guest details. When he/she submits the form, it will be displayed in a list. Now, when a home owner submits a form, every other home owner can see it too. How can I make it so that only the logged in home owner able to see his/her list of guests? Is this possible to do?
My GuestController is as follows:
class GuestController extends Controller
{
public function index()
{
//returns admin's view
$guest = Guest::all();
return view('pages.guest.index', compact('guest'));
}
public function show()
{
//returns a home owner's view
$guest = Guest::all();
return view('pages.guest.show', compact('guest'));
}
public function create()
{
return view('pages.guest.create');
}
public function store(Request $request)
{
$guest = new Guest;
$guest->code = random_int(100000, 999999);
$guest->hash = hash('sha256', $guest['code']);
$guest->owner = Auth::user()->name;
$guest->unit = Auth::user()->unit;
$guest->guestname = $request->input('guestname');
$guest->guestphone = $request->input('guestphone');
$guest->guestic = $request->input('guestic');
$guest->guestcar = $request->input('guestcar');
$guest->datevisit = $request->input('datevisit');
$guest->timevisit = $request->input('timevisit');
$guest->save();
return redirect('show-pass')->with('status', 'Guest Added Successfully');
}
CodePudding user response:
At the database level, make the owner a foreign key on the guests table. I am guessing that the owners belong to users table, so you will have something like this in your migration file
//...
Schema::create('guests', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained();
$table->bigUnsignedInteger('code');
//...
});
So your store method will be as
public function store(Request $request)
{
$guest = new Guest;
$guest->code = random_int(100000, 999999);
$guest->user_id = Auth::id();
//...
return redirect('show-pass')->with('status', 'Guest Added Successfully');
}
And then your index() method will be
public function index()
{
//returns admin's view
$guest = Guest::where('user_id', Auth::id())->get();
return view('pages.guest.index', compact('guest'));
}
PS: you should define relationships in the respective User and Guest models to make things even smoother.
CodePudding user response:
You can try something like this. I am expecting this function will call when a user other than admin login and want to see list of his guests. Please check documentation as well.
public function show()
{
//returns a home owner's view
$guest = Guest::where('owner',Auth::user()->name)->get();
return view('pages.guest.show', compact('guest'));
}
CodePudding user response:
Whenever you store a form, make sure you store the logged user's ID too and not just the name. You may encounter problems in the future if there are multiple users with the same name.
For your guests database, you should create a column with users id as foreign key. Once you're able to do that and you name the column as owner_id for example, add this to your store function
$guest->owner_id = Auth::user()->id;
You can now do this with your show function
public function show()
{
//returns a home owner's view
$guest = Guest::where('owner_id', '=', Auth::user()->id)->get();
return view('pages.guest.show', compact('guest'));
}
