我正在laravel项目中创建配置文件编辑功能。
我想更新个人资料图片。当我上传新图像时,我可以在profile / index.blade.php中看到一个新图像。
但是,当我没有上传图像时,在profile / index.blade.php中看不到任何图像。我看到一个错误
配置文件编辑页面是profile / create.blade.php。然后,当我点击保存按钮时,我可以重定向到个人资料页面profile / index.blade.php。
我想完成的是,
当我上传新图像时,我想在 index.blade.php。如果我不上传图片,我想重定向到 index.blade.php并显示以前的个人资料图片。
我的存储中有个人资料文件夹。我已经完成了php artisan storage:link。
index.blade.php
<div class="person">
@if(empty(Auth::user()->profile->image))
<img src="{{asset('image/image1.jpg')}}" >
@else
<img src="{{ asset('storage/'.Auth::user()->profile->image) }}" >
@endif
</div>
create.blade.php
<div class="content">
<div class="userinfo">
<div class="profile">
</div>
<div class="image-preview" id="imagePreview">
@if(empty(Auth::user()->profile->image))
<img src="{{asset('image/image1.jpg')}}" id="image-preview__image">
@else
<img src="{{ asset('storage/'.Auth::user()->profile->image) }}" id="preview">
@endif
</div>
<form action="{{ route('profile.store') }}" method="POST" enctype="multipart/form-data" name="image">
@csrf
<div class="preview">
<input type="file" id="file" accept="image/*" name="image">
<label for="file">
Change Image
</label>
</div>
<ul class="information">
<li>Gender :<br>
<div class="gender">
<select name="gender" id="" name="gender" >
<option class="option" value="@if(empty(Auth::user()->profile->gender))Select Gender
@else{{ Auth::user()->profile->gender }}@endif" selected="selected">
@if(empty(Auth::user()->profile->gender))Select Gender
@else{{ Auth::user()->profile->gender }}@endif
</option>
<option value="male" >male</option>
<option value="female" class="selected">female</option>
<option value="any">any</option>
</select>
</div>
</li>
<li>Country :<br>
<div class="country">
<select name="country" id="" name="country">
<option class="option" value="@if(empty(Auth::user()->profile->country))Select country
@else{{ Auth::user()->profile->country }}@endif" selected="selected">
@if(empty(Auth::user()->profile->country))Select country
@else{{ Auth::user()->profile->country }}@endif
</option>
<option value="Afghanistan">Afghanistan</option>
<option value="Albania">Albania</option>
<option value="Algeria">Algeria</option>
<option value="American Samoa">American Samoa</option>
</select>
</div>
</li><br>
<li>Birthday :<br>
<input type="text" class="birthday" id="bod" name="bod"
value="@if(!empty(Auth::user()->profile->bod)){{ Auth::user()->profile->bod }}@endif">
</li>
<li> Instagram :<br>
<input type="text" class="instagram" name="instagram"
value="@if(!empty(Auth::user()->profile->instagram)){{ Auth::user()->profile->instagram }}@endif">
</li><br>
<li>User Description :<br>
<textarea name="description" id="" cols="60" rows="10">@if(!empty(Auth::user()->profile->description)){{ Auth::user()->profile->description }}@endif</textarea></li>
<button type="submit" class="saveBtn">Save</button>
</ul>
</div>
UserController.php
<?php
namespace App\Http\Controllers;
use App\Http\Requests\Profile\CreateProfileRequest;
use Illuminate\Http\Request;
use App\Profile;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
class UserController extends Controller
{
public function __construct()
{
$this->middleware('user');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$posts = Auth::user()->favourites;
return view('profile.index')->with('profile', Profile::all())->with('posts', $posts);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('profile.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request, Profile $profile)
{
$user_id = auth()->user()->id;
$data = $request->only(['image']);
if($request->hasFile('image')){
//upload it
$image = $request->image->store('profile');
//delete old image
Storage::delete($profile->image);
} else {
$image = $profile->image;
}
Profile::updateOrCreate(
['user_id' => $user_id],
[
'image' => $image,
'gender' => request('gender'),
'country' => request('country'),
'bod' => request('bod'),
'instagram' => request('instagram'),
'description' => request('description'),
]);
//flash message
//redirect user
return redirect()->route('profile.index');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Profile $profile)
{
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
}
}
答案 0 :(得分:1)
如果没有上传图片,请从配置文件中获取图片
这里的问题是,在$profile
方法中注入的store()
不是正确的方法,请直接从用户加载它。
public function store(Request $request, $id = null)
{
$profile = auth()->user()->profile;
if($request->hasFile('image')){
//upload it
$image = $request->image->store('profile');
//delete old image
if ($profile) {
Storage::delete($profile->image);
}
} else {
$image = $profile->image??null;
}
$profile->update(
[
'image' => $image,
'gender' => request('gender'),
'country' => request('country'),
'bod' => request('bod'),
'instagram' => request('instagram'),
'description' => request('description'),
]);
//flash message
//redirect user
return redirect()->route('profile.index');
}