Several updates on the same view Laravel - laravel-5

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

Related

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?

Invalid argument supplied for foreach with laravel 5.4

I'm getting Invalid argument supplied for foreach() error in my view after publishing a post and the code i use for looping in my controller is this:
public function edit($id)
{
$post = Post::find($id);
$categories = Category::all();
$cats = array();
foreach ($categories as $category) {
$cats[$category->id] = $category->name;
}
$tags = Tag::all();
$tags2 = array();
foreach ($tags as $tag) {
$tags2[$tag->id] = $tag->name;
}
return view('admin.posts.edit')->withPost($post)->withCategories($cats)->withTags($tags2);
}
this is the only part i handle loops in my postcontroller edit section. And I know the issue is from Tags loop because when I remove the tags code in my view other part will show up correctly.
Oh and this is the loop i use in my view:
#extends('layouts.app')
#section('content')
<div class="col-md-8">
<div class="panel panel-default">
<div class="panel-heading">{{ $post->title }}</div>
<div class="panel-body">
<p><img src="{{ asset('uploads/' . $post->image) }}" alt="{{ $post->title }}" class="img-responsive" /></p>
<p>{!! $post->body !!}</p>
</div>
<div class="tags">
#foreach ($tags as $tag)
<span class="label label-default">{{ $tag }}</span>
#endforeach
</div>
</div>
</div>
#endsection
#section('sidebar')
<div class="col-md-4">
<div class="panel panel-primary">
<div class="panel-heading"><i class="fa fa-info"></i> Post Info</div>
<div class="panel-body">
<dl class="dl-horizontal">
<label>URL:</label>
<p>{{ url('blog/'.$post->slug) }}</p>
</dl>
<dl class="dl-horizontal">
<label>Created On:</label>
<p>{{ date('M j, Y h:ia', strtotime($post->created_at)) }}</p>
</dl>
<dl class="dl-horizontal">
<label>Last Update:</label>
<p>{{ date('M j, Y h:ia', strtotime($post->updated_at)) }}</p>
</dl>
<dl class="dl-horizontal">
<label>Posted In:</label>
<p>{{ $post->category->name }}</p>
</dl>
<hr/>
<div class="row">
<div class="col-md-6">
{!! Html::linkRoute('posts.edit', 'Edit', array($post->id), array('class' => 'btn btn-warning btn-block')) !!}
</div>
<div class="col-md-6">
{!! Form::open(['route' =>['posts.destroy', $post->id], 'method' => 'DELETE']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger btn-block']) !!}
{!! Form::close() !!}
</div>
</div>
<hr/>
{!! Html::linkRoute('posts.index', '<< Back to Posts', [], array('class' => 'btn btn-primary btn-block')) !!}
</div>
</div>
</div>
#endsection
PS: I'm using Laravel 5.4
Post updated view!
You can't get tags via post so Try this...
<div class="tags">
#foreach ($tags as $tag)
<span class="label label-default">{{ $tag }}</span>
#endforeach
</div>
Try:
return view('admin.posts.edit')->with("post",$post)->with("categories",$cats)->with("tags",$tags2);
and at view
<div class="tags">
#foreach ($tags as $tag)
<span class="label label-default">{{ $tag->name }}</span>
#endforeach
</div>
in your table if your primary key name if not id you have to mention in your relationship Link
2.for your post has one category so you can show this into your view like this
{{ $post->category->name }}
if you get result you can use it. but seems you need tag key and value pair so use this methods
$tags = Tag::where('post_id',$post->id)->groupBy('id')->get();
or try your way
$tags = Tag::all();
$tags2 = array();
foreach ($tags as $tag) {
$tags2[$tag->id] = $tag->name;
}
return view('admin.posts.edit',compact($post,$tags2,$cats));
in your view try this
#foreach ($tags as $key => $tag)
<span class="label label-default">{{ $tag }}</span>
#endforeach

Blade from Database not working properly

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

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

Undefined variable: section (View: D:\xampp\htdocs\laravel5\resources\views\cms\sections.blade.php)

I try to made simple crud and found this error
Undefined variable: section
(View:D:\xampp\htdocs\laravel5\resources\views\cms\sections.blade.php)
Route:
Route::resource('sections','SectionsController');
View:
{!! Form::open(["url"=>"sections","files" => "true"]) !!}
section name: {!! Form::text("section_name")!!}
<hr/>
{!! Form::file('image',["class"=>"filestyle","data-buttonText"=>"select image","data-input"=>"false"]) !!}
<br/>
{!! Form::submit("insert#upload",["class"=>"btn btn-primary"]) !!}
{!! Form::close() !!}
<div class="row">
<h3>get data from db ::</h3>
#foreach($sections as $section)
<div class="col-md-3">
<div class="thumbnail">
<p>{{$section->section_name}}</p>
<img src="/uploads/{{$section->image_name}}" width="90%" height="90%">
</div>
</div>
#endforeach
</div>
<div class="row">
<div class="col-md-4">
<div class="thumbnail">
{!! Form::open(["url"=>"sections/$section->id","method" => "patch"]) !!}
{!! Form::text("section_name",$section->section_name)!!}
{!! Form::submit("update",["class"=>"btn btn-success"]) !!}
{!! Form::close() !!}
</div>
</div>
<div class="col-md-4">
<div class="thumbnail">
{!! Form::open(["url"=>"sections/$section->id","method" => "delete"]) !!}
{!! Form::submit("delete",["class"=>"btn btn-danger"]) !!}
{!! Form::close() !!}
</div>
</div>
</div>
Controller:
use DB;
use Input;
use Validator;
class SectionsController extends Controller {
public function index() {
$section=DB::table('sections')->get();
return view('cms.sections')->withSections($section);
}
public function create() {
return view('cms.sections');
}
public function store(Request $request) {
$this->validate($request,
['section_name' => 'required|unique:sections,section_name|max:20',
'image' => 'mimes:jpeg|max:1024']);
$section_name=$request->input('section_name');
$file=$request->file('image');
$destinationPath='uploads';
$filename=$file->getClientOriginalName();
$file->move($destinationPath,$filename);
$rules = array('image' => 'mimes:jpg,png,gif|required|max:10000');
$validator = Validator::make(Input::all(), $rules);
DB::table('sections')->insert(['section_name' => $section_name,
'image_name' => $filename]);
return redirect('sections');
}
public function update(Request $request, $id) {
$section_name=$request->input('section_name');
DB::table('sections')->where('id',$id)->update(['section_name'=>$section_name]);
return redirect('sections');
}
public function destroy($id) {
DB::table('sections')->where('id',$id)->delete();
return redirect('sections');
}
}
There is one definite problem in your View code: You use $section after you end your #foreach block that defines it, for example here:
#foreach($sections as $section)
...
#endforeach
...
<div class="row">
<div class="col-md-4">
<div class="thumbnail">
{!! Form::open(["url"=>"sections/$section->id","method" => "patch"]) !!}
------------------------------------^^^^^^^^
I suspect you intended to place the <div class="row"> blocks inside your #foreach block.

Resources