Laravel Blade double curly brace not working - laravel

By reading the Laravel documentation, I've understood that I should be able to use {{ }} to escape my strings. However, by doing this, I'm getting an error, but the string works fine if I run {!! !!}. Is there anything I'm missing in my code?
A complete view of the code blade file (login.blade.php)
#extends('layouts.app')
#section('content')
<div class="kt-grid kt-grid--ver kt-grid--root">
<div class="kt-grid kt-grid--hor kt-grid--root kt-login kt-login--v3 kt-login--signin" id="kt_login">
<div class="kt-grid__item kt-grid__item--fluid kt-grid kt-grid--hor" style="background-image: url({{ asset('assets/media//bg/bg-3.jpg);">
<div class="kt-grid__item kt-grid__item--fluid kt-login__wrapper">
<div class="kt-login__container">
<div class="kt-login__logo">
<a href="#">
<img src="assets/media/logos/logo-5.png">
</a>
</div>
<div class="kt-login__signin">
<div class="kt-login__head">
<h3 class="kt-login__title">Sign In To Account</h3>
</div>
<form class="kt-form" method="post" action="/login">
{!! csrf_field() !!}
<div class="input-group">
<input class="form-control" type="text" placeholder="Email" name="email" value="" autocomplete="off">
</div>
<div class="input-group">
<input class="form-control" type="password" placeholder="Password" name="password">
</div>
#if ($errors->any())
#foreach($errors->all() as $error)
<p>{!! error !!}</p>
#endforeach
#endif
<div class="row kt-login__extra">
<div class="col">
<label class="kt-checkbox">
<input type="checkbox" name="remember"> Remember me
<span></span>
</label>
</div>
<div class="col kt-align-right">
Forget Password ?
</div>
</div>
<div class="kt-login__actions">
<button id="kt_login_signin_submit" class="btn btn-brand btn-elevate kt-login__btn-primary">Sign In</button>
</div>
</form>
</div>
<div class="kt-login__account">
<span class="kt-login__account-msg">
Don't have an account yet ?
</span>
Sign Up!
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
Error
ErrorException (E_ERROR) syntax error, unexpected 't' (T_STRING),
expecting ',' or ')' (View:
C:\xampp\htdocs\test\resources\views\auth\login.blade.php) Previous
exceptions syntax error, unexpected 't' (T_STRING), expecting ',' or
')' (0)

You need to make 'error' a variable with double brackets. It's considered an undefined constant otherwise.
#foreach($errors->all() as $error)
<p>{{ $error }}</p>
#endforeach
You also need to add the right curly brace and the missing right single quote for the URL in the asset() helper function.
<div class="kt-grid__item kt-grid__item--fluid kt-grid kt-grid--hor"
style="background-image: url({{ asset('assets/media/bg/bg-3.jpg') }}">

Related

Laravel: how to customize login error messages through Fortify

I have the following blade view where I want the error messages printed to me:
<x-layout>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<section class="colorbody vh-100">
<div class="container py-5 h-100">
<div class="row d-flex justify-content-center align-items-center h-100">
<div class="col-12 col-md-8 col-lg-6 col-xl-5">
<div class="card shadow-2-strong bordercard">
<div class="card-body p-5 text-center">
<form method="POST" action="{{route('login')}}">
#csrf
<div class="d-flex align-items-center mb-3 pb-1">
<i class="fa-brands fa-slack fa-2xl me-3"></i>
<span class="h1 fw-bold mb-0">Logo</span>
</div>
<h5 class="fw-normal mb-3 pb-3 letterspace">Enter in your account</h5>
<div class="form-outline mb-4">
<input type="email" class="form-control form-control-lg" name="email" value="{{ old('email') }}"/>
<label class="form-label" for="form2Example17">Email address</label>
</div>
<div class="form-outline mb-4">
<input type="password" class="form-control form-control-lg" name="password"/>
<label class="form-label" for="form2Example27">Password</label>
</div>
<div class="pt-1 mb-4">
<button class="btn btn-dark btn-lg btn-block" type="submit">Login</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
</x-layout>
Except that if I insert an invalid email and a password, only the error message (automatically obtained from auth.failed) of the following type is displayed:
These credentials do not match our records.
I would like, for example, to have the following error message:
'The email entered is incorrect'
'The password entered is incorrect'
How can I do this? Can anyone kindly help me?
Edit Authentication Language Lines in resources/lang/en/auth.php
return [
'failed' => 'These credentials do not match our records.',
'password' => 'The provided password is incorrect.',
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
];

