I stuck an something really easy (I guess).
I have an array:
array(30) {
["gericht_1_mo"]=>
string(6) "Nudeln"
["preis_1_mo"]=>
string(4) "5,50"
["gericht_2_mo"]=>
string(64) "Paniertes Schweineschnitzel
mit Pommes frites und Beilagensalat"
["preis_2_mo"]=>
string(2) "13"
["gericht_1_di"]=>
string(75) "Gebratenes Hähnchenbrustfilet auf asiatischer Gemüsesoße mit Basmatireis"
["preis_1_di"]=>
string(4) "4,70"
["gericht_2_di"]=>
string(83) "Geröstete Hörnchennudeln mit Ei, Käse, Speck
und Zwiebeln dazu ein bunter Salat"
["preis_2_di"]=>
string(4) "7,90"
}
and want to loop through the array in my view
#foreach($input as $key => $item)
<div class="col-xs-6" style="width: 100%;">
<h3>Day</h3>
<hr />
<div class="dishContainer">
<h4 class="dishTitle">item</h4>
<p class="dishDesc">{{ $item }}</p>
<p class="priceTag">{{ How caN I display the Price of every item ?}}€</p>
</div>
</div>
#endforeach
After a long time playing around I cant find the solution to output the data properly.
I would argue there is something wrong with your data model. At least, in order to render the data as you would like, I would convert the data before calling the view. Something like:
$output = [];
foreach($input as $key => $value){
if(preg_match('/(preis|gericht)_([0-9]+_[a-z]{2})/', $key, $matches)){
$output[$matches[2]][$matches[1] = $value;
}
}
Mind you, this code is unchecked, so it should probably be tested/corrected before use.
#for ($x = 0; $x < count($input); $x+=2)
<div class="col-xs-6" style="width: 100%;">
<h3>Day</h3>
<hr />
<div class="dishContainer">
<h4 class="dishTitle">item</h4>
<p class="dishDesc">{{ $input[$x] }}</p>
<p class="priceTag">{{ $input[$x+1] }}€</p>
</div>
</div>
#endfor
Related
I am laravel developer and developing a real time chat room using liveware , i am trying to open chat when i click to user but unfortunatly i am getting error https://flareapp.io/share/OmVDe437#F47 please help me how can resolved that ? thank u.
I am also getting error in console.
app\Http\Livewire\Messaging.php
public $selectedConservation;
public function mount(){
$this->selectedConservation = Conservation::query()
->where('sender_id',Auth::user()->id)
->orwhere('reciever_id',Auth::user()->id)
->first();
}
public function viewMessages($conservationsId){
$this->selectedConservation =
Conservation::findorFail($conservationsId);
}
public function render()
{
$conservation = Conservation::query()
->where('sender_id',Auth::user()->id)
->orwhere('reciever_id',Auth::user()->id)
->get();
return view('livewire.messaging',[
'conservation' => $conservation,
]);
}
resources\views\livewire\messaging.blade.php
#foreach ($conservation as $conservations)
<a href="#" wire:click.prevent="viewMessages({{ $conservations->id}} )">
<div class="user-card rounded bg-dark bg-opacity-10 mb-1">
<div class="mx-2">
<div class="d-flex pt-3">
<img class="rounded-circle" width="48px" height="48px" src="{{Config('wfh.file').$conservations->reciever->avator}}" alt="">
<div class="notification-text ms-3 w-100">
<span class="username fw-bold">{{$conservations->reciever->full_name}}</span>
<span class="float-end small"></span>
<p class="mt-1 text-muted">You: </p>
</div>
</div>
</div>
</div>
</a>
#endforeach
Check out the documentation on Dom Diffing Issues. It looks like you need to add a wire:key="{{ $conversations->id }}" attribute into your a tag so that Livewire can track changes to the list of conversations.
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();
I am trying to make a blog foreach that see this erro
Invalid argument supplied for foreach() (View:
my site https://weadam.com/
my code is:
controller:
public function index()
{
$localetmp = Session::get('locale');
$blogs = #json_decode(file_get_contents("https://www.weadam.com/blogs/wp-json/wp/v2/posts?per_page=4&lang=" . $localetmp));
$blogsold = Blog::orderBy('id','desc')->take(100)->get();
foreach ((array)$blogs as $blog){
// $blog->elapsed = $this->time_elapsed_string('#'.$blog->date);
// $blog->date = date("d F Y",$blog->date);
}
$error = "";
return view('home-new',['blogs' => $blogs]);
}
and view is:
<div class="eng-home-blog">
<div class="col-sm-9 home-post-disp bx-shadow brd-radius bg-white">
#foreach($blogs as $blog)
#if($blog->sticky)
<img src="{{$blog->fimg_url}}" alt="" class="img-responsive brd-radius bx-shadow">
<div class="hm-post-date">{{$blog->date}}</div>
<h3>{{$blog->title->rendered}}</h3>
<p>{!!$blog->excerpt->rendered!!}</p>
<span>{{__("READ MORE")}}</span>
#endif
#endforeach
</div>
</div>
pass true as secondary param at json_decode function
$blogs = #json_decode(file_get_contents("https://www.weadam.com/blogs/wp-json/wp/v2/posts?per_page=4&lang=" . $localetmp),true);
now you have to fetch this blog data as associative data. not as an object. here's the example code for it
<div class="eng-home-blog">
<div class="col-sm-9 home-post-disp bx-shadow brd-radius bg-white">
#foreach($blogs as $blog)
#if($blog['sticky'])
<img src="{{$blog['fimg_url']}}" alt="" class="img-responsive brd-radius bx-shadow">
<div class="hm-post-date">{{$blog['date']}}</div>
#endif
#endforeach
</div>
</div>
You can simply use forelse it will take care of the empty case
#forelse($blogs as $blog)
// do what ever you want to do
#empty
<div> Nothing to show </div>
#endforelse
Hope it helps reference.
Thanks.
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>
Right now I have a chat and what I am working on is being able to take selected chat messages and turn them into a post with comments. My struggle is that right now I am only getting the very last comment to save and none of the comments before it. This has to do with not being able to write the foreach loop correctly when saving in the controller I believe.
The error I am getting right now is
Invalid argument supplied for foreach()
I am showing the commenters array but not saving it correctly. The commentors are ids of users involved and the user_id (not userId) is what I need to save for both, right now only getting '11' and not '9'. as you can see from the error message
...'commenters' => ' 9, 11, ', 'user_id' => '11', 'comment' => 'hey there'), 'userId' => '9', 'commentorIds' => array(), 'createPost' => object(Post)))
Form
<div class="selectedChatSection" style="padding: 15px 15px 15px 15px;">
{!! Form::open(['url'=>'post/selectedChatPost']) !!}
<div class="jumbotron">
<center><img src="/img/icons/icon-chat.png"></center>
</div>
#foreach ($postSavers as $postSaver)
<div class="postSaverId" data-id="{{$postSaver->id}}"></div>
#endforeach
<input type="hidden" name="space-id" value="">
<input type="hidden" multiple="multiple" name="commenters" value="#foreach($selectedChats as $selectedChat){{$selectedChat->id}}, #endforeach ">
#foreach ($selectedChats as $selectedChat)
<div style="border-top: 1px solid #eee;padding: 10px 10px 10px 10px;">
<p><img src="/assets/avatars/{{$selectedChat->avatar}}" height:"75" width="75" style="border-radius:50px;"> <input type="hidden" name="user_id" value="{{$selectedChat->id}}">{{$selectedChat->name}} : <input type="hidden" name="comment" value="{{$selectedChat->body}}">{{$selectedChat->body}}</p>
#endforeach
</div>
<div class="modal-footer">
{!! Form::submit('post', ['id'=> 'post-button']) !!}
{!! Form::close() !!}
</div>
Controller
public function storeSelectedChat() {
$input = Request::all();
$userId = Auth::user()->id;
$commentorIds = array();
$createPost = new Post;
$createPost->space_id = 8;
$createPost->user_id = $userId;
// $createPost->content = $input['content'];
$createPost->linkPhoto = "/img/icons/icon-chat.png";
$createPost->save();
//needs to be a foreach loop here for each comment/chat segment.
foreach(Input::get('commenters') as $commenter) {
$createComments = new Comment();
$createComments->post_id = $createPost->id;
$createComments->comment = $input['comment'];
$createComments->user_id = $commenter;
$createComments->save();
}
}
Any help would be greatly appreciated. I have done this before but nothing seems to work in this instance for some reason. Thanks.
Your <input type="hidden" multiple="multiple" name="commenters" value="#foreach($selectedChats as $selectedChat){{$selectedChat->id}}, #endforeach "> should not have multiple attribute.
The multiple attribute works with the following input types: email,
and file.
According to http://www.w3schools.com/tags/att_input_multiple.asp. Even though the multiple attribute logically makes sense to you because logically you have multiple commentors, semantically it is incorrect. You still have one <input> attribute and what it contains logically is up to you interpret. You should use regex to separate your comma delimited input and then use foreach on that resulting array.