ErrorException in helpers.php line 519: htmlspecialchars() expects parameter 1 to be string, object given - laravel

It says : (View: C:\xampp\htdocs\lar\resources\views\welcome.blade.php)
And my Welcome blade code is:
#if (count($errors) > 0)
<div class="row">
<div class="col-md-6">
<ul>
#foreach($errors -> all() as $error)
<li>{{$errors}}</li>
#endforeach
</ul>
</div>
</div>
#endif
What is wrong here?

It should be $error not $errors inside foreach loop.

Related

Laravel notification blade else statement not working

I'm trying to show user notification if data is found in notification table else show 'notification not found' on user notification blade but else statement isn't working. It only shows user notification if found and a blank page if notification not found in table.
<body>
<main>
#include('siteLayout.sidebar')
<div class="wrapper">
#include('siteLayout.header')
<div class="main-container">
<div class="main-header">Notifications</div>
<div class="main-contents">
<ul class="notification-lists">
#foreach ($notifications as $notification)
#if ($notification != null)
#if ($notification->musicOwner == Auth::user()->id)
<li class="notification unread"><span class="notification-icon">
<div>You have notifications</div>
</li>
#else
<li class="notification unread"><span class="notification-icon">
<div>You have no notifications yet</div>
</li>
#endif
#endif
#endforeach
</ul>
</div>
</div>
</div>
</main>
</body>
Make sure the $notifications isn't empty as the foreach isn't working. If you would like to show the message if the $notifications is empty make it work like this, with forelse:
<body>
<main>
#include('siteLayout.sidebar')
<div class="wrapper">
#include('siteLayout.header')
<div class="main-container">
<div class="main-header">Notifications</div>
<div class="main-contents">
<ul class="notification-lists">
#forelse ($notifications as $notification)
#if ($notification != null)
#if ($notification->musicOwner == Auth::user()->id)
<li class="notification unread"><span class="notification-icon"> <div>You have notifications</div>
</li>
#endif
#endif
#empty
<li class="notification unread"><span class="notification-icon">
<div>You have no notifications yet</div>
</li>
#endforelse
</ul>
</div>
</div>
</div>
</main>
</body>

Display Laravel Validation Errors?

I'm new to Laravel,
here I'm working with Laravel project. I need to display validation errors in form
<div class="col s12 m6">
<div class="row">
<div class="col s12 input-field">
<input name="newpassword" id="newpassword" type="password" class="validate">
<label for="newpassword"> New Password </label>
<small class="email_error"><div class="error">#error('newpassword'){{$message}}#enderror</div></small>
</div>
</div>
</div>
Controller
$this->validate($request, [
'newpassword' => 'required|min:8',
], [
'newpassword.required' => 'New Password is required field.',
'newpassword.min:8' => 'Enter Minimum 8 Characters',
]);
errors :
The easiest way would be to make a shared blade file, which you can use for any error handling (sessions, requests & etc..). The file would look something like this:
#if ( session()->has('success') )
<div class="row mb-3">
<div class="col-sm-12">
<div class="alert text-white" style="background-color: #47afc4; border-radius:5px;">
{!! session('success') !!}
</div>
</div>
</div>
#endif
#if ( session()->has('error') )
<div class="row">
<div class="col-sm-12">
<div class="alert alert-danger">
{!! session('error') !!}
</div>
</div>
</div>
#endif
#if (count($errors) > 0)
<div class="row">
<div class="col-sm-12">
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
</div>
</div>
#endif
The session()->has('success') would catch any success session messages that you return in you controller:
return redirect('your route')->with('success', 'Success message');
The session()->has('error') would catch any error session messages that you return in you controller:
return redirect('your route')->with('error', 'Error message');
And $errors->all() will return any errors from the request validation.
You can then use this file in the pages you want, simply include it:
#inlcude('path.to.shared.errors.file')
If you need any more info, let me know.
You can use $errors->any() function
To find your answer look at this :
Displaying the Error Messages in Laravel after being Redirected from controller

Laravel - Invalid argument supplied for foreach()?

