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();
Related
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.
Good morning guys, i need to have a custom date that always has the current date but a certain time to be able to compare two dates and get the remaining time. At the moment something like this occurred to me:
$day=date("d");
$dayy= now();
$datelim= Carbon::createFromFormat('Y-m-d H:i:s', '2020-05-',$day, '22:00:02');
return view('fen.index', [
'works' => Period::select('paperiod.descriptionp','pamatrixinfoperio.description', 'pamatrixinfoperio.codpar')
->join('pamatrixinfoperio', 'pamatrixinfoperio.cod_paperiod', '=', 'paperiod.cod')
->where('pamatrixinfoperio.read_at', '=', 0, 'AND')
->where('pamatrixinfoperio.cod_paperiod', '=', 1)
->get(),
'Periodres' => $fechalim
]);
but I don't know how to concatenate that variable day in the carbon '2020-05-',$day, '22:00:02' , How could I use it because my objective is that the variable always has the current day, Monday, Tuesday, Wednesday, but that the time is always the same.
On the other hand, I don't know if they could help me. How can I compare dayy and datelim? So in the view I can have for example: "The work is about to end in 3 days, 3 hours and 20 seconds" I hope you can help me, because I find more complex examples in carbon but not basic and I don't know how to do this :(
my view is this:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">WorksF</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
Welcome {{Auth::user()->name}}, Lasis informs you that the following files are about to end in:
#foreach($works as $work)
<li class="list-group-item">
{{$work->description}}
you have the next time to deliver the report:
<b>{{$Periodres}}</b>
</li>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
As you can see the view is not finished but I am looking for something similar, Thanks in advance for the help.
Concatenation in PHP use . not , and you need a space between $day and the hour.
Carbon::createFromFormat('Y-m-d H:i:s', '2020-05-' . $day . ' 22:00:02');
Of course Carbon::create() as suggested by Nacho is appropriate, but still Carbon::createFromFormat() works also fine if the given input is given as 1 single string and matches the format.
I think in your case you can use:
Carbon::create($year, $month, $day, $hour, $minute, $second)
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() }}
My Controller:
class Comments extends Controller {
public function GenerateComments($id){
$theme = DB::table('New_Themes')
->where('id', $id)
->get();
$Comments = NewTheme_Comment::where('id_theme', $id)->get();
return view('comments', ['Themes'=>$theme, 'Comments'=>$Comments]);
}
My Table(NewTheme_Comment):
id
parent_id
id_theme
user
text
upVotes
downVotes
My view(contains the recursive display of the tree of comments like the same in reddit), ......(data) contains the bootstrap media object, and the </div>'s things are used to round up (visually) the tree of comments as it should be:
<?php
tree($Comments, 0, 0);
$var = -1;
function tree($Comments, $parent_id = 0, $level=0, $c=0) {
global $var;
foreach($Comments as $Comment) {
if($Comment['parent_id'] == $parent_id) {
If ($level > $var) $var++; else {
for ($i = $var-$level+1; $i>0; $i--) { if ($c < 0) echo '</div> </div>'; else $c--; };
$var=$level;
};
echo '........(data)';
tree($Comments, $Comment['id'], $level+1,$c);
};
};
};
?>
The problem is that .........(data) should contain this stuff:
<div class="media">
<div class="media-left">
<img class="media-object" style="height:40px; width:40px;" src="{{ URL::asset("images/upVote.svg") }}" >
<div>{{$Comment->upVotes-$Comment->downVotes}}</div>
<img class="media-object" style="height:40px; width:40px;" src="{{ URL::asset("images/downVote.svg") }}" >
</div>
<div class="media-body">
<p class="media-heading">{{ $Comment->text }}</p>
<p class="media-heading">{{ $Comment->user }} / {{ $Comment->created_at }} </p>
And I am using the blade above this line | , which I can't integrate into that echo in view, replacing the ..........(data).
I have the intuition that the function I should integrate into the controller but I am broken(I spent to much time on recursive method of displaying comments) and I don't know how to take the data and print out it as whole unit recursively.
Any help is GREATLY appreciated to find a way out of this mess, thank you very much
Edit 1:
This is an example if i am filling with bootstrap media object in ........(data):
<div class="media">
<a class="media-left" href="#">
<img class="media-object" src="..." alt="...">
</a>
<div class="media-body">
<h4 class="media-heading">Media heading</h4>
Without 2 x </div>
You are approaching the views in a wrong way, as blade templates are not meant to use functions, it's better to follow the below recommendations.
The best way for that is to place the function code inside a blade file, for example recursive.blade.php:
recursive.blade.php
#foreach($comments as $comment)
//place your code here
#endforeach
Then in your main blade you can call it several times:
main.blade.php
<div>
#include('recursive', ['comments' => $comments])
</div>
The below example works for me and is the most widely used approach. remember the default value for parent_id is -1.
Model
public function children(){
return $this->hasMany(self::class,'parent_id','id')->with('children');
}
Controller
$comments = Comments::where('parent_id','=',-1)->get();
return view('comments',['comments'=> $comments]);
Blade (comments.blade.php)
<div class="tree">
#include('comment-list-widget',['comments' => $comment])
</div>
Blade (comment-list-widget.blade.php)
<ul>
#foreach($comments as $comment)
<li>
<a>{{$comment->text}}</a>
#if(!empty($comment->children) && $comment->children->count())
#include('comment-list-widget',['comments' => $comment->children])
#endif
</li>
#endforeach
</ul>
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();