I have problem with displayin one picture in my home page.
Currently, I loaded some photos. I created a column in the database 'thumbnail'. Wants to do so, to load the photo - thumbnail where the column 'thumbnail
have record = 1.
My Homecontroller:
public function getHome(){
return View::make('pages.home')
->with('offers', Offer::take(8)->orderBy('created_at', 'DESC')->get());
}
my view:
#foreach($offers as $offer)
<div class="col-sm-3" style="border: 1px solid #AADFFF; height:350px; ">
#foreach($offer->photosofoffers as $photosofoffer)
<a href="{{ route('pages.view', $offer->id) }}">
<!--<a href="/pages/view/{{ $offer->id }}">-->
{{ HTML::image($photosofoffer->file, $photosofoffer->title, array( 'class'=>'feature', 'width'=>'240', 'height'=>'127')) }}
</a>
#endforeach
<h3><a href="{{ route('pages.view', $offer->id) }}">{{ $offer->title }}</h3>
<p> {{ $offer->description }} </p>
<h5>Availability: <span class="{{ Availability::displayClass($offer->availability) }}">
{{ Availability::display($offer->availability) }}
</span>
</h5>
<p>Cena <span>{{ $offer->price }}</span>
</p>
#endforeach
Where should I add something?
At the moment loads all the images of the announcement. He wants to load one image, where the 'thumbnail' = 1.
You can eager load the relations and apply a constraint. Something like:
Offer::with(array('photosofoffers' => function($q) {
$q->where('thumbnail', 1)->take(1);
}))->take(8)->orderBy('created_at', 'DESC')->get();
http://laravel.com/docs/5.0/eloquent
If you want to load only those offers that have a photo/thumbnail
Offer::with('photosoffers')->whereHas('photosofoffers', function($q) {
$q->where('thumbnail', 1)->take(1);
})->take(8)->orderBy('created_at', 'DESC')->get();
Related
I have followed Jeffery Way in Laravel 8 from scratch amazing series but I'm having a trouble in pagination. In the index page we're making our latest post to have a specific styling and then first 2 posts comes after with a different styling and then rest of posts in different styling. The problem is that these styles are carrying over with us to page2 and 3 and ...etc
I want to make this to only posts in page1 or homepage and when I go to page 2 I should have a default styling for all posts.
This is the index
<x-bloglayout>
#include('posts.__header')
#if ($posts->count())
<x-featuredCard :post="$posts[0]" />
#if ($posts->count() > 1)
<div class="lg:grid lg:grid-cols-2">
<x-postCard :post="$posts[1]" />
<x-postCard :post="$posts[2]" />
</div>
<div class="lg:grid lg:grid-cols-3">
#foreach ($posts->skip(3) as $post)
<x-postCard :post="$post" />
#endforeach
</div>
#endif
{{ $posts->links() }}
#else
<p class="text-center">No posts matches your search, please check back later</p>
#endif
</x-bloglayout>
I tried to use if directive to say if route is home or ?/page1 do this if not do that but it doesn't seem to work.
This is my pagination:
public function index()
{
return view('posts.index', [
'posts' => Post::latest()->filter(request(['search', 'category', 'author']))->paginate(6)->withQueryString(),
]);
}
Thanks
You can use currentPage() method on paginator and check if it's on first page. May not be fanciest way but here is an example:
<x-bloglayout>
#include('posts.__header')
#if ($posts->count())
<x-featuredCard :post="$posts[0]" />
#if ($posts->count() > 1)
#if($posts->currentPage() == 1)
<div class="lg:grid lg:grid-cols-2">
<x-postCard :post="$posts[1]" />
<x-postCard :post="$posts[2]" />
</div>
#endif
<div class="lg:grid lg:grid-cols-3">
#foreach ($posts->skip($posts->currentPage() == 1 ? 3 : 0) as $post)
<x-postCard :post="$post" />
#endforeach
</div>
#endif
{{ $posts->links() }}
#else
<p class="text-center">No posts matches your search, please check back later</p>
#endif
</x-bloglayout>
You can check Paginator methods from Laravel 8.x document: https://laravel.com/docs/8.x/pagination#paginator-instance-methods
Here are my routes:
Route::get('/admin/job-seeker/search/employer/{employerId}', 'AdminJobSeekerSearchController#show')->name('admin.job-seeker.search.employer.show')->middleware('verified');
Route::get('/admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}', 'AdminEmployerJobPostsController#show')->name('admin.employer.post-a-job.show')->middleware('verified');
Controller for first Route:
public function show($employerId)
{
$user = Auth::user();
$jobSeekerProfile = JobSeekerProfile::all()->where('user_id', $user->id)->first();
$employerProfiles = EmployerProfile::limit(1)->where('id', $employerId)->get();
$jobPosts = JobPosts::all();
return view('admin.job-seeker.search.employer.show', compact('employerProfiles', 'id', 'jobSeekerProfile', 'jobPosts'));
}
Controller for second route:
public function show($employerId, $jobPostId)
{
$user = Auth::user();
$jobSeekerProfile = JobSeekerProfile::all()->where('user_id', $user->id)->first();
$employerProfiles = EmployerProfile::limit(1)->where('id', $employerId)->get();
$jobPosts = JobPosts::all();
$jobPost = JobPosts::findOrFail($jobPostId);
return view('admin.employer.post-a-job.show', compact('jobSeekerProfile', 'employerProfiles', 'jobPosts', 'jobPost'));
}
I have two tables employer_profiles and job_posts. After an Employer Registers and creates a profile, he can create job posts with 2 fields job_title and job_description. For example it could be Project Manager and then a description for this role.
I'm getting this error:
Missing required parameters for [Route:
admin.employer.post-a-job.show] [URI:
admin/job-seeker/search/employer/{employerId}/post-a-job/{jobPostId}].
(View:
C:\xampp\htdocs\highrjobs\resources\views\includes\job_seeker_search_employers.blade.php)
(View:
C:\xampp\htdocs\highrjobs\resources\views\includes\job_seeker_search_employers.blade.php)
job_seeker_search_employees.blade:
#if($employerProfiles)
#foreach($employerProfiles as $employerProfile)
<br><br>
<div class="col-md-6 offset-md-3">
<div class="card" style="width: 28rem;">
<img class="card-img-top" src="https://via.placeholder.com/400" alt="Card image cap" style="max-height:400px;">
<div class="card-body">
<h1 class="card-title text-uppercase" style="color: #5dad07;"><strong>{{ $employerProfile['immediate_contact'] }}</strong></h1>
<h3><strong>Positions Available:</strong><br>
#if($jobPosts->where('user_id', $employerProfile->user_id)->count() > 0)
#foreach($jobPosts->where('user_id', $employerProfile->user_id) as $jobPost)
<h5>{{ $jobPost['job_title'] }}</h5>
#endforeach
#else
<h5>No Positions Available</h5>
#endif
</h3>
<h3><strong>Contact Details:</strong></h3>
<p>
<strong>Company Name:</strong> {{ $employerProfile['company_name'] }}<br>
<strong>Contact Email:</strong> {{ $employerProfile['email'] }}<br>
<strong>Phone:</strong> {{ $employerProfile['company_phone'] }}
</p>
<strong>Address:</strong>
<address>{{ $employerProfile['company_address'] }}</address>
</div>
</div>
</div>
<br><br><br>
#endforeach
#endif
Just edit your foreach loop, you have to pass an array as options to rout using route param name as array key:
#foreach($jobPosts->where('user_id', $employerProfile->user_id) as $jobPost)
<h5>{{ $jobPost['job_title'] }}</h5>
#endforeach
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
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', '!=', '');
});
}
I'm trying to figure out how I can apply different css styling to my index.blade.php page depending on whether the $category is active.
I.e If there is no category specified in the url then the image would be small and if there was a category in the url the image would be large.
Route.php
Route::get('blog/{category}', ['as' => 'blog.category', 'uses' => 'BlogController#getCategory']);
blogcontroller.php
public function getCategory($category = null)
{
// get all the blog stuff from database
// if a category was passed, use that
// if no category, get all posts
if ($category)
$posts = Post::where(function($query) use($category){
$query->where('category','=', $category );
}) ->get();
else
$posts = Post::all();
// show the view with blog posts (app/views/blog.blade.php)
return View::make('blog.index')
->with('posts', $posts);
}
}
index.blade.php
#foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<h2>{{ $post->img }}</h2>
<p>{{ $post->name }}</p>
<p>{{ $post->category }}</p>
#endforeach
You can change:
return View::make('blog.index')
->with('posts', $posts);
into:
return View::make('blog.index')
->with('posts', $posts)
->with('category', $category);
And now in Blade:
#foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<h2>
#if (is_null($category))
{{ $post->img }} - here somehow you need to display small image using image width/height or thumbnail
#else
{{ $post->img }}
#endif
</h2>
<p>{{ $post->name }}</p>
<p>{{ $post->category }}</p>
#endforeach
If your css is internal css so you can use the below method:
<style>
#if(your condition)
.container-wizard {
width:100%;
position: relative;
margin: 0 auto;
}
#else
.container-wizard {
width:83%;
position: relative;
margin: 20 auto;
}
#endif
</style>
If your css is external you can use the same method by reference links:
#if(your condition)
<link href="/assets/aos.css" rel="stylesheet">
#else
<link href="/assets/vendor/style.css" rel="stylesheet">
#endif
Moreover for your category you can add flag.