The PATCH method is not supported for this route. when second form is submitted

I've been working on a CMS system for a website im building and it all went good untill I added 2 forms on the same page (both updating different things) and yet it still gives me the error in the title while the other form works fine?
The page banner is the one which returns the error and the 'normal' update works fine!
Web.php
Route::patch('/beheer/paginas/{product}', 'PageController#update')->middleware('auth')->name('beheer.pages.update');
Route::patch('/beheer/paginas/update-banner/{product}', 'PageController#update-banner')->middleware('auth')->name('beheer.pages.banner.update');
Also shows in php artisan route:list that it's a PATCH method route.
My view:
#extends('layouts.beheer')
#section('content')
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card">
<div class="card-header">
<div class="d-inline card-title">Pagina bewerken</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
<div class="card-body">
<form action="{{ route('beheer.pages.update', $page->id) }}" method="POST" enctype="multipart/form-data">
#csrf
{{ method_field('PATCH') }}
<div class="form-group">
<label>Zichtbaarheid:</label>
<select class="custom-select" name="page_hidden">
<option value="0" #if (!$page->page_hidden) selected #endif>Zichtbaar</option>
<option value="1" #if ($page->page_hidden) selected #endif>Verborgen</option>
</select>
</div>
<div class="form-group">
<label>Pagina naam:</label>
<input type="text" name="page_name" value="{{ $page->page_name ? $page->page_name : old('page_name') }}" class="form-control" placeholder="Pagina naam">
</div>
<div class="form-group">
<label>Pagina tekst:</label>
<textarea name="page_text" class="form-control">{{ $page->page_text ? $page->page_text : old('page_meta_title') }}</textarea>
</div>
<div class="form-group">
<label>Pagina title SEO:</label>
<input type="text" name="page_meta_title" value="{{ $page->page_meta_title ? $page->page_meta_title : old('page_meta_title') }}" class="form-control" placeholder="Pagina titel SEO">
</div>
<div class="form-group">
<label>Pagina beschijving SEO:</label>
<input type="text" name="page_meta_description" value="{{ $page->page_meta_description ? $page->page_meta_description : old('page_meta_description') }}" class="form-control" placeholder="Pagina beschijving SEO">
</div>
<button type="submit" class="btn btn-primary float-right">Opslaan</button>
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card">
<div class="card-header">
<div class="d-inline card-title">Banner</div>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
</div>
<div class="card-body">
<form onsubmit="{{ route('beheer.pages.banner.update', $page->id) }}" method="POST" enctype="multipart/form-data">
#csrf
{{ method_field('PATCH') }}
<input type="hidden" name="image_section_banner" value="1">
<input type="hidden" name="page_id" value="{{ $page->id }}">
<div class="form-group">
<img src="{{ asset($page_banner ? $page_banner->image_large_url : asset('assets/img/banner-home.jpg')) }}" class="img-fluid" style="max-height:250px;">
</div>
<div class="form-group">
<label>Afbeelding:</label>
<input type="file" name="image" value="{{ old('image') }}" class="form-control">
</div>
<button type="submit" class="btn btn-primary float-right">Opslaan</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
If there is any additional information needed please let me know!
There is an attribute missing on your 2nd <form>. Here is what you have:
<form onsubmit="{{ route('beheer.pages.banner.update', $page->id) }}" ...>
But it should look like this:
<form action="{{ route('beheer.pages.banner.update', $page->id) }}" ...>
Since you did not specify an action attribute, it is submitting to the current url which probably doesn't have a patch route defined.

Strange view error on Laravel

