How can I call a method of a controller from blade template and receive the data is returned by the method. For Example:
MeetingRoomController
class MeetingRoomController extends \BaseController
{
public function index()
{
//get all the meeting room
$meetingRoomCountry = MeetingRoom::select('country')->distinct()->orderBy('country')->get();
return View::make('index')->with('meetingroom', $meetingRoomCountry);
}
public function findLocationForNavBar( $country )
{
$meetingRoomLocation = MeetingRoom::select('location')
->distinct()
->where('country', '$country')
->orderBy('country')
->get();
return View::make('index')- >with('meetingroomLocation', $meetingRoomLocation);
}
}
View - index.blade.php
<div id="dropdown-lvl1" class="panel-collapse collapse">
<div class="panel-body">
<ul class="nav navbar-nav">
#foreach($meetingroom as $key => $value)
<li class="panel panel-default" id="dropdown">
<a data-toggle="collapse" href="#dropdown-lvl2">
<span class="glyphicon glyphicon-off"></span> {{$country = $value->country}} <span class="caret"></span>
</a>
<div id="dropdown-lvl2" class="panel-collapse collapse">
<div class="panel-body">
<ul class="nav navbar-nav">
<li>Location</li>
</ul>
</div>
</div>
</li>
#endforeach
</ul>
</div>
</div>
I want to collect the location based on corresponding country. According to my logic, First of all I collected the distinct country then searched the location by findLocationForNavBar method in MeetingRoomController.
If you're using Laravel 5.1, you could try a Service Injection from your blade template.
http://laravel.com/docs/5.1/blade#service-injection
Example from the docs:
#inject('metrics', 'App\Services\MetricsService')
<div>
Monthly Revenue: {{ $metrics->monthlyRevenue() }}.
</div>
You can do all this stuff in the index() method and send location and country to view.
Related
Im getting all the categories like:
public function index(){
return view('home')
->with('categories', Category::orderBy('created_at', 'desc')->get())
->with('conferences', Conference::orderBy('created_at','desc')->where('status','P')->take(8)->get())
}
Then in the blade file I show the categories like below. But do you know how to order the categories by the number of conferences associated with each category? That is, show first the categories that have more conferences associated with it. Do you know how that can be achieved?
<div class=" d-md-block">
<div class="container">
<div class="row">
<div class="col p-0 m-0">
<ul class="Categories__Menu">
#foreach($categories->take(6) as $category)
<li class="">
{{$category->name}}
</li>
#endforeach
<li><a data-toggle="modal" id="showCategories" data-target=".bd-example-modal-lg" href="">More <i class="fa fa-caret-down" aria-hidden="true"></i></a></li>
</ul>
</div>
</div>
</div>
</div>
Use this.
Category::withCount('conferences')->orderBy('conferences_count', 'desc')->get();
I'm busy with my own news system and I'm using Laravel 5. I'm quite new to Laravel and using tutorials to walk my way through it. Now I'm having this error "Undefined variable: articles".
Here is my ArticlesController.php
<?php
namespace App\Http\Controllers;
use App\Article;
use Illuminate\Http\Request;
class ArticlesController extends Controller
{
public function index()
{
return view('pages.news');
}
}
Here is my news.blade.php
#foreach ($articles as $article)
<!-- Post -->
<div class="blog-item">
<!-- Post title -->
<h2 class="blog-item-title font-alt">
{{ $article->title }}
</h2>
<!-- Post data -->
<div class="blog-item-data">
<i class="fa fa-clock-o"></i> 5 December
<span class="seperator"> </span>
<i class="fa fa-user"></i> John Doe
<span class="seperator"> </span>
<i class="fa fa-folder-open"></i>
Category, Category
<span class="seperator"> </span>
<i class="fa fa-comments"></i> 5 Comments
</div>
<!-- Media gallery -->
<div class="blog-media">
<ul class="clearlist content-slider">
<li><img src="images/portfolio/full-project-1.jpg" alt="Blog image"></li>
</ul>
</div>
<!-- Text intro -->
<div class="blog-item-body">
<p>{{ $article->body }}</p>
</div>
<!-- Read more -->
<div class="blog-item-foot">
Read More <i class="fa fa-angle-right"></i>
</div>
</div>
#endforeach
I have tried couple things, such as removing everything and starting over. Nothing really did the trick.
You have to pass the variable to your view, else the view does not "know" the variable. Like this:
<?php
namespace App\Http\Controllers;
use App\Article;
use Illuminate\Http\Request;
class ArticlesController extends Controller
{
public function index()
{
return view('pages.news', ['articles' => Article::all(),]);
}
}
Reference here.
You do not pass any data from the controller to the view, so do something like this:
$articles = Article::get();
return view('pages.news', ['articles' => $articles]);
Or:
return view('pages.news', compact('articles'));
Or:
return view('pages.news')->with('articles', $articles);
https://laravel.com/docs/5.5/views#passing-data-to-views
I´m trying to do a simple blog with Laravel 5.3. Index page include aside with categories and tags, and we can filter different post with categories or tags names.
In routes I have this.
Route::get('categories/{name}',[
'as'=>'front.search.category',
'uses'=>'FrontController#searchCategory'
]);
Route::get('tags/{name}',[
'as'=>'front.search.tag',
'uses'=>'FrontController#searchTag'
]);
In Article
public function scopeSearchCategory($query,$name){
$query->where('name','=',$name);
}
In Tag
public function scopeSearchTag($query,$name){
$query->where('name','=',$name);
}
In FrontController.
public function searchCategory($name){
$tags=Tag::all();
$sliders=Slider::all();
$categories = Category::SearchCategory($name)->first();
$articles = $categories->articles()->paginate(5);
$articles->each(function($articles){
$articles->category;
$articles->images;
});
return view('front.index')->with('articles', $articles)->with('sliders', $sliders)->with('categories', $categories)->with('tags',$tags);
}
public function searchTag($name){
$categories=Category::all();
$sliders=Slider::all();
$tags = Tag::SearchTag($name)->first();
$articles = $tags->articles()->paginate(5);
$articles->each(function($articles){
$articles->category;
$articles->images;
});
return view('front.index')->with('articles', $articles)->with('sliders', $sliders)->with('categories', $categories)->with('tags',$tags);
}
In aside
<aside class="panel panel-primary">
<div class="panel-heading">
<h2 class="panel-title">{{trans('app.title_categories')}}</h2>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($categories as $category)
<a href="{{route('front.search.category',$category->name)}}">
<li class="list-group-item">
<span class="badge">{{$category->articles->count()}}</span>
{{$category->name}}
</li>
</a>
#endforeach
</ul>
</div>
</aside>
<aside class="panel panel-success">
<div class="panel-heading">
<h2 class="panel-title">Tags</h2>
</div>
<div class="panel-body">
<ul class="list-group">
#foreach($tags as $tag)
<a href="{{route('front.search.tag',$tag->name)}}">
<li class="list-group-item">
<span class="badge">{{$tag->articles->count()}}</span>
{{$tag->name}}
</li>
</a>
#endforeach
</ul>
</div>
</aside>
When a click on aside href to filter tags or categories return 'Trying to get property of non-object'. If I remove scopeSearchs and put in FrontController functions $Tag:all() or Category:all() href works, but filter fail, return all post. ¿Any idea why return this error? ¿Can´t do foreach if return one object?
You should use
$tags = Tag::searchTag($name)->first();
When you are using scope function it should always be camelCase
so searchTag() should be start with small s not capital s
You should remove foreach
use this
<a href="{{route('front.search.tag',$tags->name)}}">
<li class="list-group-item">
<span class="badge">{{$tags->articles->count()}}</span>
{{$tags->name}}
</li>
</a>
I have a Users table, a Images table and a Bookmarks table. I've specified the relationships for each one in my database and I am capable of accessing all the images that a user has uploaded. However I cannot access the images they have bookmarked.
This is what my databases relationships look like:
I am able to access the data I want using the SQL input on PHPMyAdmin however I cannot when using Laravel:
Using Laravel yields this error:
This is my User Model:
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
class User extends SentryUserModel implements UserInterface, RemindableInterface {
public $activated = true;
public function getAuthIdentifier() {
return $this->getKey();
}
public function getAuthPassword() {
return $this->password;
}
public function getRememberToken() {
return $this->remember_token;
}
public function setRememberToken($value) {
$this->remember_token = $value;
}
public function getRememberTokenName() {
return 'remember_token';
}
public function getReminderEmail() {
return $this->email;
}
public function images() {
return $this->hasMany('image', 'poster_id', 'id');
}
public function bookmarks() {
return $this->hasMany('bookmark', 'bookmarker_id', 'id');
}
}
This is my Image Model:
<?php
class Image extends Eloquent {
public function poster() {
return $this->belongsTo('user', 'poster_id', 'id');
}
}
This is my Bookmark Model:
<?php
class Bookmark extends Eloquent {
public $timestamps = false;
public function bookmarker() {
return $this->belongsTo('user', 'bookmarker_id', 'id');
}
public function image() {
return $this->hasOne('image', 'image_id', 'id');
}
}
This is the view in which the error is being thrown:
#extends('master')
#section('content')
<!-- Page Content -->
<header class="navbar navbar-inverse" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
Egami
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav navbar-right">
#if(!Sentry::check())
<li>
Login
</li>
<li>
Signup
</li>
#endif
#if(Sentry::check())
#if(Sentry::getUser()->id == $user->id)
<li class="active">
#else
<li>
#endif
Profile
</li>
<li>
Logout
</li>
#endif
</ul>
</div>
</div>
</header>
<div class="container" id="profile">
<div class="row">
<div class="tac col-md-3">
<img src="/img/{{ $user->profile_image }}" alt="{{ $user->username }}'s Profile Image">
</div>
<div class="tal col-md-8">
<h2>{{ $user->username }}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="navbar navbar-inverse" role="navigation">
<ul class="nav navbar-nav navbar-center">
<li>
Uploaded Images
</li>
<li class="active">
Bookmarked Images
</li>
</ul>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="row">
<?php
$bookmarks = $user->bookmarks()->orderBy('id', 'DESC')->simplePaginate(15);
?>
#foreach($bookmarks as $bookmark)
<div class="col-md-4 user-image">
<a href="{{ URL::action('ImageController#show', $bookmark->image()->id) }}">
<img src="/uimg/{{ $bookmark->image()->id }}.png" alt="{{ $bookmark->image()->title }}">
</a>
</div>
#endforeach
</div>
<div class="row">
<div class="container">
{{ $bookmarks->links() }}
</div>
</div>
</div>
</div>
</div>
<footer class="navbar-fixed-bottom">
<div class="container">
<p>
Copyright © Egami {{ date('Y') }}
</p>
</div>
</footer>
#stop
#section('scripts')
#stop
Try this:
<a href="{{ URL::action('ImageController#show', $bookmark->image->id) }}">
<img src="/uimg/{{ $bookmark->image->id }}.png" alt="{{ $bookmark->image->title }}">
</a>
Removed parentheses $bookmark->image()->id to $bookmark->image->id.
I am building a menu on laravel.
In blade layout i have:
#foreach ($menu1 as $menu1)
{{$menu1->nome}}
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
<span class="glyphicon glyphicon-chevron-down"></span>
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<ul class="list-unstyled">
#foreach ($menu2 as $menu2)
<li> {{$menu2->nome}} </li>
#endforeach
</ul>
</div>
</div>
</div>
#endforeach
in my controller i pass 2 query.
public function menu($lingua)
{
return View::make('index', ['menu1'=>DB::table('cat_nome')->join('lingua', 'cat_nome.id_lingua', '=', 'lingua.id') ->where('lingua.lingua','=',$lingua)->get(array('cat_nome.nome')),
'menu2' => DB::table('campo_nome')->join('lingua', 'campo_nome.id_lingua', '=', 'lingua.id') ->where('lingua.lingua','=',$lingua)->get(array('campo_nome.nome'))]
);
}
I tried to UNnest the foreach and everything works fine. When i try to nest the foreach i get
Trying to get property of non-object
#foreach ($menu2 as $menu2)
That line is most likely the culprit. You cannot use the same variable name twice in a foreach loop like that and expect good results. The second one is supposed to be what each entry in $menu2 will be scoped to during each iteration of the loop.
See the foreach documentation
Try changing the loop to look more like this:
#foreach ($menu2 as $innerMenu)
<li> {{$innerMenu->nome}} </li>
#endforeach
I would suggest doing the same thing for your outer menu as well.