I don't understand why the error message is not displayed in form control with laravel. What is my error?
While I could get the "okey" message when I fulfill the conditions below, I could not clearly understand that the error message was not displayed because it did not meet the conditions.
"Information Is Beautiful When It Is Shared."
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Website\contact;
Route::get('/register', [contact::class, 'index'])->name('register');
Route::post('/register-submit', [contact::class, 'post'])->name('register.post');
contact.php
<?php
namespace App\Http\Controllers\Website;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class contact extends Controller
{
public function index()
{
return view('Website.contact');
}
public function post(Request $request)
{
$request->validate([
'name' => ['required', 'max:10'],
'email' => ['required'],
]);
echo "okey";
}
}
contact.blade.php
<div class="container pt-lg-5">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form method="post" action="{{route('register.post')}}">
#csrf
<div class="form-group">
<label for="exampleInputEmail1">name</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label for="exampleInputPassword1">E-mail</label>
<input type="email" class="form-control" name="email">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
Illuminate\Validation\Validator {#258 ▼
#translator: Illuminate\Translation\Translator {#255 ▶}
#container: Illuminate\Foundation\Application {#2 ▶}
#presenceVerifier: Illuminate\Validation\DatabasePresenceVerifier {#259 ▶}
#failedRules: array:2 [▼
"name" => array:1 [▼
"Required" => []
]
"email" => array:1 [▼
"Required" => []
]
]
#excludeAttributes: []
#messages: Illuminate\Support\MessageBag {#261 ▼
#messages: array:2 [▼
"name" => array:1 [▼
0 => "The name field is required."
]
"email" => array:1 [▼
0 => "The email field is required."
]
]
#format: ":message"
}
#data: array:3 [▶]
#initialRules: array:2 [▶]
#rules: array:2 [▼
"name" => array:2 [▼
0 => "required"
1 => "max:10"
]
"email" => array:1 [▼
0 => "required"
]
]
#currentRule: "required"
#implicitAttributes: []
#implicitAttributesFormatter: null
#distinctValues: []
#after: []
+customMessages: []
+fallbackMessages: []
+customAttributes: []
+customValues: []
+extensions: []
+replacers: []
#fileRules: array:9 [▼
0 => "Between"
1 => "Dimensions"
2 => "File"
3 => "Image"
4 => "Max"
5 => "Mimes"
6 => "Mimetypes"
7 => "Min"
8 => "Size"
]
#implicitRules: array:10 [▶]
#dependentRules: array:21 [▼
0 => "After"
1 => "AfterOrEqual"
2 => "Before"
3 => "BeforeOrEqual"
4 => "Confirmed"
5 => "Different"
6 => "ExcludeIf"
7 => "ExcludeUnless"
8 => "ExcludeWithout"
9 => "Gt"
10 => "Gte"
11 => "Lt"
12 => "Lte"
13 => "RequiredIf"
14 => "RequiredUnless"
15 => "RequiredWith"
16 => "RequiredWithAll"
17 => "RequiredWithout"
18 => "RequiredWithoutAll"
19 => "Same"
20 => "Unique"
]
#excludeRules: array:3 [▼
0 => "ExcludeIf"
1 => "ExcludeUnless"
2 => "ExcludeWithout"
]
#sizeRules: array:8 [▼
0 => "Size"
1 => "Between"
2 => "Min"
3 => "Max"
4 => "Gt"
5 => "Lt"
6 => "Gte"
7 => "Lte"
]
#numericRules: array:2 [▼
0 => "Numeric"
1 => "Integer"
]
#dotPlaceholder: "YEdPXjrZ4cLZZ3wV"
}
If you are willing for another way to solve your problem, or a quick solution you can always use Validator class directly,
First add this to your controller,
use Illuminate\Support\Facades\Validator;
Then,
public function post(Request $request)
{
$validator = Validator::make($request->all(),[
'name' => ['required', 'max:10'],
'email' => ['required'],
]);
if ($validator->fails()) {
return redirect()->back()
->withErrors($validator);
}
echo "okey";
}
In my case I solved it by removing \Illuminate\Session\Middleware\ StartSession::class from 'middleware' in Kernel.php, leaving it in the 'web' group in the same file.
go on kernal.php and find web route and removed \Illuminate\Session\Middleware\StartSession::class,
then check all validation working fine
Related
I am using PHP SDK to fetch all posts and comments with relative likes from a facebook page. When I try to fetch also the name of the commentators with the property from, nothing change. This is the link:
/me/posts?fields=message,comments.summary(true) {message,from{id,name},likes.summary(true)},likes.summary(true),created_time'
I still get:
"comments" => array:3 [▼
"data" => array:2 [▼
0 => array:3 [▼
"message" => "commento al secondo post"
"likes" => array:3 [▶]
"id" => "126427989164897_126428849164811"
]
1 => array:4 [▶]
]
There is no attribute from in the response, as you can see. I have tried a lot of combination but I can see the name and the id of the commentator only if is the page itself.
Any suggestion?
I have a search engine with typeahead. What I want is that after doing a search and giving the submit, show the results. This gives two problems: first it returns an empty array and second, it does not allow me to access the properties telling me it is not an object.
In controller, I used collect() to allow me to access the properties, but it does not work and WHERE either.
public function store(Request $request)
{
$url = 'storage/json/es/noticia.json';
$datos = file_get_contents($url);
$data = json_decode($datos, true);
$data = collect($data)->where("title","LIKE","%{$request->texto}%")->all();
return view('web.buscar.index', compact('data'));
}
If I use $data = collect($data)->all(); I can see the collection:
array:8 [▼
0 => []
1 => array:4 [▼
"id" => 2
"title" => "There is a title"
"lead" => "There is a lead"
"slug" => "there-is-a-title"
]
2 => array:4 [▶]
3 => array:4 [▶]
4 => array:4 [▶]
5 => array:4 [▶]
6 => array:4 [▶]
7 => array:4 [▶]
]
Then If I try: $value->title in the view I have the error: Trying to get property 'title' of non-object. In the view I have:
{!! Form::open([
'route' => 'buscar',
'id' => 'buscar',
'name' => 'buscar',
'class' => 'buscador col-xs-12',
'method' => 'POST',
'accept-charset' => 'utf-8'
]) !!}
<input id="texto" name="texto" class="input_buscador typetitulo" autocomplete="off" type="text"/>
{!! HTML::image('images/web/icons/lupa.svg', '', array('height' => '30', 'class' => 'boton_buscador', 'onclick' => 'document.buscar.submit()') ) !!}
{!! Form::close() !!}
#if(isset($data))
#foreach($data as $value)
<span>{{$value->title}}</span><br>
#endforeach
#endif
If I use $data = collect($data)->pluck('title'); in the controller and in the view I don't call the property 'title', this works, but it's not what I want because I need to access other properties too.
Any idea? Thanks in advance!
This is failing because the first array in your array does not have any values, so you will get undefined index, remove any empty arrays by doing
public function store(Request $request)
{
$url = 'storage/json/es/noticia.json';
$datos = file_get_contents($url);
$data = json_decode($datos, true);
$data = array_filter($data);
$data = collect($data)->where("title","LIKE","%{$request->texto}%")->all();
return view('web.buscar.index', compact('data'));
}
Or you can test to see if it is there in your foreach
#foreach($data as $value)
<span>{{$value->title ?? ''}}</span><br>
#endforeach
You can then search the collection using a filter
collect($data)->filter(function ($item) use ($request) {
return $item->title == $request->texto;
)
You can edit the return to be more granular using stristr etc
I tried to set cookie (easy example):
Route::get('set', function () {
Cookie::queue(Cookie::forever('cart_id', 123));
});
Route::get('get', function (Illuminate\Http\Request $request) {
dump($request);
});
But I get in dump of $request:
Request {#40 ▼
...
+cookies: ParameterBag {#43 ▼
#parameters: array:3 [▼
"cart_id" => null
"XSRF-TOKEN" => null
"laravel_session" => null
]
}
+headers: HeaderBag {#46 ▼
#headers: array:9 [▼
...
"cookie" => array:1 [▼
0 => "cart_id=eyJpdiI6IldNbjJwMHdRclBmWkFkUFJ3SnJ6Z2c9PSIsInZhbHVlIjoiVnhBWGl3ODFBaGhwTCtSc0NoSmlLUT09IiwibWFjIjoiYjU2NjRmNDI4OGQ4NGZmZjNjMDhmYTEwMWJlOWQ2ZjUwYjAxNTMxYjU2OWY3Y2I0NjYxNzQzZTNhZjVkMDcxOSJ9; XSRF-TOKEN=eyJpdiI6Ino2ckNVSU5cL3VEcE5VQWRqQm1wTkV3PT0iLCJ2YWx1ZSI6IkF2b1Y0VDFkMlVqam8zeWVJQk55Mm96XC9PZFlyZUxma3FBTXNBV2VoK3AyTkp5Q2RNOHZtQnRyMFB0a3c0Mm5cL1hJdkkyY1haUHhZV0FEejdKSjMzaHc9PSIsIm1hYyI6IjFlNjVkZDE2NDJiYjA2MDQzM2JiYWYzMWY2YWRiNTk0ZDM4MjBlNmEzZmEwNTUxZWUzY2NmYmEyZDljMmYwOGIifQ%3D%3D; laravel_session=eyJpdiI6Imh1QW1iamlscnVpV3JFTThHYWdTNkE9PSIsInZhbHVlIjoiZElUMU1MZlRwa1BRZFd6T0pWMGhSanNCcmVNcjdEUGJrd2Zhc3RqbFUzVFBOamxsclBnbWhHcVk4QXFXU2VFMkVGYXBUSm4xVVFvcGZoZnVxN1VTVnc9PSIsIm1hYyI6IjkyYjA5YTI3MmE3MWE5OWRhNDBjYWZkZjQwZjk0NjI1OTY4MGNjZDM4ZDQ4YWE3YjdkMWMxNmQwZjgxMzQzZTYifQ%3D%3D"
]
]
#cacheControl: array:1 [▶]
}
}
Of course, firstly I go to /set link, then /get.
Where did I go wrong? Why all of my cookies equals to null?
By the way, last 5 hours I tried to make a function, that will contains Cookie::queue() function, but it goes wrong too. I dont know, what's wrong with my laravel cookies
I have 3 model: Tag, Post, User, and map them with TagPost, TagUser model.
When User search Tag in input search, I using $http.post (VUE) and send array tag selected to Server.
Example tags print in server:
array:2 [
0 => array:10 [
"id" => 197
"name" => "nisi"
"slugify" => "nisi"
"thumbnail" => "http://lorempixel.com/400/400/?85397"
"seo_title" => null
"seo_description" => null
"seo_keyword" => null
"cover" => null
"created_at" => "2017-06-13 07:19:30"
"updated_at" => "2017-06-04 06:01:14"
]
1 => array:10 [
"id" => 184
"name" => "dolores"
"slugify" => "dolores"
"thumbnail" => "http://lorempixel.com/400/400/?45793"
"seo_title" => null
"seo_description" => null
"seo_keyword" => null
"cover" => null
"created_at" => "2017-06-08 15:37:59"
"updated_at" => "2017-05-27 13:34:02"
]
]
I want when user search tags then view update users and posts have tags user search.
View here:
In server (laravel) how to code with relationship model using least query?
Assuming that you have correctly set your relationship this should work
Tag::with('posts','users')->whereIn('id', $your_ids_send_by_vue)->get();
And then in your controller do the logic to get your desired data
I'm trying to use https://github.com/skmetaly/laravel-twitch-restful-api package to get twitch integration to my website.
That's the error that i get.
ErrorException in helpers.php line 469:
htmlentities() expects parameter 1 to be string, array given (View: /var/www/rafdev.ovh/html/msvixen/resources/views/twitch.blade.php)
My controller
$code = Input::get('code');
if ($code !== null)
{
$token = TwitchApi::requestToken($code);
} else
{
$token = null;
}
$data = TwitchApi::streamsFollowed($token);
return view('twitch', ['token' => $token, 'data' => $data]);
my view
#extends('master')
#section('content')
<h1>Twitch.TV</h1>
{{ $token }}
{{ $data }}
#endsection
After using dd()
array:9 [▼
0 => array:11 [▼
"_id" => 17733016640
"game" => "World of Warcraft"
"viewers" => 15551
"created_at" => "2015-11-15T22:27:13Z"
"video_height" => 1080
"average_fps" => 60.2769481401
"delay" => 0
"is_playlist" => false
"_links" => array:1 [▶]
"preview" => array:4 [▶]
"channel" => array:22 [▶]
]
1 => array:11 [▶]
2 => array:11 [▶]
3 => array:11 [▶]
4 => array:11 [▶]
5 => array:11 [▶]
6 => array:11 [▶]
7 => array:11 [▶]
8 => array:11 [▶]
]
so it works, but when i try to display data - its back to the htmlentities() error
This is happening because $data is returned as an array.
When TwitchApi::streamsFollowed($token); is called, the Facade calls the method in Skmetaly\TwitchApi\Services\TwitchApiService.
This in turn creates an instance of Skmetaly\TwitchApi\API\Users and calls the streamsFollowed() method there.
This method makes a call to /streams/followed which returns a data set such as the example below. It's automatically converted to an array rather than JSON using the Guzzle HTTP Client's json() method.
{
"_links": {
"self": "https://api.twitch.tv/kraken/streams/followed?limit=25&offset=0",
"next": "https://api.twitch.tv/kraken/streams/followed?limit=25&offset=25"
},
"_total": 123,
"streams": [...]
}
In order to display the streams you'd need to iterate over the streams array within $data.
If you were to modify your controller slightly
return view('twitch', ['token' => $token, 'streams' => $data->streams]);
You'd then be able to iterate over the streams in your view.
#foreach($streams as $stream)
{{ $stream }}
#endforeach
Update: You'll notice that each stream is also an array. What this means is you need to choose which of the keys in each array you'd like to display. Let's assume that inside one of the streams there is a key called broadcaster which contains a string; you could modify the above as follows.
#foreach($streams as $stream)
{{ $stream['broadcaster'] }}
#endforeach
Having now read the streams example response documentation it would appear that the contents of a stream varies depending on whether or not the stream is online. NB: This is assuming the data structure is the same as you've not posted the contents of a stream in your question.
This means that offline, {{ $stream['broadcaster'] }} would work, but when online it wouldn't and you'd get the same error. What you'll likely need to do is use an #if #else block in your #foreach to determine if the stream is null before trying to echo part of the information.
You could also filter the offline streams in the controller by removing null values from data.