My search for the topic Laravel 5.5 -- Separate table for authenticated users and guest users has resulted in separate URL for authenticated and guest users. However, I have a table of which, few fields are for all users and remaining are for only authenticated users. Should I split the table?
I want to split the table because the function below returns all the columns. In the view, I only choose the specific fields to be displayed. If someone makes a GET request without a browser, won't it return all columns irrespective of user type.
public function show(Balancesheet $Balancesheet) {
return view('Balancesheets.show', compact('Balancesheet'));
}
If you want to get a specific column, you can use 'pluck()' method, which allows you to select a specific column. If your Laravel version older then 5.3 rename pluck as lists
:
if(\Auth::user()) {
User::pluck('user_column')->all();
} else {
User::pluck('guest_column')->all();
}
If you need to more than one column you can choose them within get()
method:
if(\Auth::user()) {
query->get(['guest','columns']);
} else {
query->get(['user','columns']);
}
I found the following code more useful .
public function show(Balancesheet $Balancesheet) {
if(\Auth::user()) {
$Balancesheet = Balancesheet->take(10)->select('name', 'slug')->get();
} else {
$Balancesheet = Balancesheet->select('cashflow', 'district')->take(10)->select('name', 'slug')->get();
}
return view('Balancesheets.show',compact('Balancesheet'));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.