0
votes

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')); 
} 
2
What is the purpose of separating the tables? - user9340246
It is because , the function below returns all the columns. In view I only chooses the specific fields to be displayed. If someone makes a get requests without browser, won't it return the all columns irrespective of user type. public function show(Balancesheet $Balancesheet) { return view('Balancesheets.show',compact('Balancesheet')); } - Rakesh kumar

2 Answers

1
votes

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']);
}
0
votes

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')); 
}