Blade from Database not working properly - laravel

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 !!}');

Related

Pass data from form submit to controller via get Method

can anyone tell, what is wrong with the following way to pass the data from view->route->controller. Currently I get Missing required parameters for [Route: show.exclusion] [URI: exclusion/create/{id}]
This is my view part:
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">In welcher Gruppe möchten Sie User einladen?</div>
<div class="panel-body">
{!! Form::open(array('route'=>'show.exclusion', 'method'=>'get', 'id'=>'$group->idgroup')) !!}
<div class="form-group">
{{Form::label('choosegroup', 'Wähle eine Gruppe')}}
<select class="form-control m-bot15" name="idgroup">
#foreach($groups as $group)
<option id="{{ $group->idgroup }}">{{ $group->groupname }}</option>
#endforeach
</select>
<!--{{ csrf_field() }}-->
</div>
<div>
{{Form::submit('Search',['class' => 'btn btn-primary'])}}
<a class="btn btn-default btn-close" href="{{ route('home') }}">Cancel</a>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
This is my route...
Route::get('exclusion/create/{id}', 'ExclusionController#show')->name('show.exclusion');
And here you can see my controller function...
public function show($id)
{
$groupid = $id;
$members = V_Exclusion::where([['idgroup', $groupid],['groupadmin', Auth::id()]])->get();
$groups = V_Exclusion::where('groupadmin', Auth::id())->get();
return view('exclusions.createexclusion')
->with('members', $members)
->with('groups', $groups);
}
Try this.
In Route
Route::get('exclusion/create/{id}', 'ExclusionController#show');
{!! Form::open(array('route'=>['exclusion/create',$group->idgroup], 'method'=>'get', 'id'=>'$group->idgroup')) !!}

Several updates on the same view Laravel

In a setup process I want to:
select langs of the application (1 or more) ...
Update the DB
Select the default language
Update again the DB...
For this i created 3 routes.
Route::get('/home/setup', 'BackOffice\FirstconnectionController#initLang');
Route::patch('/home/setup', 'BackOffice\FirstconnectionController#initLangUpdate')->name('setup.setLang');
Route::patch('/home/setup', 'BackOffice\FirstconnectionController#setDefaultLang')->name('setup.setDefaultLang');
The first is the home page where i make eloquent requests
The second route display the list of languages
The third route displays the list of languages which are published ...
Here is my view :
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
{{-- IF NO LANGS ARE PUBLISHED I CAN CHOOSE HERE --}}
#if ($langsCount == 0)
{!! Form::model($langs, [
'method' => 'PATCH',
'route' => 'setup.setLang'
])
!!}
#foreach($langs as $lang)
<div class="form-group">
{{--<label class="col-md-4"> {{ $lang->langname }} </label>--}}
{{--<input id="{{ $lang->langisocode }}" type="checkbox">--}}
{!! Form::label($lang->langname, $lang->langname ) !!}
{!! Form::checkbox( 'lang[]', $lang->id ) !!}
</div>
#endforeach
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Valider</button>
</div>
{!! Form::close() !!}
{{-- NOW I SELECT DEFAULT LANGUAGE... --}}
#else
{!! Form::model($langs, [
'method' => 'PATCH',
'route' => 'setup.setDefaultLang'
])
!!}
#foreach($pubLangs as $pubLang)
{!! Form::label($pubLang->langname, $pubLang->langname ) !!}
{!! Form::radio( 'lang', $pubLang->id ) !!}
<br>
#endforeach
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Valider</button>
</div>
{!! Form::close() !!}
#endif
Here is my controller :
// I display the info here
public function initLang()
{
$langs = Lang::onlyTrashed()->get();
$langsCount = Lang::count();
$pubLangs = Lang::all();
return view('admin.firstConnection', compact('langs', 'langsCount', 'pubLangs'));
}
public function initLangUpdate(Request $request) {
$request = $request->input('lang');
foreach ($request as $entry) {
Lang::withTrashed()->find($entry)->restore();
}
return redirect('admin/home/setup')->with('success', 'OK');
}
public function setDefaultLang(Request $request) {
$request = $request->input('lang');
return $request;
}
I will update the setDefaultLang after ...
I have this error message :
Route [setup.setLang] not defined

My view displays raw blade code

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?

Comparing dates in view and applying class

I am currently displaying projects within a view using a foreach loop.
#foreach ($projects as $project)
<div class="col-xs-4 col-sm-4 col-md-4 col-lg-4">
{!! Form::open(array('class' => 'form-inline delete', 'method' => 'DELETE', 'route' => array('projects.destroy', $project->id))) !!}
<div class="panel panel-default">
<div class="panel-heading projectPanelHeading">
<h4>{{ $project->projectName }}</h4>
</div>
<div class="panel-body projectPanel">
<p>Deployment Date: {{ ($project->deploymentDate ? date('d-m-Y', strtotime($project->deploymentDate)) : '') }}</p>
<p>Status: {{ $project->status or '' }}</p>
</div>
</div>
{!! Form::close() !!}
</div>
#endforeach
As you can see, each project has a $project->deploymentDate which I convert to a date object with the format d-m-Y.
What I am trying to do is compare the deploymentDate with the current date, and if the deploymentDate has passed, to apply a panel-red class to the panel.
I was thinking about doing something like this
{!! Form::open(array('class' => 'form-inline delete', 'method' => 'DELETE', 'route' => array('projects.destroy', $project->id))) !!}
#if(date('d-m-Y', strtotime($project>deploymentDate)) < date('d-m-Y'))
<div class="panel panel-default panel-red">
<div class="panel-heading projectPanelHeading">
<h4>{{ $project->projectName }}</h4>
</div>
<div class="panel-body projectPanel">
<p>Deployment Date: {{ ($project->deploymentDate ? date('d-m-Y', strtotime($project->deploymentDate)) : '') }}</p>
<p>Status: {{ $project->status or '' }}</p>
</div>
</div>
#else
<div class="panel panel-default">
<div class="panel-heading projectPanelHeading">
<h4>{{ $project->projectName }}</h4>
</div>
<div class="panel-body projectPanel">
<p>Deployment Date: {{ ($project->deploymentDate ? date('d-m-Y', strtotime($project->deploymentDate)) : '') }}</p>
<p>Status: {{ $project->status or '' }}</p>
</div>
</div>
#endif
{!! Form::close() !!}
What I do not like about this approach is the repetition of the panel block. Additionally, there are a lot of mistakes with panel-red being applied to projects who's deploymentDate has not yet passed, and vice versa.
Is there a better way to achieve what I am after which is both correct, and does not require repetition?
Thanks
First of all, add the deploymentDate attribute to the $dates array in your Project model. This will ensure you have Carbon instances for your deploymentDate.
Project.php
protected $dates = ['deploymentDate'];
Once achieved you can check the if it's less than by simple function:
#if($project->deploymentDate->lt(\Carbon\Carbon::Now()))
This can also be achieved by a method on the model, in your Project.php
public function checkDeploymentDate()
{
return $this->deploymentDate->lt(\Carbon\Carbon::Now());
}
Then in your views:
#if($project->checkDeploymentDate())
Finally for the panel part, you can add a Blade directive for it:
AppServiceProvider.php
<?php
namespace App\Providers;
use Blade;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Perform post-registration booting of services.
*
* #return void
*/
public function boot()
{
Blade::directive('deploymentDate', function($expression) {
return "<?php echo($expression); ?>";
});
}
}
Blade directives will help you extend new functionality to your Blade templates. So in the above example, in your view template:
{{#deploymentDate(test)}} will echo test

Create helpers function for displaying validation errors

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') !!}

Resources