Invalid argument supplied for foreach() laravel 5.8 - php

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.

Related

Laravel 7.14 loading social links with for each loop on header

I have a datatable for social links. a user can edit the link whenever he wants.
Just link any other crud it is very simple. I am trying to load all the links in blade view with icons in header of my app. I created as function and added a for each loop in the snippets.
here's my controller:
public function socials()
{
$socials = Social::all();
return view('inc.subheader')->with('socials', $socials);
}
Here's blade snippent:
<div class="col-md-3 justify-content-center social-links">
<ul class="nav d-flex justify-content-center">
#foreach ($socials as $social)
<li class="">
<i class="{{ $social->icon }} fa-2x"></i>
</li>
#endforeach
</ul>
</div>
The error I'm getting is "$socials is undefined" and when I go on localhost/inc/social I only see three dots and no icons with links.
Change this line:
return view('inc.subheader')->with('socials', $socials);
to this:
return view('inc.subheader', compact('socials'));
You can try all combination :
return view('inc.subheader', compact('socials'));
return view('inc.subheader', [ 'socials'=> $socials]);

How to Handle Blade laravel [closed]

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 ?

href returning another URL

After I click in the menu navigation (for exemple href='home') when i am in the view .../public/single/1 they send me to .../public/single/home and not .../public/home
Here is my code:
MENU LIST
<div class="header_content d-flex flex-row align-items-center justify-content-start">
<div class="logo"><span>m</span>a <span> B</span>ibli<span>o</span></div>
<nav class="main_nav">
<ul class="d-flex flex-row align-items-start justify-content-start">
<li class="{{$home}}">Acceuil</li>
<li class="{{$about}}">About us</li>
<li class="{{$listing}}">Produits</li>
<li class="{{$blog}}">Nouveauté</li>
<li class="{{$contact}}">Contactez nous!</li>
</ul>
</nav>
<div class="submit ml-auto">Recherche >></div>
<div class="hamburger ml-auto"><i class="fa fa-bars" aria-hidden="true"></i></div>
</div>
</div>
WEB.PHP
Route::get('/single/{id}','AnnonceController#annonce');
Route::get('/home','PagesController#home');
Route::resource('home','AnnonceController');
And this is my controller:
public function annonce($id) {
$annonce = DB::table('annonce')
->join('article', 'annonce.ID_ANNONCE', '=', 'article.ID_ANNONCE')
->join('photo_articles', 'photo_articles.ID_ARTICLE', '=', 'article.ID_ARTICLE')
->select('annonce.*', 'article.NOM_ARTICLE','PHOTO_ARTICLE')
->where('annonce.ID_ANNONCE',$id)
->get();
return view('single')->with('annonce',$annonce);
}
It's happening because you giving href='home' which will add to your current URL , which means you are on single/1(route parameter) and when you click it will become single/home(parameter).
so give the full path.
I suggest you should use the route function of laravel and also give the name to your route like below
Route::get('/home','PagesController#home')->name('myhome');
see Documentation for naming route.
then call this in your blade like this
{{route('myhome')}}
You return statement should be like this return view('single',['id' => $id])->with('annonce',$annonce);
Note: Please use relationship in Laravel instead of DB::table
public function annonce($id) {
$annonce = DB::table('annonce')
->join('article', 'annonce.ID_ANNONCE', '=', 'article.ID_ANNONCE')
->join('photo_articles', 'photo_articles.ID_ARTICLE', '=', 'article.ID_ARTICLE')
->select('annonce.*', 'article.NOM_ARTICLE','PHOTO_ARTICLE')
->where('annonce.ID_ANNONCE',$id)
->get();
return view('single',['id' => $id])->with('annonce',$annonce);
}

Trying to get property of non-object error in laravel 5.4

I'm trying register in my app but this Error happened and I don't know why.
and my post.blade.php file has this code:
<div class="blog-post">
<h2 class="blog-post-title">
<a href="/posts/{{$post->id}}">
{{$post->title}}
</a>
</h2>
<p class="blog-post-meta">
{{$post->user->name }}
{{$post->created_at->toFormattedDateString()}}
</p>
{{$post->body}}
postcontroller has this code:
public function index()
{
$posts=Post::latest()->get();
return view('posts.index',compact('posts'));
}
and index file is:
#foreach($posts as $post)
#include('posts.post')
#endforeach
<nav class="blog-pagination">
<a class="btn btn-outline-primary" href="#">Older</a>
<a class="btn btn-outline-secondary disabled" href="#">Newer</a>
</nav>
</div><!-- /.blog-main -->
I think your code need to update like:
public function index()
{
$posts=Post::with('users')->latest()->get();
return view('posts.index',compact('posts'));
}
<p class="blog-post-meta">
{{$post->users->name }}
{{$post->created_at->toFormattedDateString()}}
</p>
Hope this work for you!
assume that you are including post.blade.php in index.blade.php file.
In above case you are not passing $post to post.blade.php. Your foreach loop will be like :
#foreach($posts as $post)
#include('posts.post', ['post' => $post])
#endforeach
I assume that you are including post.blade.php in index.blade.php file.
In above case you are not passing $post to post.blade.php. Your foreach loop will be like :
#foreach($posts as $post)
#include('posts.post', ['post' => $post])
#endforeach

model function call in blade view laravel

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
Although its no good Practice Accessing the DB in Blade (better do this in the controller and pass the data) you can do:
<div class="price"><strong>Products</strong>
<span>
{{ BasicModel::where('sub_id', $row->sub_id)->count() }}
</span>
</div>
Its not tested, but have a look at the Eloquent docs, the count() method is explained there.
Update: I am not shure if laravel will find the class BasicModel (I never would access Models directly in blade, as stated do this in the controller and pass the data.) So maybe you need to write it with the full Namespace most likely {{ \App\BasicModel::where() }}.

Categories