How does pagination work in Laravel 5? - php

I created pagination by writing
$users = User::paginate(25);
Then to display the links, I used the following command,
{!! $users->render() !!}
As the command given below no longer works in Laravel 5
{{$users->links()}}
{!! $users->render() !!} displays the pagination links in the view but there is a slight issue with the links.
This link it generates is like this
http://localhost/laravel/public/users/?page=2
How can I change the generated link to something like this
http://localhost/laravel/public/users?page=2
i.e to remove the slash(/) after users in the url?
The reason for this is that the url with slash after users fails.
Any help regarding pagination will be greatly appreciated.
Thanks

Please try the following $users->setPath('')->render();. I had a similar situation and this fixed the issue for me.

Related

php echo stops rendering the page half way while laravel blade template works fine

I have a mcqs website, the problem is that when i try to load a quiz page with multiple questions and their answers, it loads fine.
Working code:
#foreach ($question->answers as $answer)
<span style="display:inline-block">
{{$answer->content}}
</span>
#endforeach
Not Working Code:
#foreach ($question->answers as $answer)
<span style="display:inline-block">
{!! $answer->content !!}
</span>
#endforeach
{!! $answer->content !!} is the same as echo $answer->content
however for the same code when i try to render the answers with HTML formatting using <?php echo , the page stops rendering midway. forexample if say the page was to load 30 questions , it will only load 10 and stops rendering . Even when i tried using {!!$answer->content!!} , the problem remains the same.
Note: i am returning set of questions using the below code:
$questions = Question::all()->where('exam_id',$exam_id)->random($questionsCount)->shuffle();
return view('quiz',compact('exam','questions','questionsCount','t'));
I have one to many relationship setup so i can extract the answers of the question using $question->answers
it works fine when i don't echo any answer content.
Anyhelp will be greatly appreciated. Thanks!
Looks like an issue with unescaped text. When you use the blade template's escape method ({{ }}) it is successful. When you use the unescaped method, ({!! !!}) it fails. This is the same when using the unescaped php echo.
There is likely some type of character in one of your questions that is causing a break of the loop or php echo.
To fix, use the blade escape {{ }} or php escape (htmlentities(), htmlspecialchars() etc) in the echo.
htmlentities-vs-htmlspecialchars is worth checking out as well.

how show text form contact.blade.php in the message in laravel

I makes website by laravel as multi language
and I make full switches to change the language and it work as the flowing code
<?php
return [
'home_menu'=>'home',
'about_menu'=>'about',
'contact_menu'=>'contact',
'message'=>'<h1> welcoome</h1>',
];
Now I need to show text from contact.blade.php from resource/views Instead of
welcoome.
There are many ways you can show your translations in Laravel
{{ trans(filename.arrayIndex) }}
#lang(filename.arrayIndex)
and in case you have html in your translations then
{!! trans(filename.arrayIndex) !!}
I hope this will help you but you must check all the Laravel documentation before starting the project. It will really help you understanding the Lara Concept better

how to show article comment contains (php codes) as string

hi sorry for my language.
im using laravel 5.6 to create a simple article site.
i have a problem with show article comments as string. the problem is : laravel try to render php codes and the page give me some errors.
imagine a user wants to send his laravel code as comment to me:
(this is just an example)
#foreach($article->tags as $tag)
<a class="tags" href="/tags/{{ $tag->slug }}">{{ $tag->title }}</a>
#endforeach
if user send this codes as comment to me, my page not loading and give me errors
my code to show comments :
{!!$comment->body !!}
help me please . thank you
Replace {!!$comment->body !!} with {{$comment->body }}.
When you use {!! !!}}, the template engine tries to parse the contents within the braces. Use {{ }} so that the contents are displayed as it is escaping all html.
i found the problem :
problem is for laravel blade codes.
like: #foreach . #if . #auth and ....
i have to scape this codes with space :
# foreach . # if and ....

Laravel 5.5: Blade forces https on external URLs

i'm displaying external Website Links from my Users in a Profile Page. The URL Format is: www.xyz.com. When i put a http:// before the {{ url }} Laravel changes the Protocoll automatically to https://. In the Result a lot of the Websites don't open properly. I set the APP_URL in the .env File to
https://domain because i want to use SSL in the App.
Laravel (Blade) is removing the http:// from the URL when i just want to print the Variable too. Even when i put http:// right in front the URL in the DB (MySQL) i still have the same problem.
All i do in the Controller is:
$user = \App\User::find($id);
After 2 days of research i'm reaching out to the community. Basically i'm searching for something like secure_url() inverse.
What i tried:
{{ url('http://'.$url) }}
{!! url('http://'.$url) !!}
{!! url(e('http://'.$url)) !!}
<a href="http://{{ $url }}">
{{ $url }}
Putting the Protocoll right in the DB
What do i not get here? Thank you for any help.
EDIT
Laravel does this only when i visit my site over https.
UPDATE & SOLUTION
It was the problem of the Laravel Cache Speed Package. I deactivated it and everything was back to normal. My fault, my bad.

Laravel "{slug}" not being replaced, instead being url encoded

I've recently embarked on learning Laravel. In doing so I've began completing tutorials from around the web, including from youtube. In one such tutorial I've been developing a very basic blog. I've hit something of an impasse when working with slugs. For some reason, it just doesn't want to work for me!
When the following code is entered in my 'routes.php' file, I have noticed that the braces '{' and '}' are automatically converted to URL encoding and the word "slug" is displayed. Additionally, the '/' following the word posts is nowhere to be seen. This means a link will read as "http:[domain name]/posts/%7Bslug%7D[article name]"
Route::get('/posts/{slug}', [
'as' => 'post-show',
'uses' => 'PostController#getIndex'
]);
The problem is partially resolved if I change the code thus:
Route::get('/posts/{slug?}', [
'as' => 'post-show',
'uses' => 'PostController#getIndex'
]);
The word "slug" is no longer displayed as part of the URL. However, for some reason the slug's preceding '/' is still not visible, as though eaten by the slug in question! It now reads "http:[domain name]/posts**%7Bslug%7D**[article name]".
I'd really appreciate pointing in the right direction if anyone can lend a hand. Thank you.
Based on your comment, the way you're calling the action is incorrect. Try this view code:
#if ($posts->count())
#foreach ($posts as $post)
<article>
<h2>{{ $post->title }}</h2>
{{ Markdown::parse(Str::limit($post->body, 157)) }}
read moreā€¦
</article>
#endforeach
#endif
I am not near a terminal to test this, but it should get you closer. The problems this code addresses are:
To generate a URL using the route name, use route. Your code was using action.
To pass a parameter and fill in the "{slug}", you pass an array as the second argument to route (and action for that matter). Your code was neither passing an array nor passing the value into the function.
You were missing a } after the Markdown::parse, but that may have been a copy & paste error.
If this helped you, remember to up vote and mark as accepted!

Categories