Laravel 如何将 HTML 保存到数据库然后检索它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50425305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel how to save HTML to database and then retrieve it
提问by Tiago Filipe
This question was already asked, however, I still didn't get anything working properly.
这个问题已经被问到了,但是,我仍然没有得到任何正常工作。
I am trying to create a functionality in my Admin Panel on my Laravel App that allow user to paste ad codes that then will be rendered in specific parts of the laravel app.
我试图在我的 Laravel 应用程序的管理面板中创建一个功能,允许用户粘贴广告代码,然后将在 Laravel 应用程序的特定部分呈现。
I already have my view:
我已经有了我的看法:
<form action="{{ route('update.ads', $ads->id) }}" method="POST" class="form-horizontal">
{{ csrf_field() }}
<div class="form-group">
<label class="control-label col-sm-12" >Ad1 (Interstitial or popup on Homepage)</label>
<div class="col-sm-10">
<input type="text" name="ad1" id="ad1" class="form-control" value="{{ $ads->ad1 }}">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-12" >Ad2 (Video Page - Banner Desktop)</label>
<div class="col-sm-10">
<input type="text" name="ad2" id="ad2" class="form-control" value="{{ $ads->ad2 }}">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-12" >Ad3 (Video Page - Banner Mobile)</label>
<div class="col-sm-10">
<input type="text" name="ad3" id="ad3" class="form-control" value="{{ $ads->ad3 }}">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-info" value="Update" />
</div>
</div>
</form>
I have my controller:
我有我的控制器:
public function updateAds($id, Request $request){
$this->validate($request, [
'ad1',
'ad2',
'ad3'
]);
$adsData = $request->only(["ad1","ad2","ad3"]);
$adsData['ad1'] = htmlentities($adsData['ad1']);
$adsData['ad2'] = htmlentities($adsData['ad2']);
$adsData['ad3'] = htmlentities($adsData['ad3']);
Ad::find($id)->update($adsData);
Session::flash('success_msg', 'Ads updated successfully!');
return redirect()->route('admin.ads');
}
Data is saved, but then when i try to retrieve it on my pages, no matter what code i write, the content will always be text. It is never rendered as code.
数据已保存,但是当我尝试在我的页面上检索它时,无论我编写什么代码,内容都将始终是文本。它永远不会呈现为代码。
Here is what i have tried in terms of output:
这是我在输出方面的尝试:
{{ addslashes(htmlspecialchars_decode($ads->ad1)) }}
{{ htmlspecialchars_decode($ads->ad1) }}
Thank you all for your help! Regards, Tiago
谢谢大家的帮助!问候, 蒂亚戈
回答by wunch
The {{ }}
is the "safe" blade echo; it actually re-encodes things with htmlspecialchars automatically before outputting the data.
该{{ }}
是“安全的”刀片回声; 它实际上在输出数据之前自动使用 htmlspecialchars 重新编码内容。
Try {!! html_entity_decode($ads->ad1) !!}
or something similar. I'm not sure how it's getting stored, so you might have to do different decoding, but the key thing here is to use {!! !!}
, which displays the raw, unescaped data.
尝试{!! html_entity_decode($ads->ad1) !!}
或类似的东西。我不确定它是如何存储的,所以你可能需要做不同的解码,但这里的关键是使用{!! !!}
,它显示原始的、未转义的数据。