Laravel get profile name - php

I'm laravel newbie and I'm making simple CMS. So i have simple question:
I wan't when click on avatar - redirect to user profile (http://example.com/profiles/nickname/id)
In DB it saves that:
as you see author_id I have, now I need to get author name from users table:
And then generate url: http://example.com/profiles/Evaldas/2 (Evaldas, because author_id is 2 in topics table)
My routes file:
Route::get('topic/{tname}/{tid}', 'viewTopic#showTopic');
My viewTopic.php Controller:
<?php
namespace App\Http\Controllers;
use DB;
use View;
class viewTopic extends Controller
{
public function showTopic($tname, $tid)
{
return View::make('posts', [
'topics' => DB::table('topics')
->where('id', $tid)
->where('seo_title', $tname)
->first(),
'posts' => DB::table('posts')
->where('topic_id', $tid)
->select()
->get()
]);
}
}
And layout:
#extends('layouts.main')
#section('content')
<div class="media">
<div class="media-left">
<a href="HERE MUST BE HREF TO PROFILE">
<img class="media-object" src="http://localhost/uploads/avatars/2.jpg" style="width: 64px">
</a>
</div>
<div class="media-body" rel="#author{{ $topics->author_id }}">
<h4 class="media-heading">{{ $topics->title }}</h4>
#if(!empty($topics->text))
{{ $topics->text }}
#else
Message empty :(
#endif
</div>
#foreach($posts as $post)
<div class="media">
<div class="media-left">
<a href="HERE MUST BE HREF TO PROFILE">
<img class="media-object" src="http://localhost/uploads/avatars/1.png" style="width: 64px">
</a>
</div>
<div class="media-body" rel="#post{{ $post->pid }}">
{{ $post->text }}
</div>
</div>
#endforeach
</div>
#stop
Thanks so much in advance :)

If you want to use the Query Builder, you can use the join() method:
DB::table('posts')
->where('topic_id', $tid)
->join('users', 'user.id', '=', 'posts.author_id')
->select()
->get()
This way, the user information will be available:
$post->nickname
You can then build your URL using a laravel helper, for exemple, if you have a profile route:
<a href="{{route('profile', ['nickname' => $post->nickname, 'id' => $post->author_id]);}}">
An other method is to create models for your tables and define relationship between them.
See this: http://laravel.com/docs/5.1/eloquent-relationships
Using this method, you will be able to write things like $post->author->nickname

Related

Laravel Collection get one of values by key

