Previous / next page links using Laravel - laravel-4

I am developing a small portfolio site and i am stuck for a second.
So users are allowed to upload their sort them ( with jQuery sortable )
So when other users view his / her portfolio it is displayed by sort order, and when you click on the image it shows a big image.
Here i would like a Previous / next navigation so people can navigate.
It works but i have a problem, so currently in my database the image with the 8 is the last one, but because the images are displayed by sort order, and the image with id 8 which is the last is in the fifth place it stops returning the next id, and it should be there.
Here is what i tried.
Controller
public function show($id)
{
$photo = $this->photo->find($id);
if(is_null($photo)) return App::abort('404');
$previous = $this->photo->where('id', '<', $photo->id)->where('user_id', '=', $photo->user_id)->orderBy('id', 'DESC')->first();
$next = $this->photo->where('id', '>', $photo->id)->where('user_id', '=', $photo->user_id)->orderBy('sort', 'ASC')->first();
$this->layout->title = "Saját képek";
$this->layout->content = View::make('photo::show')
->with('photo', $photo)
->with('previous', $previous)
->with('next', $next);
}
View
<div class="ui page grid">
<div class="ui grid">
<div class="wide column">
<div class="ui segment">
<div class="photo-viev-nav">
#if(!empty($previous->id))
<i class="ui left icon"></i> Vissza
#endif
<strong>{{ $photo->user->name() }} potfóliója</strong>
#if(!empty($next))
Következő <i class="ui right icon"></i>
#endif
</div>
<div class="photo-view">
{{ HTML::image($photo->photoOriginal(), '', array('class' => 'ui huge image')) }}
</div>
</div>
</div>
</div>
The second i tried is this query
$previous = $this->photo->where('id', '<', $photo->id)->where('user_id', '=', $photo->user_id)->orderBy('sort', 'ASC')->max('id');
$next = $this->photo->where('id', '>', $photo->id)->where('user_id', '=', $photo->user_id)->orderBy('sort', 'ASC')->min('id');
But the problem with this it ignores the sort order
Could please someone give me a hint?

Experimented more, and i was a bit silly, instead of focusing on the id i needed to focus on the sort order and get the id's based on that way
Correct query
$previous = $this->photo->where('sort', '<', $photo->sort)->where('user_id', '=', $photo->user_id)->orderBy('', 'DESC')->first();
$next = $this->photo->where('sort', '>', $photo->sort)->where('user_id', '=', $photo->user_id)->orderBy('sort', 'ASC')->first();

Related

How to check date validation with Carbon in Laravel 8.*

I've got index page on which I'm displaying events.
#foreach($events as $event)
<div class="row">
<div class="d-flex justify-content-center" style="margin: auto">
<a href="{!! route('eventView', ['id' => $event->id]) !!}" style="text-decoration: none;">
<div class="row" style="margin-top: 10 px">
<p style="display:block">{{$event->name}}</p>
</div>
#if($event->photo_patch)
<img src="{{ asset('storage/' . $event->photo_patch) }}">
#else
<img src="placeholder" alt="...">
#endif
</a>
</div>
</div>
</div>
#endforeach
But the problem is that its displaying all events, even those which took place in the past.
I would like to display events only with today's or future date
So I'm stuck now and I dont know what to do next.
I tried to do loop which shows me events that are today/in the future:
public function index()
{
$date = Carbon::today();
$events = Event::where('date_of_event' >= $date)->get();
foreach($events as $event){
if($event->date_of_event >= $date){
dump('Valid');
}
else{
dump('Invalid');
}
}
dump($events);
dump($date);
return view('frontend/index',['events'=>$events]);
}
But I dont know how to display them later inside of my view.
My second shot was to do it with this line:
$events = Event::where('date_of_event' >= $date)->get();
But then I've got error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select * from `events` where `1` is null)
which isnt really accurate since column name should be valid.
How can I implement that properly?
The default operator for where is =. If you want to change this you'll use the second argument for the operator and the third for your value.
Event::where('date_of_event', '>=', $date)->get();
See Where Clauses for more details.
To make that code short you may use this code
Event::whereDateOfEvent('>=', $date)->get();

Undefined index: id Laravel 5.8

My Tables:
kategoris table
id | kode_kategori | kategori_name |
items table
id | kategori_id | item_name
In items table the kategori_id column has foreignkey.
My Controller:
public function edit($id)
{
// $item = Item::findOrFail($id);
$item = DB::table('items')
->join('kategoris', 'items.kategori_id', '=', 'kategoris.id')
->where('items.id', '=', $id)
->select('items.*', 'kategoris.*', 'items.id', 'items.kategori_id')
->get();
// dd($item);
return view('master-dev/item/edit', compact('item'));
}
My View:
<div class="card card-default">
{{ Form::model($item,['route'=>['item.update',$item['id']], 'files'=>true,'method'=>'PUT']) }}
<div class="card-header">
<h3 class="card-title"><b>Edit Data Item</b></h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<!-- /.card-header -->
<div class="card-body">
#if(!empty($errors->all()))
<div class="alert alert-danger">
{{ Html::ul($errors->all())}}
</div>
#endif
<div class="row">
<div class="col-md-6">
<div class="form-group">
{{ Form::label('kode_kategori', 'Kode Kategori') }}
<select name="kode_kategori" id="kode_kategori" class="form-control">
#foreach ($item as $i)
<option valu="{{ $i['kode_kategori'] }}">{{ $i['kode_kategori'] }}</option>
#endforeach
</select>
</div>
</div>
..........
..........
{{ Form::close() }}
I've tried any solutions in stackoverflow such as adding (ifempty...) and other solution but still the result Undefined index: id in my edit blade. When I was trying using dd and vardump the results was shown. I need to loop the foreach in my dropdown menu to show the list of data from my categories table. And I need to join my items table and my categories table to get the name of the categories.
you are calling same id from items and kategoris try this
public function edit($id)
{
// $item = Item::findOrFail($id);
$item = DB::table('items')
->join('kategoris', 'items.kategori_id', '=', 'kategoris.id')
->where('items.id', '=', $id)
->select('items.*', 'kategoris.id as kategory_id', 'kategoris.kode_kategori', 'kategoris.kategori_name')
->get();
// dd($item);
return view('master-dev/item/edit', compact('item'));
}
if this answer doesnot work show your database relation i will give you solution
$item = ....->get() will return a Collection to only have one item you need to use $item = ....->first() instead
But since you have #foreach ($item as $i) I believe, you still want to have a collection, but in that case, your issue is here
{{ Form::model($item,['route'=>['item.update',$item['id']], 'files'=>true,'method'=>'PUT']) }}
Since you have a collection, we don't know what $item['id'] it's referring to. Perhaps $item->first()['id'] ?
I solved the problem, there's a conflict fetching data from items and kategoris tables. There are differences calling a value with array and object, mostly if its data looped. So in the controller I must declared one by one, the selected value id from kategoris table, and I have to join both tables to get the name of the kategoris, and then I have to declare once more to get the list of the kategoris data. So there are three (3) variables to declare each one of them. For this long time I was looking for the short code in my Controller but I cannot find it.
Thank you for all of you guys helping me this problem. Cheers.