I have this code in my controller:
public function contact(){
$people = ['Michael', 'martin', 'Peter', 'Marian'];
return view('contact', compact('people'));
}
and in my contact.blade.php:
#extends('layouts.app')
#section('content')
<h1>Contact Page</h1>
#if (count($people))
<ul>
#foreach(#people as $person)
<li>{{$person}}</li>
#endforeach
</ul>
#endif
#endsection
#section('footer')
#endsection
I am getting the error:
Invalid argument supplied for foreach() (View: /home/mao/Documents/blog/resources/views/contact.blade.php)
I do not see the error.. been rewriting it twice. this should be correct as far as i can see on online guides?
I think you just have a mistype in your foreach
Try this
#foreach($people as $person)
<li>{{$person}}</li>
#endforeach
Change the # in $
change #people to $people (# => $)
#extends('layouts.app')
#section('content')
<h1>Contact Page</h1>
#if (count($people))
<ul>
#foreach($people as $person)
<li>{{$person}}</li>
#endforeach
</ul>
#endif
#endsection
#section('footer')
#endsection

laravel Form Validation errors

version: laravel 5.7
Router:
Route::get('regist','User\RegistController#registView');
Route::post('regist','User\RegistController#regist');
Form:
<form class="form-signin" method="POST" action="/regist">
.....
</form>
Validate:
$this->validator=Validator::make($input,$rule,$message);
if($this->validator->fails()){
return \Redirect::back()->withErrors($this->err());
}
The Problem:
Do not display an error message.Need to press Enter in the address bar to reload the page.Want to use the Validateor::make method.How can I modify it?
If the validation worked, did your put the error message to be displayed on the page you redirected to?
for example in your blade page did you do something like below?
#if(count($errors->all()) > 0)
<div class="alert alert-danger" role="alert">
<p><b>Required Fields Missing!</b></p>
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
I will show you a simple example, if you are in the controller use the validator facade
use Illuminate\Support\Facades\Validator;
In the conroller user the below code
public function update(Request $request, $id) //use of request
{
$this->validate($request, [
'name' => 'required',
], [
'name.required' => 'Name is required',
]);
}
If validation fails it will automatically redirect back or you could use a validator fail statement and send a modified message.
After that you should put an if statement to display the error message as below:
#if(count($errors->all()) > 0)
<div class="alert alert-danger" role="alert">
<p><b>Required Fields Missing!</b></p>
<ul>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Or if you have only one defined error message use:
#if(Session::has('success'))
<div class="alert alert-success" role="alert">
{ Session::get('success') }}
</div>
#endif
Hope this helps
For add validation on form you can use Laravel Request Validation classes. which will help you to pull validation in separate class.
Here is the reference for request classes :- https://laravel.com/docs/5.7/validation#form-request-validation
That will help you to pull validation and also improvements in your code.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif

using Pagination with orderBy Eloquent Model

I have a function witch is getting all properties joining the images tables with orderBy and need to add paginate
here is my function
public function show_all()
{
$properties = PropertyDetail::where('active', 1)->with('propImages')->paginate(15)
->orderBy('id', 'DESC')
->get();
return View::make('portal.properties.view_all', compact('properties'));
}
in my view I got
call_user_func_array() expects parameter 1 to be a valid callback,
class 'Illuminate\Support\Collection' does not have a method 'orderBy'
another thing when I removed the orderBy('id', 'DESC')->get() and I try
it works but when I try to put {{ $prop->links() }} in the view I got
Call to undefined method Illuminate\Database\Query\Builder::links()
here is how my view looks like
#foreach($properties as $prop)
<div class="property col-xs-12 col-sm-6 col-md-4">
<div class="image">
<div class="content imgContainer">
{{ HTML::link('property-details/'.$prop->id, '') }}
#foreach($prop->propImages->slice(0, 1) as $image)
{{ HTML::image('images/propertyImages/'.$image->image, $prop->title) }}
#endforeach
</div>
<div class="price"> OMR. <span class="priceNumber"> {{ $prop->price }}</span></div>
</div>
<div class="title">
<h2>{{ HTML::link('', $prop->title, array('title'=>'$prop->title')) }}</h2>
</div>
<div class="location">{{ trans('location.'.$prop->propLocation->city) }}</div>
<div class="bathrooms">
<div class="content">{{ $prop->bathroom }}</div>
</div>
<div class="bedrooms">
<div class="content">{{ $prop->bedroom }}</div>
</div><!-- /.bedrooms -->
<div class="receptionRoom">
<div class="content">{{ $prop->dining_room }}</div>
</div>
</div>
#endforeach
{{ $prop->links() }}
The correct syntax is:
$properties = PropertyDetail::where('active', 1)
->with('propImages')
->orderBy('id', 'desc')
->paginate(15);

Resources