I am trying to update the file path to the database using Laravel Storage facades. But the cover_photo field is not updating. the avater field is updating correctly.
When a user is created using the factory method I set the default path for both the avater and cover_photo field. This is the code for the factory section
$factory->define(App\User::class, function (Faker $faker) {
static $password;
$rndNum = rand(0,1);
return [
'name' => $faker->name,
'slug' => str_slug($faker->name),
'gender' => rand(0,1),
'avater' => $rndNum == 0 ?'public/defaults/avaters/female.png':'public/defaults/avaters/male.png',
'cover_photo' => 'public/defaults/avaters/cover.jpg',
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
});
$factory->define(App\Profile::class, function ($faker) {
return [
'location' => null,
'about' => null,
'user_id' => function () {
return factory(App\User::class)->create()->id;
}
];
});
In the database seeder i use this code to run the factory
public function run()
{
factory(\App\Profile::class,6)->create();
}
Now in the profileController section when i try to update the database fields by this code
public function update(Request $request, $id)
{
$this->validate($request,[
'location' => 'required|max:55',
'about' => 'required|max:1024',
]);
$profile = Profile::where('user_id',$id)->with('user')->first();
if ($request->hasFile('avater')){
$profile->user->update([
'avater' => $request->avater->store('public/users/avaters/'),
]);
}
if($request->hasFile('cover_photo')){
$profile->user->update([
'cover_photo' => $request->cover_photo->store('public/users/avaters/'),
]);
}
$profile->update([
'location' => $request->location,
'about' => $request->about,
]);
return $profile;
$notification = array(
'message' => 'Profile Updated!',
'alert-type' => 'success'
);
return redirect()->back()->with($notification);
}
And then i used this code to view the image in the blade file
<img src="" class="img-responsive" alt="">
Problem is that the path to the cover_photo in the database is not updating. What can I do? database image
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2AyJZf6
via IFTTT
Aucun commentaire:
Enregistrer un commentaire