已解决:如何在 laravel 中获取和显示图像?
SOLVED: How to fetch and show image in laravel?
希望你一切都好。我正在 laravel 中的一个项目中工作,我必须在 MYSql 数据库中存储用户徽标并在用户需要时检索。我成功地将徽标路径存储在数据库中,现在我想在视图中显示它,但不幸的是我不能。徽标存储在 'storage/app/public/' 中。我尝试了资产功能以及我在 google 和 Whosebug 上找到的所有可能方法,但没有成功。
要存储的控制器函数
public function store(Request $request)
{
$path = $request->file('logo')->store('public');
$company = new company;
$company->name = $request->input('name');
$company->email = $request->input('email');
$company->website = $request->input('website');
$company->logo = $path;
$company->save();
$request->session()->flash('status', 'New Record added successfully');
return redirect('company');
}
查看
@foreach($data as $company)
<tr>
<th scope="row">{{ $company->name }}</th>
<td>{{ $company->email }}</td>
<td>
<img src="{{ asset($company->logo) }}" alt="" style="width:100px; height:100px;">
</td>
<td> {{ $company->website }}</td>
<td>
<a href="{{ URL::to('company/'. $company->id. '/edit') }}">Edit</a>
<a href="{{ URL::to('company/'. $company->id. '/destroy') }}">Delete</a>
</td>
</tr>
@endforeach
我哪里错了???任何帮助将不胜感激:)
请试试这个,如果有效请告诉我。
控制器
public function store(Request $request)
{
// Image upload
if($request->hasFile('logo')){
$image = $request->file('logo');
$extension = $image->getClientOriginalExtension();
$fileName = $image->getFilename().'_'.date('d-m-Y_h-i-s').'.'.$extension;
Storage::disk('public')->put($fileName, File::get($image));
}else{
$fileName = null;
}
$company = new company;
$company->name = $request->input('name');
$company->email = $request->input('email');
$company->website = $request->input('website');
$company->logo = $fileName;
$company->save();
$request->session()->flash('status', 'New Record added successfully');
return redirect('company');
}
Blade
@foreach($data as $company)
<tr>
<th scope="row">
{{ $company->name }}
</th>
<td>
{{ $company->email }}
</td>
<td>
@if($company->logo !== null)
<img src="{{ url('storage/'.$company->logo) }}" alt="" style="width:100px; height:100px;">
@else
<img src="{{ asset('path_to_url_default_logo_image') }}" alt="" style="width:100px; height:100px;">
@endif
</td>
<td>
{{ $company->website }}
</td>
<td>
<a href="{{ URL::to('company/'. $company->id. '/edit') }}">Edit</a>
<a href="{{ URL::to('company/'. $company->id. '/destroy') }}">Delete</a>
</td>
</tr>
@endforeach
希望你一切都好。我正在 laravel 中的一个项目中工作,我必须在 MYSql 数据库中存储用户徽标并在用户需要时检索。我成功地将徽标路径存储在数据库中,现在我想在视图中显示它,但不幸的是我不能。徽标存储在 'storage/app/public/' 中。我尝试了资产功能以及我在 google 和 Whosebug 上找到的所有可能方法,但没有成功。
要存储的控制器函数
public function store(Request $request)
{
$path = $request->file('logo')->store('public');
$company = new company;
$company->name = $request->input('name');
$company->email = $request->input('email');
$company->website = $request->input('website');
$company->logo = $path;
$company->save();
$request->session()->flash('status', 'New Record added successfully');
return redirect('company');
}
查看
@foreach($data as $company)
<tr>
<th scope="row">{{ $company->name }}</th>
<td>{{ $company->email }}</td>
<td>
<img src="{{ asset($company->logo) }}" alt="" style="width:100px; height:100px;">
</td>
<td> {{ $company->website }}</td>
<td>
<a href="{{ URL::to('company/'. $company->id. '/edit') }}">Edit</a>
<a href="{{ URL::to('company/'. $company->id. '/destroy') }}">Delete</a>
</td>
</tr>
@endforeach
我哪里错了???任何帮助将不胜感激:)
请试试这个,如果有效请告诉我。
控制器
public function store(Request $request)
{
// Image upload
if($request->hasFile('logo')){
$image = $request->file('logo');
$extension = $image->getClientOriginalExtension();
$fileName = $image->getFilename().'_'.date('d-m-Y_h-i-s').'.'.$extension;
Storage::disk('public')->put($fileName, File::get($image));
}else{
$fileName = null;
}
$company = new company;
$company->name = $request->input('name');
$company->email = $request->input('email');
$company->website = $request->input('website');
$company->logo = $fileName;
$company->save();
$request->session()->flash('status', 'New Record added successfully');
return redirect('company');
}
Blade
@foreach($data as $company)
<tr>
<th scope="row">
{{ $company->name }}
</th>
<td>
{{ $company->email }}
</td>
<td>
@if($company->logo !== null)
<img src="{{ url('storage/'.$company->logo) }}" alt="" style="width:100px; height:100px;">
@else
<img src="{{ asset('path_to_url_default_logo_image') }}" alt="" style="width:100px; height:100px;">
@endif
</td>
<td>
{{ $company->website }}
</td>
<td>
<a href="{{ URL::to('company/'. $company->id. '/edit') }}">Edit</a>
<a href="{{ URL::to('company/'. $company->id. '/destroy') }}">Delete</a>
</td>
</tr>
@endforeach