I have some cotroller like this:
Route::get('/article/create', 'ArticlesController#create');
and here's my ArticlesController#create:
public function create(){
return view('articles.create',
[
'title'=>'Add Artikel',
'username'=>'Whatever Myname',
'status' => 'Offline'
]
);
}
when i trying to access blog.dev/article/create i got this strange errors:
"Trying to get property of non-object (View: E:\xampp\htdocs\blog\resources\views\articles\single.blade.php)"
how come i can get this kind of error when my view is pointing at articles.create but the error is at single.blade.php which it suppose for ArticlesController#view?
this is what in create.blade.php:
#extends('admin.layout')
#section('content')
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">Horizontal Form</h3>
</div>
#include('admin.formerrors')
<form method="post" class="form-horizontal" action="/articles">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input class="form-control" id="title" name="title" placeholder="Judul Artikel">
</div>
</div>
<div class="form-group">
<label for="content" class="col-sm-2 control-label">Content</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="content" name="content"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
#endsection
and here's in my single.blade.php :
#extends('admin.layout')
#section('content')
<h1>{{ $article->title }}</h1>
<p>{{ $article->content }}</p>
<hr>
#foreach($article->comments as $comment)
<blockquote>
<p>{{ $comment->comment }}</p>
<small>{{ $comment->created_at->diffForHumans() }}</small>
</blockquote>
#endforeach
#include('admin.formerrors')
<form method="post" class="form-horizontal" action="/article/{{ $article->id }}/comment">
{{ csrf_field() }}
<div class="box-body">
<div class="form-group">
<label for="comment" class="col-sm-2 control-label">Comment</label>
<div class="col-sm-10">
<textarea class="form-control" rows="3" id="comment" name="comment"></textarea>
</div>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
#endsection
and here's my router:
Route::get('/articles', 'ArticlesController#index')->name('home');
Route::post('/articles', 'ArticlesController#save');
Route::get('/article/{id}', 'ArticlesController#view');
Route::get('/article/create', 'ArticlesController#create');
Route::post('/article/{article}/comment', 'CommentsController#save');
Route::get('/register', 'RegistrationController#create');
Route::post('/register', 'RegistrationController#store');
I try to change the create function by pointing into another view, but still it showing same error and pointing at single.blade view.
I delete all code at single.blade and write 'test' text, i don't get any errors. but i'm pointing my controller for viewing into create.blade not sigle.blade
After seeing you routes the problem is in this two routes :
Route::get('/article/{id}', 'ArticlesController#view');
Route::get('/article/create', 'ArticlesController#create');
In this case Laravel will consider the create in your path blog.dev/article/create as an id parameter of the view route here => /article/{id}.
So as a solution you should simply inverse the two routes :
Route::get('/article/create', 'ArticlesController#create');
Route::get('/article/{id}', 'ArticlesController#view');
You need to find what's in the file by reading more than just that line. Maybe you're including that file in the articles.create? Maybe you're accessing a variable that doesn't exist whenever you load the page. Edit your answer with more of the error and what's inside both blades and we can pinpoint what's wrong.

Undefined variable: user (View: C:\xampp\htdocs\myproject\resources\views\works.blade.php)

