I'm trying to make tags filter on my page, but I have an error says Call to a member function links() on null.
Thank you for anything you can do to help.
This is my controller:
<?php
public function index(Request $request)
{
if($request->has('cari')) {
$forums = \App\Forum::where('title','LIKE','%'.$request->cari.'%')->paginate(5);
} elseif($request->has('tags')) {
$tag = Tag::find($request->tags);
$forums = $tag->forums;
} else {
$forums = Forum::paginate(5);
}
$populars = DB::table('forums')
->join('comments','forums.id','=','comments.commentable_id')
->select(DB::raw('count(commentable_id) as count'),'forums.id','forums.title','forums.slug')
->groupBy('id','title','slug')
->orderBy('count','desc')
->take(5)
->get();
return view('forum.index', compact('forums','populars'));
}
And this is my link in view:
<div class="row justify-content-center">
{!! $forums->links() !!}
</div>
This is my forum model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class forum extends Model
{
public function tags()
{
return $this->belongsToMany('App\Tag');
}
public function user()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
Related
User.php
public function roles()
{
return $this->belongsToMany(Role::class)->withTimestamps();
}
public function hasRole($role)
{
if($this->roles()->where('name',$role)->first())
return true;
else
return false;
}
public function teams()
{
return $this->belongsToMany(Team::class)->withTimeStamps();
}
Team.php
public function users()
{
return $this->belongsToMany(User::class)->withTimeStamps();
}
Role.php
public function users()
{
return $this->belongsToMany(User::class)->withTimestamps();
}
Now i want to get all users that are not engaged with any teams and whose role is member
Take a look at this helper:
https://laravel.com/docs/5.8/eloquent-relationships#querying-relationship-absence
$users = User::doesntHave('teams')->get();
-- EDIT
$users = User::doesntHave('teams')->whereHas('roles', function($query) {
$query->where('name', 'member');
})->get();
I have filters which belong to filter groups
Filter.php:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Filter extends Model
{
public function group()
{
return $this->belongsTo('App\FilterGroup');
}
public function products()
{
return $this->belongsToMany('App\Product', 'product_filters');
}
public function categories()
{
return $this->belongsToMany('App\Filter', 'category_filter');
}
}
And categories which have many to many relationship with the filters
Category.php:
namespace App;
use App\Events\CategoryDelete;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $events = [
'deleting' => CategoryDelete::class
];
protected $table = 'categories';
public function parent()
{
return $this->belongsTo('App\Category', 'parent_id');
}
public function children()
{
return $this->hasMany('App\Category', 'parent_id');
}
public function parents()
{
$parentCategories = collect([]);
$parent = $this->parent;
while (!is_null($parent)) {
$parentCategories[] = $parent;
$parent = $parent->parent;
}
return $parentCategories->reverse();
}
public function products()
{
return $this->hasMany('App\Product');
}
public function filters()
{
return $this->belongsToMany('App\Filter', 'category_filter');
}
public function hasFilter($filter_id)
{
foreach ($this->filters as $filter) {
if ($filter->id == $filter_id) {
return true;
}
}
return false;
}
public function getFilterGroups()
{
$filterGroups = collect([]);
foreach ($this->filters as $filter) {
if (!$filterGroups->has($filter->group->id)) {
$filterGroups[$filter->group->id] = $filter->group;
}
}
return $filterGroups;
}
}
And in the category view I want to display the filters along with their filter group but when I try the following:
#foreach($category->filters as $filter)
{{ $filter->group->name }}
<br>
#endforeach
It throws the Trying to get property of non-object exception
Why can't I get the group of the filter?
I changed the group() method inside the Filter model to filterGroup and now when I call $filter->filterGroup it works fine. I dont know why, maybe group is some reserved word or something.
I have 3 models: Article, Comment, Reaction.
Each article has many comments, and each comment has many reactions:
App\Article:
class Article extends Model
{
public function comments() {
return $this->hasMany('App\Comment');
}
}
App\Comment:
class Comment extends Model
{
public function article() {
return $this->belongsTo('App\Article');
}
public function reactions() {
return $this->hasMany('App\Reactions');
}
}
App\Reaction:
class Reaction extends Model
{
public function comment() {
return $this->belongsTo('App\Comment');
}
}
In my ArticleController#index I want to fetch comments and their reactions:
ArticleController:
public function index()
{
$articles = Article::with('comments')
->select('articles.*')
->leftjoin('comments', 'comments.article_id', '=', 'articles.id')
->get();
return view('wiki')->withArticles($articles);
}
I can loop through the comments ($article->comments), however I'm not sure how to do a with('reactions') for the comments? i.e.,
#foreach($article->comments as $comment)
#foreach($comment->reactions)
// something like this...
#endforeach
#endforeach
you can do Nested Eager Loading
$article = Article::with('comments.reactions')
->leftjoin('comments', 'comments.article_id', '=', 'articles.id')
->select('articles.*')
->get();
You can also do this way
ArticleController:
public function index()
{
$articles = Article::with('comments')
->select('articles.*')
->get();
return view('wiki')->withArticles($articles);
}
App\Article:
class Article extends Model
{
public function comments() {
return $this->hasMany('App\Comment')->with('reactions');
}
}
App\Comment:
class Comment extends Model
{
public function article() {
return $this->belongsTo('App\Article');
}
public function reactions() {
return $this->hasMany('App\Reactions');
}
}
I Followed a tutorial with this source code:
https://github.com/laracasts/Dedicated-Query-String-Filtering/tree/master/app
If you have laracasts you can watch the video here
What i like to achieve is filter products based on their category.
When i filter on the product itself , it works fine
class ProductFilter extends QueryFilter
{
public function categorie($name)
{
return $this->builder->where('name' , $name);
}
}
But when i try to filter on the relationship it doens't work. (i get no errors either) . The error is located in this file , i think
class ProductFilter extends QueryFilter
{
public function categorie($name)
{
return $this->builder->categories()->where('name' , $name);
}
}
View
<form method="get" action="/producten/categorie" style="display:inline-block">
#foreach($roots as $root)
<li><button type="submit" name="categorie" value="{{$root->name}}" class="button-link">{{$root->name}}</button></li>
#endforeach
</form>
Route
Route::get('producten/categorie' , 'FrontProductController#index');
FrontProductController
public function index(ProductFilter $filters)
{
Product::filter($filters)->get();
}
QueryFilter class
abstract class QueryFilter
{
protected $request;
protected $builder;
public function __construct(Request $request)
{
$this->request = $request;
}
public function apply(Builder $builder)
{
$this->builder = $builder;
foreach ($this->filters() as $name => $value) {
if (! method_exists($this, $name)) {
continue;
}
if (strlen($value)) {
$this->$name($value);
} else {
$this->$name();
}
}
return $this->builder;
}
public function filters()
{
return $this->request->all();
}
}
Product Model
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
public function scopeFilter($query, QueryFilter $filters)
{
return $filters->apply($query);
}
In the product filter i need to do the following for many-to-many relationships:
public function category($name)
{
return $this->builder->whereHas('categories', function ($query) use ($name) {
$query->where('name', $name);
});
}
What is the best way I could replace /controller/method/id in the URL with just /slug?
For example: trips/1 would become /honduras-trip
This is what I am doing but couldn't their be a better way?
My routes:
Route::get('/{slug}', 'HomeController#show');
My Controller:
public function show($slug)
{
$class = Slug::where('name', '=', $slug)->firstOrFail();
if($class->slugable_type == 'Trip')
{
$trip = Trip::find($class->slugable_id);
return $trip;
}
if($class->slugable_type == 'Project')
{
$project = Project::find($class->slugable_id);
return $project;
}
if($class->slugable_type == 'User')
{
$user = User::find($class->slugable_id);
return $user;
}
}
My Slug Model:
class Slug extends Eloquent {
public function slugable()
{
return $this->morphTo();
}
}
The other models all have this method:
public function slugs()
{
return $this->morphMany('Slug', 'slugable');
}
In your routes.php just give
Route::get('slug', array('uses' => 'HomeController#show'));
In your controller, write show() function
public function show() {
return View::make('welcome');
}
In your view give,
<li>slug</li>