Call to undefined function in Laravel view - php

I have a About Us portion in my Laravel website home page. I have uploaded a huge article for About Us from admin panel which is shown in about us page. But I want to show a specific number of word for about us in home page and add read more after that. while click on read more it goes to about us page.
But I can't show specific number of words from that article in my home page.
I have use echo word_teaser_end($about_us[0]->details, 10); function but it gives error
Call to undefined function word_teaser_end() (View: E:\xampp\htdocs\OFFICE\SEN_CARE\sencare.com.bd\resources\views\frontend\home\home.blade.php)
What should I do now ?

I don't know about the word_teaser_end() as it's define function,
Try str_limit() helper function which can help you to load the number of words from string
{{ str_limit($string,'10') }}
https://laravel.com/docs/5.6/helpers#method-str-limit

This should help.
{!! str_limit($string,'10') !!}

Related

How do I use variables in a different place?

I'm trying to use a variable in a different place but it doesn't seem to be working.
Basically the idea that we had is to use the data you assign in the admin panel for your contact info that will be displayed on the contact page onto our footer. But whenever we try to use {{ location.adress }} in our footer.twig file it returns nothing.
To insert anything in view in .twig file you have to determine it in corresponding controller file, like #K. B. said.
In this particular case open /catalog/controller/common/footer.php
Find
$data['newsletter'] = $this->url->link('account/newsletter', '', true);
Add after
$data['address'] = nl2br($this->config->get('config_address'));
Now open /catalog/view/theme/YOUR_THEME/template/common/footer.twig
And place anywhere you need
{{ address }}
Than clear TWIG cache. Done. Now the address from the settings is in your footer.
If you wish to retrieve some data in your template, you must declare that data in corresponding controller file. For example if need retrieve {{ address }} it should be declared $data['address'] = 'data_retrieved_from_db'; For admin and for catalog are different files.

Dynamic nav laravel 5.3

I'm building a navigation similar to wordpress basically what I want to do is add the navigation dynamically. my code in the header.blade.php looks something like:
#foreach($nav as $navs)
<li><a>$navs->title</a></li>
#endforeach
I will like the links to display in all my pages but I'm not sure how to set this in the route. Please any suggestion would be really helpful.
I'm still new to Laravel.
Sorry for not explaining properly, what I actually want is:
Once I add the navigation links and the details, it should display something like this
localhost/about-us
localhost/contact-us
So far I have got all the details and slug in the db.
e.g Controller to get all pages
class NavController extends Controller
{
public function nav()
{
$navs=Page::all();
return view('themes.first.header')->withNavs($navs);
}
}
e.g header.blade looks something like this
#foreach($navs as $nav)
<li>$nav->title</a></li>
#endforeach
The header is included on every page in master.blade so my issue is
for example I go localhost/blog
The links doesn't display but if I add route like this
Route::get('nav','NavController#nav');
Then go to /nav then it works fine. but I want something like this
localhost/about-us
and also for the links to display on every page. I hopefully I have explained properly now if not just let me know.
Please ignore suggesting code for the href="" because I've already done all that bit in my other pages etc so I know how to handle the code for the href, the part that I'm having issues is just with the navigation links not showing on all the pages.
Finally got the navigation working by using the suggestion from this thread
Proper way to make a dynamic navigation in Laravel 5
The blade syntax is wrong.
#foreach($nav as $navs)
<li><a>{{$navs->title}}</a></li>
#endforeach
Btw it's more likely that you wanted $navs as $nav and $nav->title.
If you have any problem with the routing part, share your code. Unless, I can only help with this.
In your route (example)
Route::get('page/{title}', 'PageController#details');
In PageController
public function details($title)
{
$page = Page::where('title', $title)->firstOrFail();
return view('your.page')->with(['page' => $page]);
}
In header.blade.php
#foreach($navs as $nav)
<li>{{ $nav->title }}</li>
#endforeach

pagination in laravel freamwork

Hi I use paginate() method for pagination it work fine, but a tag in blade file have problem my link like this:
http://localhost/blog/admin/news/?page=2
and when i click on them i'm redirect :
http://localhost/admin/news?page=2
and get 404 error and when click link like this:
http://localhost/blog/admin/news?page=2
it's work fine. what the problem ?
Why when use http://localhost/blog/admin/news/?page=2
redirect me an other page?
thanks.
I had the same problem. You can use the setPath() method to choose the right path. You can see the usage of that method here http://laravel.com/docs/5.1/pagination#displaying-results-in-a-view

L5 Pagination keeping current url variables when changing pages

So I am trying to paginate a set of results in Laravel 5.
$listings = $query->paginate(16);
Then on my view I'm using this to display the pagination links.
{!! $listings>appends(Request::except('page'))->render() !!}
I want to retain all the GET variables in the URL when changing pages.
When I try to load a page I get this error:
Call to undefined function appends()
I know pagination has been modified in L5 but can't seem to find out how to get it to work.
I don't see the logic for changing pagination from how it was in L4 but that's another story.
Any help would be appreciated!
Thanks!
You are missing the "-" in your method call $listings->appends

How to get the title of a page in Laravel

I set my title of the page like this in the blade view template:
#section('title', 'Example.com - Welcome to Example.com for all your needs')
This is working well. Now I want to know how can I access/print the current page title in the body part?
I mean for getting the current URL, we can use Request::url(). Is there a way that I can get the current page title?
I don't know of a way to do this in Laravel, but this is easily accomplished in Javascript/JQuery:
$(document).ready(function(){
console.log($("title").text());
});
Will print the title of the current page to the console. You can assign this value to an element by targeting it's id $("#id_of_element") or class $(".class_of_elemnt") and setting the text to the title's text:
$("#id_of_element").text($("title").text());
Hope that helps!
I think it's not a good idea to set the page title with hard code in layout file.
one of the best ways is to have a default page title in layout and override it in every view page.
just like below :
layout title :: {{ isset($title) ? $title : 'page title' }}
You can print your page title using
#yield('title)
wherever you want.

Categories