To display validation errors after input field I using:
<div class="form-group">
{!! Html::decode(Form::label('first_name','First Name:<span class="required">*</span>',['class'=>'control-label col-sm-3'])) !!}
<div class="col-sm-6">
{!! Form::text('first_name',null,['class'=>'form-control']) !!}
#if ($errors->has('first_name'))
<span class="help-block">
<strong>{{ $errors->first('first_name') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
{!! Html::decode(Form::label('last_name','Last Name:<span class="required">*</span>',['class'=>'control-label col-sm-3'])) !!}
<div class="col-sm-6">
{!! Form::text('last_name',null,['class'=>'form-control']) !!}
#if ($errors->has('last_name'))
<span class="help-block">
<strong>{{ $errors->first('last_name') }}</strong>
</span>
#endif
</div>
</div>
// and so on......
This code works perfectly. But I have to write almost same code in every single input box. So, I planned to make a global function to display errors. To achieve this I did the following.
Create a helpers.php inside app folder
Write the following code:
function isError($name){
if($errors->has($name)){
return '<span class="help-block"><strong>'.$errors->first($name).'</strong></span>';
}
}
run composer dump-autoload
Used it in blade file this way:
<div class="form-group">
{!! Html::decode(Form::label('first_name','First Name:<span class="required">*</span>',['class'=>'control-label col-sm-3'])) !!}
<div class="col-sm-6">
{!! Form::text('first_name',null,['class'=>'form-control']) !!}
{{ isError('first_name') }}
</div>
</div>
<div class="form-group">
{!! Html::decode(Form::label('last_name','Last Name:<span class="required">*</span>',['class'=>'control-label col-sm-3'])) !!}
<div class="col-sm-6">
{!! Form::text('last_name',null,['class'=>'form-control']) !!}
{{ isError('last_name') }}
</div>
</div>
Now, when I go to create.blade.php I have an error
Undefined variable: errors (View: D:\xampp\htdocs\hms\resources\views\guest\create.blade.php)
I know the problem is in helpers.php because I didn't defined the $errors, I just paste that code from blade file.
The problem is that the $errors variable is undefined within the scope of your helper method.
This can be easily solved by passing the $errors object to the isError() helper method.
Helper
function isError($errors, $name){
if($errors->has($name)){
return '<span class="help-block"><strong>'.$errors->first($name).'</strong></span>';
}
}
Blade Template
{!! isError($errors, 'first_name') !!}
Related
Why it gives an error of undefined id, i just changed Route::get to route::post and it says that id is undefined... should i change the way i pass it? is it a correct way of inserting data into database ?
here's the route
Route::post('messenger/store/{id}','MessengerController#store')->name('messenger.store');
View...
{!! Form::open(['method'=>'POST','action'=>['MessengerController#store',$id]]) !!}
<div class="form-group">
{!! Form::text('msg',null,['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Send Message',['class'=>'btn btn-primary'])!!}
</div>
{!! Form::close() !!}
index controller from where i pass ID
public function index($id)
{
//
$user=Auth::user();
return view('messenger.index',compact('user','id'));
}
{!! Form::open(['method'=>'POST','action'=>['MessengerController#store',$id]]) !!}
<div class="form-group">
{!! Form::text('msg',null,['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Send Message',['class'=>'btn btn-primary'])!!}
</div>
{!! Form::close() !!}
change this.
{!! Form::open(['method'=>'POST','url'=>route('messenger.store',[$id])]) !!}
<div class="form-group">
{!! Form::text('msg',null,['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Send Message',['class'=>'btn btn-primary'])!!}
</div>
{!! Form::close() !!}
to this.
Using action is kind of deprecated.
I'm new to Laravel and using ver 5.5. I have a simple view and when I run the code, instead of showing the rendered version, I see the Blade commands.
This is the view code:
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>{{ $title }}</h2>
</div>
<div class="pull-left">
{!! \App\Combine\BaseCombine::tableHeader($collection) !!}
{!! \App\Combine\BaseCombine::tableData($collection) !!}
{!! \App\Combine\BaseCombine::tableFooter($collection) !!}
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('transaction.index') }}"> Back</a>
</div>
</div>
</div>
#endsection
Here is the code in the controller that executes the view:
return view('general.genericTable',
[
'title' => __FUNCTION__,
'transactionID' => $transactionsID,
'type' => $type,
'collection' => TransactionCombine::agents($transactionsID, $type),
]
);
This is what I see when I execute the code:
#section('content')
{{ $title }}
{!! \App\Combine\BaseCombine::tableHeader($collection) !!} {!! \App\Combine\BaseCombine::tableData($collection) !!} {!! \App\Combine\BaseCombine::tableFooter($collection) !!}
Back
#endsection
What have I done wrong?
I am using voyager backoffice with laravel, and i'm having a problem with the blade coming from the database, all the code works except the blade, all the blade code outside the database works well.
I've already used {{ }}, {!! !!}, {{{ }}}, Html_entity_decode () but nothing works.
Any help is appreciated.
Thank you
View:
#extends ('layout')
#section ('content')
#foreach ($pageContent as $page)
{!! $page->slug !!}
{!! $page->title !!}
{!! $page->body !!}
#endforeach
#endsection
body that came from db:
<div class="container contacts_content_container">
<div class="row">
<div class="col-sm-12 text-center">
<div class="content">
<h1>Contact US Form</h1>
#if(Session::has('success'))
<div class="alert alert-success">
{{ Session::get('success') }}
</div>
#endif
{!! Form::open(['route'=>'contactus.store']) !!}
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
{!! Form::label('Name:') !!}
{!! Form::text('name', old('name'), ['class'=>'form-control', 'placeholder'=>'Enter Name']) !!}
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
{!! Form::label('Email:') !!}
{!! Form::text('email', old('email'), ['class'=>'form-control', 'placeholder'=>'Enter Email']) !!}
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
<div class="form-group {{ $errors->has('message') ? 'has-error' : '' }}">
{!! Form::label('Message:') !!}
{!! Form::textarea('message', old('message'), ['class'=>'form-control', 'placeholder'=>'Enter Message']) !!}
<span class="text-danger">{{ $errors->first('message') }}</span>
</div>
<div class="form-group">
<button class="btn btn-success">Contact US!</button>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
Controller:
<?php
namespace App\Http\Controllers;
use App\Page;
use Illuminate\Routing\Controller;
class PageController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
// get all the pages
$pages = Page::all();
// load the view and pass the pages
return view('public.about')
->with('pages', $pages);
}
public function slug($slug)
{
// get page where slug
$pageContent = Page::where('slug', $slug)->get();
// load the view and pass the page content
return view('public.' . $slug)
->with('pageContent', $pageContent);
}
}
Routes:
//Pages Routing With slug
Route::get('/{slug}','PageController#slug');
I'd say keeping templates in a DB is not a good idea, but if you really need it you can parse Blade template manually. There are multiple ways to do that and one of these is using compileString() method:
$html = Blade::compileString($page->template);
May be compileString() might help you.Try this
Blade::compileString('Your blade syntax from db {!! $variable !!}');
Question like this are asked, i have searched a lot but found nothing which works for me.
Here is Controller function:
public function showHomePage() {
$communities = Community::all();
$ideas = Idea::paginate(8);
return view('publicPages.index', compact(['communities', 'ideas']));
}
Now the paginate method is working but when in view i call the method stated in documentation Docs
{!! $idea->render() !!}
It generates following error
Call to undefined method Illuminate\Database\Query\Builder::render()
I have also tried {!! $idea->render() !!} but the issue is same. Tried this also $ideas = DB::table('ideas')->paginate(8);
Here is view :
#foreach($ideas as $idea)
<div class="lst-popular-project clearfix">
<div class="grid_3">
<div class="project-short sml-thumb">
<div class="top-project-info">
<div class="content-info-short clearfix">
<a href="{{URL::to('showidea', array($idea->id))}}" class="thumb-img">
<img src="{{asset($idea->idea_image)}}" alt="$TITLE">
</a>
<div class="wrap-short-detail">
<h3 class="rs acticle-title">
<a class="be-fc-orange" href="project">
{{$idea->idea_title}}
</a>
</h3>
<p class="rs tiny-desc">
by
<a href="profile.html" class="fw-b fc-gray be-fc-orange">
{{$idea->User->name}}
</a>
</p>
<p class="rs title-description">
{{$idea->idea_info}}
</p>
<p class="rs project-location">
<i class="icon iLocation"></i>
{{$idea->idea_location}}
</p>
</div>
</div>
</div>
</div>
</div>
#endforeach
you must replace
{!! $idea->render() !!}
with
{!! $ideas->render() !!}
$ideas variable contains from,total,page etc
I guess it shoule be $ideas:
{!! $ideas->render() !!}
I am using following route
Route::get('admin/new/password','PostController#newPasswordForm');
Route::post('admin/new/password','PostController#newPasswordStore');
and function is like this
public function newPasswordForm() {
return view('pages.posts.password');
}
and in view file this is the code
#section('content')
<div class="container bg-white">
<div class="row padding-top-80 padding-bottom-40">
<div class="col-md-12">
{!! Form::open(['url' => 'newpassword','method'=>'POST']) !!}
<div class="col-md-8">
<h2>Enter Your Password</h2>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password') !!}
</div>
<div class="form-group">
{!! Form::label('confirmpassword', 'Confirm Password') !!}
{!! Form::password('password_confirmation') !!}
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
{!! Form::submit('Save', ['class' => 'btn btn-primary btn-large']) !!}
</div>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
#endsection
but I am getting following error.
ErrorException in FormBuilder.php line 11:
Undefined offset: 0 (View: ..\resources\views\pages\posts\password.blade.php)
I cannot understand what I am doing wrong. Please help me out.TIA.