I'm new to Laravel and I'm trying to get one of the values in one collection by one ID retrieved from another collection.
My function returns 2 Collections:
public function index()
{
$category = BlogCategory::all(['id', 'name']);
$post = BlogPost::orderBy('id', 'desc')->take(14)->get();
return view('blog.index', ['posts' => $post], ['categories' => $category]);
}
In a foreach loop I'm getting values from collections:
#foreach($posts as $post)
#if($loop->iteration > 2)
<div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">
<a href="#">
<img src="#" alt="">
<p class="post-category">{{ $categories->get($post->category_id) }}</p>
<h5 class="post-title">{{ $post->title }}</h5>
</a>
</div>
#endif
#endforeach
I'm partially getting the result as you can see in the image below, but I want to get only the name.
Here is the code that I'm trying to figure out
{{ $categories->get($post->category_id) }}
If there is a better or correct way to do it, let me know.
Blog Posts migration:
public function up()
{
Schema::create('blog_posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->longText('content');
$table->mediumText('slug');
$table->bigInteger('author_id')->unsigned();
$table->bigInteger('category_id')->unsigned();
$table->timestamps();
$table->foreign('author_id')->references('id')->on('blog_authors');
$table->foreign('category_id')->references('id')->on('blog_categories');
});
}
You should put in place the relationships between BlogPost and BlogCategory models, seen that you already have a category_id field in BlogPost model, i.e.:
in BlogPost Model:
public function category(){
return $this->belongsTo(\App\BlogCategory::class);
}
in BlogCategory Model:
public function posts(){
return $this->hasMany(\App\BlogPost::class);
}
Next you can eager load categories with the $posts in your controller with only two queries:
public function index()
{
$posts = BlogPost::with('category')->orderBy('id', 'desc')->take(14)->get();
return view('blog.index', compact('posts'));
}
Then in your view you can access each $post->category object directly because eager loaded in the controller:
#foreach($posts as $post)
#if($loop->iteration > 2)
<div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">
<a href="#">
<img src="#" alt="">
<p class="post-category">{{ $post->category->name }}</p>
<h5 class="post-title">{{ $post->title }}</h5>
</a>
</div>
#endif
#endforeach
It could be optimized, first, you need to make relation one to many from categories to posts
first: make sure that you have in posts migrations category_id column
Second: Open Category Model and write this method this will allow you to fetch posts that belong to the category
public function posts(){
return $this->hasMany(\App\Post::class);
}
Third: open shop model and write this method this will allow you to fetch category that belongs to the post
public function catgeory(){
return $this->belongsTo(\App\Category::class);
}
Finally: you will edit your view like this one
#foreach($posts as $post)
<div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">
<a href="#">
<img src="#" alt="">
<p class="post-category">{{ $post->category->title }}</p>
<h5 class="post-title">{{ $post->title }}</h5>
</a>
</div>
#endforeach
and of course, you wouldn't call categories in your controller anymore
public function index()
{
$post = BlogPost::orderBy('id', 'desc')->take(14)
->with('category')->get();
return view('blog.index', ['posts' => $post]);
}
Maybe it better use a relation to drive this situation. You can load in the controller the data in this way.
In the Model Post:
function categories(){
return $this->belongsToMany('App\BlogCategory')->select(array('id', 'name');
}
Maybe is hasOne -> the relationship if you don't have a table pivot...
In the Controller:
public function index()
{
$data['posts'] = BlogPost::orderBy('id', 'desc')->take(14)->with('categories')->get();
return view('blog.index', $data);
}
In the View:
#foreach($posts as $post)
#if($loop->iteration > 2)
<div class="col-sm-6 col-md-3 d-flex justify-content-center other-post">
<a href="#">
<img src="#" alt="">
<p class="post-category">{{ $post->categories->name }}</p>
<h5 class="post-title">{{ $post->title }}</h5>
</a>
</div>
#endif
#endforeach
this line $categories->get($post->category_id) return for you an array of category, so the solution for you here is just like bellow:
{{ $categories->get($post->category_id)['name'] }}

ErrorException Undefined variable when trying to show data in view

So i was trying to display a record from a database with laravel, and i have defined the variable in #foreach statement but when i run it it shows ErrorException Undefined variable , although all variable already inside the foreach statement, am i missing a method function in my controller?
this is the view
welcome.blade.php
<div class="blog-item">
<div class="blog-text text-box text-white">
#foreach ($guestbooks as $guestbook)
<div class="top-meta">{{ Carbon\Carbon::parse($guestbook->created_at)->format('d-m-Y') }} / di Rakitan</div>
<h3>{{ $guestbooks->name }}</h3>
<p>{!! \Illuminate\Support\Str::words($guestbook->message, 50, '...') !!}</p>
Lanjutkan Baca <img src="asset/img/icons/double-arrow.png" alt="#"/>
</div>
</div>
<!-- Blog item -->
<div class="blog-item">
<div class="blog-text text-box text-white">
<div class="top-meta">{{ Carbon\Carbon::parse($guestbook->created_at)->format('d-m-Y') }} / di Rakitan</div>
<h3>{{ $guestbook->name }}</h3>
<p>{!! \Illuminate\Support\Str::words($guestbook->message, 50, '...') !!}</p>
Lanjutkan Baca <img src="asset/img/icons/double-arrow.png" alt="#"/>
</div>
</div>
<!-- Blog item -->
<div class="blog-item">
<div class="blog-text text-box text-white">
<div class="top-meta">{{ Carbon\Carbon::parse($guestbook->created_at)->format('d-m-Y') }} / di Rakitan</div>
<h3>{{ $guestbook->name }}</h3>
<p>{!! \Illuminate\Support\Str::words($guestbook->message, 50, '...') !!}</p>
Lanjutkan Baca <img src="asset/img/icons/double-arrow.png" alt="#"/>
#endforeach
</div>
</div>
this is the controller
GuestbookController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Guestbook;
class GuestbookController extends Controller
{
public function index()
{
$guestbooks = Guestbook::get();
return view('post.post_textarea',[
'guestbooks' => $guestbooks
]);
}
public function store(Request $request)
{
Guestbook::create([
'name' => $request->name,
'message' => $request->message
]);
return redirect()->back();
}
}
and this is the routes
Route::get('/posting','GuestbookController#index')->name('guestbook');
Route::post('/posting','GuestbookController#store')->name('guestbook.store');
Your GuestbookController function index() is returning a view called post.post_textarea and passing your $guestbook variable to that view, while you are trying to get that variable in your welcome.blade.php.
Change your index function to return welcome view like this:
public function index()
{
$guestbooks = Guestbook::get();
return view('welcome',[
'guestbooks' => $guestbooks
]);
}
i figured the problem was that there are two routes routing into the same url as Route::get('/posting','GuestbookController#index')->name('guestbook'); so i delete the other one and it works thanks about that typo tough #Denis Ćerić

Laravel 5.7 Comment count for different posts

I am stucked at point where i got comment count for posts but it shows count of all comments of all posts on every post. I would like to know how to output in blade comment count for post ID
here is controller:
$posts = $posts->orderBy("posted_at", "desc")
->paginate(config("blogetc.per_page", 10));
$comments = BlogEtcComment::all();
return view("blogetc::index", [
'posts' => $posts,
'title' => $title,
'comments' => $comments,
]);
blade:
#foreach($posts as $post)
<section class="blog_area p_120">
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="blog_left_sidebar">
<article class="blog_style1">
<div class="blog_img">
<img class="img-fluid" src="blog_images/{{$post->image_large}}" alt="">
</div>
<div class="blog_text">
<div class="blog_text_inner">
<div class="cat">
<a class="cat_btn" href="{{$post->url()}}">{{$post->slug}}</a>
<i class="fa fa-calendar" aria-hidden="true"></i>{{$post->created_at}}
<i class="fa fa-comments-o" aria-hidden="true"></i> {{count($comments)}}
</div>
<h4>{{$post->title}}</h4>
<p>{!! $post->generate_introduction(400) !!}</p>
<a class="blog_btn" href="{{$post->url()}}">Lasīt vairāk</a>
</div>
</div>
</article>
</div>
</div>
</div>
</div>
</section>
#endforeach
//Quickfix:
//assuming your posts table is called posts and that in your blogetccomments table //you have a post_id column pointing to the original post. Try something like
$posts = DB::table('posts')
->leftJoin('blogetccomments', 'posts.id', '=', 'blogetccomments.post_id')
->selectRaw('posts.*, count(blogetccomments.post_id) as commentcount')
->groupBy('posts.id')
->get();
In your blade template, Access the comments count for each post, as follows..
#foreach($posts as $post)
...
{{$post->title}}...
{{$post->commentcount}}
...
#endforeach
You can use withCount() to get count of comments for a specific post :
$posts = $posts->withCount('comments')
->orderBy("posted_at", "desc")
->paginate(config("blogetc.per_page", 10));
Above would require you to have comments relation on your Post Model :
Post Model :
public function comments()
{
return $this->hasMany(BlogEtcComment::class);
}
BlogEtcComment Model :
public function post()
{
return $this->belongsTo(Post::class);
}
And then in blade :
#foreach($posts as $post)
<p>Post : $post->id</p>
<p>Comments : $post->comments_count</p>
#endforeach

Laravel search for topic by title and generate link

I'm laravel newbie. I have something like this:
Now I wan't to make when click first topic redirect to that topic url. For that I need to use DB:: function. I wan't search by given title, and then get id, etc.. For example i have posts:
table name same as topic title in image above, so I need use DB::select or ::table and search by that name, then print results. How to?
Layout:
#extends('layouts.main')
#section('content')
<div class="panel panel-default">
<div class="panel-heading">Forum</div>
<div class="panel-body">
#forelse($forums as $forum)
#forelse($topics as $topic)
{{ $topic->title }}<br>
#empty
<div class="alert alert-danger">Apgailėstaujame tačiau šis forumas yra tuščias!</div>
#endforelse
#empty
<div class="alert alert-danger">Apgailėstaujame tačiau forumas nerastas</div>
#endforelse
</div>
</div>
{!! $topics->render() !!}
#stop
Thanks in advance ;(
UPDATED
viewForum Controller:
<?php
namespace App\Http\Controllers;
use DB;
use View;
class viewForum extends Controller
{
public function showForum($fname, $fid)
{
$forums = DB::table('forums')
->where('id', $fid)
->where('seo_name', $fname)
->select()
->get();
$topics = DB::table('topics')
->where('forum_id', $fid)
->select()
->paginate(1);
return View::make('forum', compact('forums', 'topics'));
}
}
In your topic href do this:
{{ $topic->title }}<br>
In your routes.php add this
Route::get('topic/{fname}/{fid}', 'viewForum#showForum');

eloquent scope to eager load with constriant?

Ok so I'm trying to setup a scope that will restrict which users are returned based on a column in a hasOne relation.
Here are the two methods from my User model:
public function scopeHasImage($query)
{
return $query->with(['profile' => function($query){
$query->where('avatar', '!=', '');
}]);
}
public function profile()
{
return $this->hasOne('App\Profile');
}
When I call it like so:
$users = User::HasImage()->simplePaginate(15);
All of that works ok untill I go to display the code in my blade template like so:
#foreach($users as $user)
<div class="col-xs-12 col-sm-3 col-md-2">
<a href="{{ route('user.profile.public', ['id' => $user->id]) }}">
<img class="img-responsive" src="{{ $user->profile->avatar }}" />
</a>
</div>
#endforeach
That results in this error:
Trying to get property of non-object
I have dumped the $user->profile and it is an object which has attributes listed and one of those attributes is avatar. So I'm not sure why this is not working.
Any thoughts would be greatly appreciated.
Almost certainly one of your users has no profile attached. Because you probably dumped only the the first $user->profile you didn't see that...
You can fix this by wrapping it in an if:
#foreach($users as $user)
#if($profile = $user->profile)
<div class="col-xs-12 col-sm-3 col-md-2">
<a href="{{ route('user.profile.public', ['id' => $user->id]) }}">
<img class="img-responsive" src="{{ $profile->avatar }}" />
</a>
</div>
#endif
#endforeach
Or exclude users without a profile in the first place (which is probably what you wanted all along)
public function scopeHasImage($query)
{
return $query->with('profile')->whereHas('profile', function($query){
$query->where('avatar', '!=', '');
});
}

Categories