我创建了一个GlobalComposer类来获取所有视图中的用户名:
<?php namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
class GlobalComposer {
/**
* Bind data to the view.
*
* @param View $view
* @return void
*/
public function compose(View $view)
{
$view->with('username', Auth::user());
}
}
?>
我想从数据库中获取用户名,以便根据用户名保存用户的图片,但问题是我获得了一个用id,用户名,姓名,电子邮件,创建日期创建的文件夹... 。所有列...如何专门获取上述类的用户名?
文件夹创建:
public function create(array $data)
{
File::MakeDirectory('/home/vagrant/Code/laravel/UserFolders/' . $data['username']);
File::makeDirectory("/home/vagrant/Code/laravel/UserFolders/{$data['username']}/fileuploads",
$mode = 0755, $recursive = true, $force = false);
File::makeDirectory("/home/vagrant/Code/laravel/UserFolders/{$data['username']}/photos",
$mode = 0755, $recursive = true, $force = false);
return User::create([
'name' => $data['name'],
'username'=>$data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
谢谢
答案 0 :(得分:0)
如果您的用户表中存在用户名,请执行此操作。
public function compose(View $view){
$view->with('username', Auth::user()->username);
}
至于此,你所做错的是使用recursive,它会创建路径中指定的所有文件夹(如果它们不存在),
其次我不喜欢在Laravel应用程序之外创建此文件夹的想法/home/vagrant/Code/laravel/UserFolders/
,为什么不在storage
中创建一个受外界保护的文件夹
public function create(array $data)
{
File::makeDirectory("/home/vagrant/Code/laravel/UserFolders/{$data['username']}/fileuploads", $mode = 0755, $recursive = true, $force = false);
if(File::exists("/home/vagrant/Code/laravel/UserFolders/{$data['username']}/fileuploads")){
File::makeDirectory("/home/vagrant/Code/laravel/UserFolders/{$data['username']}/photos", $mode = 0755);
}
return User::create([
'name' => $data['name'],
'username'=>$data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}