Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
my controller function is. I passed data through bidder array but array data can't print
public function bidders()
{
$payments = \App\Models\Payment::all();
$posts = Post::all()->where('user_id','=',Auth::user()->id);
foreach ($posts as $post) {
$postid = $post->id;
$bidder[] = Bid::all()->where('post_id','=',$postid);
}
return view('user.student.student-post-bidders',compact('bidder','posts','payments'));
}
Here is my blade in this blade my data print from DB.
How can I print array? indexes of array print in a blade
#php
dd($bidder);
$index = 0;
#endphp
#foreach($bidder as $bid)
#foreach($posts as $post)
#if($post->id == $bid[$index]['post_id'])
<div class="row" >
<!-- Dashboard Box -->
<div class="col-xl-12" >
<div class="dashboard-box margin-top-0">
<!-- Headline -->
<div class="headline">
<h4><strong><span class="bg-danger text-center" style="margin-left: 46px">Your Post Title:</span>{{$bid[$index]->Post->title}}</strong></h4>
</div>
<div class="content">
<ul class="dashboard-box-list">
<li>
<!-- Overview -->
<div class="freelancer-overview manage-candidates">
<div class="freelancer-overview-inner">
<!-- Avatar -->
<div class="freelancer-avatar">
<div class="verified-badge"></div>
#if($bid[$index]->Tutor->profile_Image=='')
<img data-cfsrc="{{asset('asset/images/user-avatar-placeholder.png')}}" alt="" style="display:none;visibility:hidden;">
#else
{{-- {{asset('storage/app/public/profileImages/' . $pic->profile_Image)}}--}}
<img data-cfsrc="{{asset('storage/app/public/profileImages/' . $bid[$index]->Tutor->profile_Image)}}" alt="" style="display:none;visibility:hidden;">
#endif
</div>
<!-- Name -->
<div class="freelancer-name">
<h4>{{$bid[$index]->Tutor->name}}</h4>
<!-- Details -->
<strong><span>Subject:</span></strong>
#foreach($bid[$index]->Tutor->subject as $sub)
<strong><span>{{\App\Models\Subject::find($sub->pivot->subject_id)->subject}}</span></strong>
#endforeach
<!-- Rating -->
<div class="freelancer-rating">
<div class="star-rating" data-rating="{{$bid[$index]->Tutor->rating}}"></div>
</div>
<!-- Bid Details -->
<ul class="dashboard-task-info bid-info">
<li><strong id="{{ $bid[$index]->id}}">PKR:{{$bid[$index]->bid_price}}</strong><span>Fixed Price</span></li>
<li><strong>Days:{{$bid[$index]->days}}</strong><span>Delivery Time</span></li>
</ul>
<!-- Buttons -->
<div class="buttons-to-right always-visible margin-top-25 margin-bottom-0" onclick=" indexID({{$i}})">
<a href="#small-dialog-1"
onclick="moveprice({{ $bid[$index]->id}}), payment({{ $bid[$index]->id}})" class="popup-with-zoom-anim button ripple-effect"
#foreach($payments as $pay) #if($bid[$index]->post_id == $pay->post_id) id="hide" #endif #endforeach><i class="icon-material-outline-check "></i> Accept Bid</a>
{{-- #dd($bid->id)--}}
<i class="icon-feather-mail"></i> Send Message
<a id="btn" class="button gray ripple-effect ico" onclick="removeBid({{$bid[$index]->id}})" title="Remove Bid" data-tippy-placement="top"><i class="icon-feather-trash-2"></i></a>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
</div>
<span style="visibility: hidden">{{$i++}}</span>
#endif
#endforeach
#php
$index++;
#endphp
#endforeach
When I run code error will be occur
offset 1 I need all data print in a blade
How to print array index wise
you should use relations,
if you want to show the list of bidders then in bidders model
app/bidders.php
public function post()
{
return $this->belongsTo('App\Post');
}
Assuming that you have proper standards in table structure for mapping eloquent.
then you need to change your bidders function as below
public function bidders()
{
$payments = \App\Models\Payment::get();
$bidders = Bidder::whereHas('post', function($post) {
return $post->where('user_id','=',Auth::user()->id);
//Provides a filter for bidders, post which are related to user
})->with('post')->get();
return view('user.student.student-post-bidders',compact('bidders', 'payments'));
}
And there should be only one loop for bidders on blade as below.
Here is your answer why array is not appearing in blade check in comments for json_encode
#foreach($bidders as $bid)
{{$bid->id}} //gives you bid parameters
{{json_encode($bid->post)}} //this will get all the post details array/object is not printable on blade so encoded to json just for display purpose
//suppose you need only name from post then
{{$bid->post->name}};
#endforeach
This will lower your code length, as well as improve performance due to eager loading and less looping. Also removes stuff like creating array and maintain index and if condition on blade for id check.
That's it.
Happy Coding!!
I think you just need to refactoring your code ),
first of all use $bid->{$index} instead of this $bid[$index]. you have collection but you can use it as array
do you want to group your bidder by post_id ?
Related
I need your help. Lets say I have array with 10 elements and every record to be shown on every iteration and I want to be done with Livewire, let me share with you some code and will tell you what I have tried till now.
public $content;
public $array = ['first', 'second', 'third' ,'fourth'];
foreach ($array as $item) {
sleep(1);
$this->content[] = "Element ${item}";
}
<div class="modal-body">
#if ($content)
<ul class="listree">
#foreach ($content as $element)
<li>
<div class="listree-submenu-heading">
{!! $element['title'] !!}
</div>
<ul class="listree-submenu-items">
#foreach ($element['elements'] as $sub)
<li>
<div class="listree-submenu-heading">
{!! $sub['title'] !!}
</div>
</li>
#endforeach
</ul>
</li>
#endforeach
</ul>
#endif
</div>
My idea is display first record, wait 1 second, display first and second record, wait 1 second, display first, second and third records and so on... How I can do this with livewire. The problem is that $content is filled with the information after all iteration and then the component is refreshed.
I tried on every iteration to send custom event which will call refresh method, but without succes. I will appreaciate any advice, and if you need more information, I will provide it.
Assumming you're also using alpinejs, this can be done pretty easily.
<x-app-layout>
<div class="text-gray-800 ml-10">
<ul class="bg-green-200">
<!-- The main foreach loop. -->
#foreach (range('a', 'z') as $element)
<!-- render al <li> tags with display:none. -->
<!-- Show the 1st after 0s, the 2nd after 1s, the 3rd after 2s, ... -->
<li class="bg-blue-200 m-5"
x-data="{show: false, index: {{ $loop->index }} }"
x-init="setTimeout(() => show = true, index * 1000)">
<div x-show="show">
{{ $element }}
...
</div>
</li>
#endforeach
</ul>
</div>
</x-app-layout>
$loop is a special object you can access within a #foreach block.
I have a paging setup, which can be shown as such:
<div class="page__A4">
</div>
<div class="page__A4">
</div>
<div class="page__A4">
</div>
Data is then passed from my SQL database using PHP, through a for loop, e.g:
#foreach($data as $value)
<div class="element">
{{$value}}
</div>
#endforeach
which would leave the final markup:
<div class="page__A4">
#foreach($data as $value)
<div class="element">
{{$value}}
</div>
#endforeach
</div>
<div class="page__A4">
</div>
<div class="page__A4">
</div>
My $value is dynamic in height, and when the amount of $value's exceeds the first page__A4 element in height, I want it to proceed to the next page.
The real issue relies in that I am unable to use Javascript. I need to print these pages to PDF, which is done by combining a laravel view with a SASS styling file - meaning javascript wont be loaded in my final printed product.
Is there a way to achieve this, using a combination of laravel/php/SASS?
You have to class="A_4" dynamic too if you want to use it in javascript.
foreach($values as $ value){
<div class="'example'.$value.">
</div>
}
I'm facing a problem
Invalid argument supplied for foreach()
in laravel.
I used DB:: and Join and where
give me
Invalid argument supplied for foreach()
Controller:
public function index()
{
$jobs =DB::table('jobs')->where('is_approved',true)
->join('countries','jobs.country_id','countries.id')
->join('job_types','jobs.job_type_id','job_types.id')
->select('jobs.*','countries.countryname','job_types.job_type')
->first();
$category=categories::all();
$countries=country::all();
return view('front.home.homeContant', compact('jobs','category','countries'));
}
View
#forelse($jobs as $job)
<a href="{{url('/job-details',$job->id)}}" class="job-listing">
<!-- Job Listing Details -->
<div class="job-listing-details">
<!-- Logo -->
<div class="job-listing-company-logo">
<img src="{{ asset('front') }}/images/EETLogo.png" alt="">
</div>
<!-- Details -->
<div class="job-listing-description">
<h3 class="job-listing-title">{{ $job->title }}</h3>
<!-- Job Listing Footer -->
<div class="job-listing-footer">
<ul>
<li><i class="icon-material-outline-location-on"></i>{{ $job->countryname }}</li>
<li><i class="icon-material-outline-business-center"></i> {{ $job->Job_Type }}</li>
<li><i class="icon-material-outline-access-time"></i><?php echo \Carbon\Carbon::createFromTimeStamp(strtotime($job->created_at))->diffForHumans() ?></li>
</ul>
</div>
</div>
<!-- Bookmark -->
<span class="list-apply-button ripple-effect">Apply Now</span>
</div>
</a>
#empty
<div class="job-listing-description">
<h3 class="job-listing-title">No Jobs Found</h3>
</div>
#endforelse
I used Get() but it's gives me ampty array
$jobs = DB::table('jobs')
->join('countries','jobs.country_id','countries.id')
->join('job_types','jobs.job_type_id','job_types.id')
->select('jobs.*','countries.countryname','job_types.job_type')
->where('is_approved',true)
->get();
When I delete
->join('job_types','jobs.job_type_id','job_types.id')
It's working very well
You are using first() which gives a model instance or stdObject. You can not loop them. You need to use get().
$jobs = DB::table('jobs')
->join('countries','jobs.country_id','countries.id')
->join('job_types','jobs.job_type_id','job_types.id')
->select('jobs.*','countries.countryname','job_types.job_type')
->where('is_approved',true)
->get();
Thank you all for your kindly help
the problem solved when I change
->select('jobs.*','countries.countryname','job_types.job_type')
to
->select('jobs.*','countries.countryname','job_types.Job_Type')
I just capitalized
Job_Types
I don't know why I must capitalize it, but finally, it's worked.
My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
I think you are closing the PHP tag in the wrong place. Change to this:
<?php print_r($count);
$data = $this->BasicController->count {{ ($row->sub_id) }}
echo $data['count'];//here is the rightplace to close your PHP ?>
</span></div>
All PHP code needs to stay between the <?php and ?>
Looking to your image, apparently, it's printing the $count and then it's printing the plain text , so you have 80$data = ...
If you counted your rows in controller by the code below
$count = DB::table('products')->where('sub_id', $id)->count();
then why wrote so many code in html for counting?
Remove all code form counting form your html and just use this {{ $count }}
In my application, I've always been able to pass data to any view as one would normally do using view('myView', compact('data'));. As of today, any view I try to render this way times out. I'm getting the error Maximum execution time of 120 seconds exceeded in Whoops!. I tried increasing php.ini and httpd.conf timeout times but no cigar. It's really odd and it doesn't make sense to me because I've always been able to render my views almost instantly, even when retrieving 15k+ records from the database and passing them to the view like I've always done.
My controller:
use App\Product;
use Illuminate\Support\Facades\Session;
class HomeController extends Controller {
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
//the controller is normally like this
//$products = Product::paginate(16);
//return view('home', compact('products'));
//I'm testing with these 2 lines below but no cigar.
$product = Product::wherePid(303)->first();
return view('test', compact('product'));
}
}
The test view I created:
#extends('app')
#section('content')
{{ $product->name }}
#stop
My application view:
#extends('app')
<pre>{{ var_dump(Session::all())}}</pre>
#section('content')
<div class="row">
#foreach($products as $product)
<div class="col-xs-6 col-sm-3 col-lg-3 col-md-3">
<?php
if($product->img[7] == 'm' || $product->img[7] == 'M') echo "<div class='continenteIcon'></div>";
else echo "<div class='jumboIcon'></div>";
?>
<div class="thumbnail">
<a href="products/{{$product->pid}}"><img src="{{$product->img}}" title="
<?php
if($product->dispname != '') echo $product->dispname;
else echo $product->name;
?> ">
</a>
<div class="caption">
<h4>
<a style="text-decoration:none;" class="wordwrap" title="
<?php
if($product->dispname != '')
echo $product->dispname;
else echo $product->name;
?>" href="products/{{$product->pid}}">
<?php
if($product->dispname != '')
echo $product->dispname;
else echo $product->name;?>
</a>
</h4>
<p>{{$product->brand}}</p>
<span class="pull-right price">€{{$product->price}}</span>
<br/>
<span class="pull-right ppk">€{{round($product->pricekilo, 2)}} Kg, L ou Und</span>
</div>
<div class="ratings">
<p class="pull-right"> {{-- # review--}}</p>
<p>
<form method="post" action="add/{{$product->pid}}">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<button title="Adicionar ao carrinho" type="submit" class="btn btn-success">
<i class="fa fa-shopping-cart"></i>
</button>
</form>
<form method="post" action="products/related/{{$product->pid}}">
<input type="hidden" name="_token" value="{{{ csrf_token() }}}" />
<button title="Ver artigos semelhantes" style="position:relative; bottom:35px;" type="submit" class="btn btn-info pull-right">
<i class="fa fa-search"></i>
</button>
</form>
</p>
</div>
</div>
</div>
#endforeach
</div>
<div class="row">
{!! $products->render() !!}
</div>
<div class="row">
<div class="pull-right">
* Preço por unidade, Litro ou Kilograma
</div>
#stop
#section('scripts')
#stop
The problem doesn't only happen in this view, but every single time I try to fecth someting from the database and pass it to the view to render. I keep getting timeouts and I can't seem to fix it no matter what I do.
I am clueless why this is happening. It seems like it started out of the blue. I have no Idea what could be causing this issue.
Any help?
P.S.: I'm using Wamp.
EDIT: I forgot to add something that might be important:
Everything is up and running in Wamp. If I dd() out the query result and do not render the view
$products = Product::paginate(16);
dd($products);
//return view('home', compact('products'));
this is fast, as it always used to be. And by fast I mean it takes less than 1 second to retrieve everything I need. But if I render the view with
return view('home', compact('products'));
everything just stalls and I get a 500 (I checked with Fiddler2 and after the page stops loading, the request status is 500)
It seems like you may be requesting too many records which may be using too much of your RAM. I would use the chunk command to help you with managing the amount you're requesting.
For example:
User::chunk(200, function($users)
{
foreach ($users as $user)
{
//
}
});
First check logs.
Next try to dd($product)
Next if you try to render view with last 2 lines (getting first record) remove pagination from template.
Clean template to minimum e.g.
#extends('app')
#section('content')
<div class="row">
#foreach($products as $product)
#endforeach
</div>
#stop
I just sorted it out. The issue was in the following block of code in app.blade.php.
$size = Session::get('size');
...
<input type="text" value="'.Session::get($item).'">
...
I was messing around with data from an existing session and everything was working fine. I assumed I was doing it right. I wasn't. Not by a chance :)
Assumption is the mother of all screw ups.
Surrounded the whole block with if(Session::has('size') and everything is blazing fast and running smoothly as usual.
Thanks #Pyton for pointing me out into the right direction and thanks everyone for your contribution.