How to display data value according to dropdown in laravel?

I have a table in my database which has a field lis_type and the field has 2 values: sale and rent, and I am calling data on my view page. Let's suppose I click on the Sale page, there only sale similar listing should be displayed. If I click on Rent then similar listing should be shown for rent. However, right now both listings are coming together, Please let me guide where I am making a mistake.
Controller
public function listingshownow(Request $r, $slug)
{
$listview = Listing::where('slug', $slug)->first();
$products = Listing::where('propId', $listview->propId)
->where(function ($query) {
$query->where('lis_type', '=', 'sale')->orWhere('lis_type', '=', 'rent');
})->get();
return view('listings-view', compact('products'));
}
View for Similar Listings...
#foreach($products as $prod)
#if($prod->lis_type === 'sale')
<div class="row featured portfolio-items">
<div class="col-lg-7 col-md-12 homes-content pb-0 mb-44">
<ul class="homes-list clearfix">
<li>
<i class="fa fa-bed" aria-hidden="true"></i>
<span>{{$prod->ls_fs}}</span>
</li>
<li>
<i class="fa fa-bath" aria-hidden="true"></i>
<span>{{$prod->bathroom}} Bathroom</span>
</li>
</ul>
</div>
</div>
#endif
#endforeach
You need to pass a variable to your controller to indicate what kind of page you are viewing, so you can use that in your query/filter.
For example, let's say your sales page URL looks like http://example.com/listings/sales. Then you could set up a route like:
Route::get('listings/{type}', 'ListingsController#show');
Now your ListingsController could have a method like:
// The $type parameter will be what was matched in the route
public function show($type) {
$listings = Listings::where('lis_type', $type)->get();
return view('listings-view', ['listings' => $listings]);
}
You might want to check that $type only matches the values you expect. You could either do that in the controller, eg:
// At the top of show()
if ($type !== 'sales' && $type !== 'rent') {
abort(404);
}
Or you could restrict it in your route, by specifying that the {type} there has to match some pattern:
Route::get('listings/{type}', 'ListingsController#show')->where('type', 'sales|rent');
All of this is covered in the Laravel routing docs, I suggest reading them, and better yet, take some minutes and browse all the docs! 30min skimming will give you a broad idea of how things work, and the knowledge of where to look and read more when you need a question answered.

Paginate don't fill first page? - Laravel 6

I am using the page of Laravel with order by desc and I indicate that 5 elements are displayed.
When returning 7 the first page shows me 2 and the second 5. I want the first one to be filled before the second. If I use ASC it works correctly.
Controller:
public function list(){
$questions = Question::orderBy('id', 'DESC')->paginate(5);
return view('web.pages.list', compact('questions'));
}
Vista:
#foreach ($questions as $q)
<a href="/list/{{$q->category($q->category_id)}}/{{$q->id}}">
<div class="container-list-question">
<div class="header-category-list-question">
<span>{{$q->category($q->category_id)}}</span>
</div>
<div class="data-question">
Autor: {{$q->author}}
</div>
<div class="title-list-question">
<span>{{$q->question}}</span>
<div class="btn-show"> Show question</div>
</div>
</div>
</a>
#endforeach
{{ $questions->links() }}

Pass two variable in one array and show it on view page in Laravel

I am trying to merge two arrays into a single one and return it (the merged array) into a view in order to format and display it.
Below is the method in my controller intended for that purpose;
public function showJobCategoryContent($id)
{
$jobsInfo = Job::where('category_id', '=', $id)->where('published', '=', 1)->paginate(3);
$userInfo = Employee::all();
$array = array_merge($jobsInfo->toArray(), $userInfo->toArray());
return view('front.category-content.job-category-content', [
'jobsInfosById'=> $array
]);
}
Here, the content of my view;
#forelse($jobsInfosById as $jobInfoById)
<li>
<div class="well">
<h4>{{ $jobInfoById['company_name'] }}</h4>
<h4>{{ $jobInfoById['full_name'] }}</h4>
</div>
</li>
#endforelse
I get the following error:
Undefined index: company_name
What am I doing wrong and how can I resolve it?
use
$jobInfoById->company_name
instead of
$jobInfoById['company_name']

Resources