一、分页:
我们从Blade模板调用render方法时,确保使用{!! !!}语法以便HTML链接不被过滤。
有时候就是想得太复杂了,简单的东西操作就行,还老往难的地方钻。
返回上一页就行的,这样简单的js操作。
window.history.go(-1);
二、Laravel Blade模板中的forelse
在Laravel Blade模板中经常在循环输出前先判断一下集合是否有值,然后再 foreach
比如:
@if ($posts->count()) @foreach ($posts as $post) <p>This is post {{ $user->id }}</p> @endforeach @else <p>No posts found.</p> @endif
其实在Laravel Blade中可以使用 forelse
:
@forelse ($posts as $post) <p>This is post {{ $post->id }}</p> @empty <p>No posts found.</p> @endforelse
它的实现可以在 Illuminate/View/Compilers/BladeCompiler.php 找到:
/** * Compile the forelse statements into valid PHP. * * @param string $expression * @return string */ protected function compileForelse($expression) { $empty = '$__empty_'.++$this->forelseCounter; return "<?php {$empty} = true; foreach{$expression}: {$empty} = false; ?>"; }
三、js实现json对象数组的错误弹框提醒
@section('script_link') @if (config('app.debug')) <!-- 使用 https://github.com/VinceG/twitter-bootstrap-wizard --> @endif <script src="/assets/admin/js/jquery.bootstrap.wizard.min.js"></script> <script src="/assets/lib/select2/js/select2.min.js" type="text/javascript"></script> <script src="/assets/admin/js/jquery.form.js" type="text/javascript"></script> <script src="/assets/lib/toastr/toastr.min.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="/assets/lib/toastr/toastr.min.css">
@stop @section('script') <script type="text/javascript"> @if (isset($versions)) var url = "{!! action('Admin\VersionController@edits') !!}"; var message = '编辑成功'; @else var url = "{!! action('Admin\VersionController@store') !!}"; var message = '添加成功'; @endif // wizard on tab next 提交表单 $('#formVersion').ajaxForm({ url: url, type: 'POST', dataType: 'json', async: false, success: function (returnData) { if (returnData.status == 'success') { layer.msg(message, {icon: 1}); setTimeout(function(){ // 返回上一页 window.location.href = "{!! action('Admin\VersionController@index') !!}"; },1000) }else{ var msg = JSON.parse(returnData.message); $.each(msg, function(i) { var info = msg[i]; for (var i = 0; i < info.length; i++) { layer.msg(info[i],{icon: 0}); } }); } } }); </script>