我正在用 vue.js 和 axios 在 laravel 中创建 CRUD。我确实创建了完整的 CRUD,但是使用从我的 Controller 返回数据的路由,但是如果我放置返回数据的路由,显然所有人都可以访问返回 Controller 的数据。
我正在使用 laravel 5.5、vue.js 和 axios。如果我将路由放入浏览器中,例如: localhost/tasks 我在 Controller 中传递的所有数据都会显示。
路线:
Route::get('/', function () {
return view('dashboard');
});
Route::resource('tasks', 'TaskController',['except' => 'show', 'create', 'edit']);
Controller :
public function index()
{
$tasks = Task::orderBy('id', 'DESC')->get();
return $tasks;
}
app.js
data: {
keeps: [],
newKeep: '',
fillKeep: {'id': '', 'keep': ''},
errors: []
},
methods: {
getKeeps: function()
{
//este es el nombre de las rutas (index)
var urlKeeps = 'tasks';
axios.get(urlKeeps).then(response =>{
this.keeps = response.data
});
},
// Metodo de editar
editKeep: function(keep){
this.fillKeep.id = keep.id;
this.fillKeep.keep = keep.keep;
$('#edit').modal('show');
},
主要仪表板位于“/” route , Controller 从我的 Controller 返回数据,路线是“/tasks”和其他方法,但是当任何其他用户在浏览器中输入“www.myweb.com”时/tasks”它返回来 self 的 Controller 的所有数据,如何避免这个问题?
最佳答案
如果任务模型具有多个用户的数据,您应该限制 tasks
路由以仅返回当前登录用户的数据。无论是通过 Ajax 访问还是直接在浏览器窗口中访问,都是一样的。
使用 auth
中间件限制该路由和任务 Eloquent 查询的范围以返回该用户的数据。
例如
$tasks = Task::where('user_id', \Auth::user()->id)->orderBy('id', 'DESC')->get();
那么用户是否直接访问该 URL 并看到自己的数据并不重要。
关于mysql - 如何隐藏 Controller 返回的数据,在 Laravel 和 vue.js 中进行 CRUD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56879083/