Skip to content

Commit ee67f29

Browse files
committed
Added: Now we can create users and save there photos in DB
1 parent c3ec43c commit ee67f29

File tree

9 files changed

+129
-92
lines changed

9 files changed

+129
-92
lines changed

.idea/workspace.xml

Lines changed: 104 additions & 89 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/Http/Controllers/AdminController.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Photo;
56
use App\Role;
67
use App\User;
78
use Illuminate\Http\Request;
@@ -99,14 +100,30 @@ public function createUser(){
99100
public function storeUser(Requests\UsersRequest $request){
100101

101102
$user = new User();
103+
$file = $request->file('photo');
102104

103105
$user->name = $request->name;
104106
$user->email = $request->email;
105107
$user->role_id = $request->role_id;
106-
$user->password = Hash::make($request->password);
108+
$user->password = bcrypt($request->password);
107109
$user->created_at = $request->created_at;
108110
$user->updated_at = $request->updated_at;
109111

112+
if($file){
113+
$name = time().$file->getClientOriginalName();
114+
$date = new \DateTime();
115+
116+
$photo = new Photo();
117+
$photo->path = $name;
118+
$photo->created_at = $date->getTimestamp();
119+
$photo->updated_at = $date->getTimestamp();
120+
$photo->save();
121+
$user->photo_id = $photo->id;
122+
123+
$file->move('image', $name);
124+
125+
}
126+
110127
$user->save();
111128

112129
return redirect('admin/users');

app/Photo.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66

77
class Photo extends Model
88
{
9-
//
9+
public function user(){
10+
return $this->hasOne('App\User','photo_id','id');
11+
}
1012
}

app/User.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,7 @@ public function roles(){
2929
return $this->belongsTo('App\Role', 'role_id', 'id');
3030
}
3131

32-
32+
public function photo(){
33+
return $this->belongsTo('App\Photo', 'photo_id', 'id');
34+
}
3335
}

database/migrations/2016_08_01_140409_create_photos_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function up()
1414
{
1515
Schema::create('photos', function (Blueprint $table) {
1616
$table->increments('id');
17+
$table->string('path');
1718
$table->timestamps();
1819
});
1920
}
43.3 KB
Loading
43.3 KB
Loading
43.3 KB
Loading
43.3 KB
Loading

0 commit comments

Comments
 (0)