I install Auth in my laravel project. In my user table i define a role colum for define Teacher and Student. so in my blade page I want to give access some part for the teacher and hide that part for the student. But I can't do that.
Here is my code: this part only for the teacher.
#if($user->role==Teacher)
<div class="">
<div class="">
<div class="col-md-7 col-md-offset-3">
{!! Form::open(['files'=>true])!!}
<div class="panel panel-default">
<div class="panel-heading">Submit your works</div>
<div class="panel-body">
<div class="form-group>">
<label>Write a new title</label>
<textarea class="form-control" name="assainments" id="assainments"></textarea>
</div>
</div>
<div class="panel-footer clearfix">
<div class="row">
<div class="col-md-6">
<label for="file-upload" class="custom-file-upload">
<i class="glyphicon glyphicon-paperclip"></i>
</label>
<input id="file-upload" name="status_file_upload" type="file"/>
</div>
<div class="col-md-6">
<button class="btn btn-info pull-right btn-am"><i class="fa fa-plus">Add works</i></button>
</div>
</div>
</div>
</div>
#endif
To use the $userin your view you need to pass it from the controller or the method from which you are calling the view.<br/>
Or instead of$useryou can useAuth::user()`:
#if(Auth::user()->role==Teacher)

Laravel Ajax updating variable

<div class="panel panel-primary">
<div class="panel-heading">Add user Status</div>
<form action="{{ route('post.create') }}" method="post">
<div class="panel-body">
<div class="form-group">
<textarea class="form-control" name="body" id="new-post" rows="5" placeholder="Your Post"></textarea>
</div>
</div>
<div class="panel-footer clearfix">
<button class="btn btn-info pull-right"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></i> Post</button>
</div>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
</div>
<div id="main" class="main">
<header><h3>What other people say...</h3></header>
#foreach($top_15_post as $status)
{!!
view ('layouts.app-internal.user-status-layaout',[
'status' =>$status,
'user' =>\App\User::find($status->user_id),
'comments'=>\App\Comment::where('status_id',$status->id)->get(),
'countcomment'=>\App\Comment::where('status_id',$status->id)->count(),
'countlike'=>\App\Like::where('like',1)->where('post_id',$status->id)->count() ,
'countdislike'=>\App\Like::where('like',0)->where('post_id',$status->id)->count(),
])->render()
!!}
#endforeach
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Right part -->
</div>
</div>
</div>
This is my home.blade.php in laravel. im simply implementing a posting text system which user can comment and like to the posts.I'm passing above variables to the user-status-layaout.blade.php, it shows the comments and likes as well as the posts.
This is my user-status-layaout.blade.php.My like and dislike functions are working properly. but i want update like count using ajax.How do i update countlike and countdislike variable instantly using ajax?
<div class="panel panel-default">
<div class="panel-heading">{{ $status->user->name }} Posted on {{ $status->created_at }}</div>
<div class="panel-body">
<div class="row">
<div class="col-md-1">
<img src="{{Auth::user()->getavatar()}}" class="img-responsive">
</div>
<div class="post" data-postid="{{ $status->id }}">
<p>{{ $status->body }}</p>
<div class="interaction">
{{ Auth::user()->likes()->where('post_id', $status->id)->first() ? Auth::user()->likes()->where('post_id', $status->id)->first()->like == 1 ? 'You like this post' : 'Like' : 'Like' }} |
{{ Auth::user()->likes()->where('post_id', $status->id)->first() ? Auth::user()->likes()->where('post_id', $status->id)->first()->like == 0 ? 'You don\'t like this post' : 'Dislike' : 'Dislike' }}
#if(Auth::user() == $status->user)
|
Edit |
<form method="get" action="{{ route('post.delete', ['post_id' => $status->id]) }}" accept-charset="UTF-8" style="display:inline">
<button class="btn btn-xs btn-danger" type="button" data-toggle="modal" data-target="#confirmDelete" data-title="Delete Post" data-message="Are you sure you want to delete this post ?">
<i class="glyphicon glyphicon-trash"></i> Delete
</button>
</form>
#endif
<ul class="list-unstyled list-inline ">
<li>
<button class="btn btn-xs btn-info" type="button" data-toggle="collapse" data-target="#view-comments-{{$status->id}}" aria-expanded="false" aria-controls="collapseExample">
<i class="fa fa-comments"></i>View & Comment </button>
</li>
<li>{{$countcomment}} comments</li>
<li >{{$countlike}} likes</li>
<li>{{$countdislike}} dislikes</li>
</ul>
</div>
</div>
</div>
</div>
<div class="panel-footer clearfix">
<div class="form-group">
<form action="{{ route('comment.create') }}" method="post">
<div class="input-group">
<input type="text" class="form-control" name="comment-text" id="comment-text" placeholder="Add Comment">
<span class="input-group-btn">
<button class="btn btn-info btn-xs" type="submit"><span class="glyphicon glyphicon-plus"></span>Add </button>
</span>
<input type="hidden" value="{{ $status->id }}" name="id" id="id">
</div><!-- /input-group -->
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
</div>
<div class="collapse" id="view-comments-{{$status->id}}">
#if($comments->first())
#foreach($comments as $comment)
<blockquote >
<div class="row" >
<div class="col-md-1" >
<img src="{{\App\User::find($comment->user_id)->getavatar()}}" class="img-responsive">
</div>
<div class="col-md-11">
<ul class=" list-inline list-unstyled ">
<li>
{{\App\User::find($comment->user_id)->name}}
</li>
</ul>
<div class="comment" data-commentid="{{ $comment->id }}">
<p> {{$comment->comment_text}}</p>
<div class="interaction1">
#if(Auth::user() == $comment->user)
Edit |
<form method="get" action="{{ route('comment.delete', ['comment_id' => $comment->id]) }}" accept-charset="UTF-8" style="display:inline">
<button class="btn btn-xs btn-danger" type="button" data-toggle="modal" data-target="#confirmDeleteComment" data-title="Delete Comment" data-message="Are you sure you want to delete this comment ?">
<i class="glyphicon glyphicon-trash"></i> Delete
</button>
</form>
#endif
</div>
</div>
</div>
</div>
<p> posted {{$comment->created_at->diffForHumans()}}</p>
</blockquote>
#endforeach
#else
<p>This status no comment</p>
#endif
</div>
</div>
You could create a timer function using WindowTimers.setTimeout()
$('some-selector').load(function(){
function likeTimer() {
setTimeout(function(){
// Some JSON request that updates the elements' values.
likeTimer();
}, 1000);
}
});
Or you could poll the server periodically with WindowTimers.setInterval()
$('some-selector').load(function(){
setInterval(function() {
// Some JSON request that updates the elements' values.
}, 1000);
});
Then just define a route in Laravel that returns JSON for the data you want to update.

Resources