Laravel-5.8::Invalid argument supplied for foreach() - laravel

hi i have multiple db tables ('dealers,suppliers,histories') and trying to show data of suppliers which are related to dealers(in dealers table supplier_id is using as foreign key)at dealers index but it is showing error: Invalid argument supplied for foreach() ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,m trying to get history data form dealers index by supplier id,,,,,,,,,,,,
note only dealers are showing on index and I am using resource route for it
code of index:
#foreach ($data as $row)
<tr>
<td>{{ $row->dealer_name }}</td>
<td>
#foreach($row->supplier as $sp)
{{ $sp->supplier_name }}<br>
#endforeach
</td>
</tr>
#endforeach
Controller code:
$data = Dealer::all();
return view('dealer.index', compact('data'));
supplier model:
public function dealerHistory()
{
return $this->hasoneThrough('App\History', 'App\Dealer');
}
all other models only have protected fillable in them according to this https://laravel.com/docs/5.8/eloquent-relationships#has-one-through

Here as you mentioned dealers are showing then problem must be in #foreach($row->supplier as $sp)
As error says Invalid argument supplied for foreach() you need to verify $row->supplier is array.
#foreach ($data as $row)
<tr>
<td>{{ $row->dealer_name }}</td>
<td>
#if(is_array($row->supplier) && !empty($row->supplier))
#foreach($row->supplier as $sp)
{{ $sp->supplier_name }}<br>
#endforeach
#endif
</td>
</tr>
#endforeach

#foreach ($data as $row)
<tr>
<td>{{ $row->dealer_name }}</td>
<td>
#if(!empty($row->supplier()))
#foreach($row->supplier as $sp)
{{ $sp->supplier_name }}<br>
#endforeach
#endif
</td>
</tr>
#endforeach

Related

how to retrieve one to many relationship. Property [produks] does not exist on this collection instance

Operator Model
public function produk()
{
return this->hasMany(Produk::class);
}
Produk Model
public function operator()
{
return this->belongsTo(Operator::class);
}
Controller
public function operator()
{
$data = Operator::all();
return view('data', compact('data'));
}
View
#foreach ($data as $o)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $o->n_oper }}</td>
<td>
#foreach ($data->produk as $p)
{{ $p->n_prod }}
#endforeach
</td>
</tr>
#endforeach
Output
Exception
Property [produk] does not exist on this collection instance. (View: C:\xampp\htdocs\laravel\oni\resources\views\data.blade.php)
what went wrong? please kindly assist me, im new to this
this is a typo may be?? whatever, you are calling relationship on collection. relation belongs to an object.
#foreach ($data as $o)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $o->n_oper }}</td>
<td>
#foreach ($o->produk as $p)
{{ $p->n_prod }}
#endforeach
</td>
</tr>
#endforeach
data is the collection. you have to call relationship on $o
and there's some other typos may be. you are missing the $ sign in $this keyword. update your relationship
public function produk()
{
return $this->hasMany(Produk::class);
}
and
public function operator()
{
return $this->belongsTo(Operator::class);
}
You are calling relation on collection. You have to call it on single instance like this
#foreach ($data as $o)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $o->n_oper }}</td>
<td>
#foreach ($o->produk as $p)
{{$p->n_prod}}
#endforeach
</td>
</tr>
#endforeach
And also change
return this->hasMany(Produk::class);
this to
return $this->hasMany(Produk::class);
and also for other relation like
return this->belongsTo(Operator::class);
to
return $this->belongsTo(Operator::class);
I hope its work.

How to fetch value from multiple table through pivot table in Laravel?

I'm trying to fetch value from different tables. I get the value but it repeat same quantity. I'm getting this see this image
I expect this see this image
Here is my controller code
$orders = Order::all();
view('admin.order.index', compact('orders'));
Here is my model relationship
return $this->belongsToMany(FoodItem::class, 'order_food_items', 'order_id', 'food_item_id')->withTimestamps();
}
public function orderItems() {
return $this->hasMany(OrderFoodItem::class);
}
public function user() {
return $this->belongsTo(User::class);
}
Here is in blade code, May be I'm doing wrong
#foreach ($orders as $key => $order)
#foreach ($order->orderFoodItems as $product)
#foreach ($order->orderFoodItemPrice as $price)
<tr>
<th scope="row">{{ $key + 1 }}</th>
<td>{{ $product->name }}</td>
<td>
<img src="{{ asset('/storage/items/food/' . $product->image) }}"
alt="">
</td>
<td>{{ $order->user->name }}</td>
<td>
<span class="badge badge-primary">Pending</span></td>
<td>something</td>
<td>{{ $price->discounted_price }}</td>
</tr>
#endforeach
#endforeach
#endforeach
Can anyone help me?

Trying to get property 'column_name' of non-object while displaying data on view

I want to show data on my blade view from relationship data but when am trying to display data whereas table contains only one row of data it's showing on view but if I insert more than one data in table it's giving me an error.
I have three tables courses, sections, course_section. In course_section table these are the following columns course_id and section_id.
I have tried {{ $section->courses()->first()->courseTitle }} this snippet in view found on stackoverflow.
My Section Model code:-
class section extends Model
{
public function courses(){
return $this->belongsToMany('App\Model\admin\course\course','course_sections','course_id');
}
}
My Section Controller code:-
$sections = section::with('courses')->orderBy('id','DESC')->get();
return view('backend.courses.section.all',compact('sections','courses'));
My view code:-
#foreach ($sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>{{ $section->courses()->first()->courseTitle }}</td>
</tr>
#endforeach
I am getting this error
"Trying to get property 'courseTitle' of non-object (View:
resources/views/backend/courses/section/all.blade.php)"
Here is the following you are doing wrong:
Replace $section->courses() with $section->courses as you are already doing early loading. And $section->courses() will query to db again.
Check if relational data exists then show.
So your code would be as follow:
#foreach ($sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>
#php
$course = $section->courses->first();
#endphp
{{ $course->courseTitle or "" }}
</td>
</tr>
#endforeach
Let me know if it helps!
Edited:
As per conversation the relationship has been changed like course ->hasMany -> sections and section ->belongsTo -> course so the blade would be change like.
#foreach ($sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>
#php
$course = $section->course;
#endphp
{{ $course->courseTitle or "" }}
</td>
</tr>
#endforeach
Section Controller:
$sections = section::with('courses')->orderBy('id','DESC')->get();
return view('backend.courses.section.all', compact('sections'));
In the View you have to loop the sections and then the courses and create each row. For ex:
#foreach ($sections as $section)
#foreach ($section->courses as $course)
<tr>
<td>{{ $section->title }}</td>
<td>{{ $course->courseTitle }}</td>
</tr>
#endforeach
#endforeach
Note it's $section->courses and not $section->courses(), because the related courses are already there, you doesn't need to query them again.
Update
Or you can do the query over course
$courses = course::with('sections')->get();
return view('backend.courses.section.all',compact('courses'));
And in the View:
#foreach ($courses as $course)
#foreach ($course->sections as $section)
<tr>
<td>{{ $section->title }}</td>
<td>{{ $course->courseTitle }}</td>
</tr>
#endforeach
#endforeach

How to calculate the total of price for product in Laravel blade

I want to calculate the total price. How can I do it?
This is my blade code:
#foreach ($ventes as $value)
<td>{{ $value->produit->nom }}</td>
<td>
{{ $value->quantitevendu }}</a>
</td>
<td>
({{$value->prix}})*{{$value->quantitevendu}}.euro
</td>
<td>
({{$value->prix}}).DH
</td>
<td>
{{ $value->description }}
</td>
</tr>
#endforeach
It gives me results like this:
(10)*2
(11)*4
(2)*1
But I want results like this:
20
44
2
You did concatenation instead of multiplication. Try this:
#foreach ($ventes as $value)
<td>{{ $value->produit->nom }}</td>
<td>
{{ $value->quantitevendu }}</a>
</td>
<td>{{$value->prix * $value->quantitevendu}} .euro</td>
<td>({{$value->prix}}).DH</td>
<td>{{ $value->description }}</td>
</tr>
#endforeach
I hope you get it the idea.
It's best practice to put heavy calculation within your Model. Laravel allow to create imaginary attributes event when they doesn't really exist
on your table. for that your can just define an attribute accessor in which you perform all your calculation like bellow
class Vente extends Model {
public function getPrixTotalAttribute()
{
return $this->attributes['prix'] * $this->attributes['quantitevendu'];
}
}
And in your view you will have just to do {{ $value->prix_total}}

Laravel foreach in a table. Some <tds> are being skipped

I have a table for some for each loop. For some records, the title is being skipped, even though it exists. Instead of showing null, there is one less td in the row, which skews the table.
Here is the loop
#foreach($comments as $comment)
<tr>
<td> {{ $comment->id }}</td>
#foreach($comment->articles as $article)
<td>{{ $article->title ?: 'No Title' }}</td>
#endforeach
<td>{{ $comment->body ?: 'No Comment' }}</td>
</tr>
#endforeach
Any idea why the title is being skipped in some instances.
You don't have to put the td inside a loop, else you'll end up with messy table, you may need to use a list instead to list the articles:
#foreach($comments as $comment)
<tr>
<td> {{ $comment->id }}</td>
<td> Articles :
<ul>
#foreach($comment->articles as $article)
<li>{{ $article->title ?: 'No Title' }}</li>
#endforeach
</ul>
</td>
<td>{{ $comment->body ?: 'No Comment' }}</td>
</tr>
#endforeach
That will keep your table with the same size, and organize your article.
The number of columns has to match in the table and the code above is not ensuring that. A comment can has 0 to N articles so you will end with a table containing from 2+0 to 2+N columns. Maybe you can try to show the article titles in a table within the 2nd column and replace the foreach with a forelse and use the loop variable. More info at https://laravel.com/docs/5.6/blade
Something like this
#foreach($comments as $comment)
<tr>
<td> {{ $comment->id }}</td>
<td>
#forelse($comment->articles as $article)
#if ($loop->first)
<table>
#endif
#if ($loop->last)
</table>
#endif
<tr><td>{{ $article->title ?: 'No Title' }}</td></tr>
#else
No articles
#endforelse
</td>
<td>{{ $comment->body ?: 'No Comment' }}</td>
</tr>
#endforeach
depends on how you want to display the data, look at this
if comment have 2 article, <td></td> <td></td>
what about next comment have 3 article, <td></td> <td></td> <td></td>
so your code is breaking the table
#foreach($comment->articles as $article)
<td>{{ $article->title ?: 'No Title' }}</td>
#endforeach
Try using the below code you did mistake in ternary condition
Ternary syntax : if(condition ? true : false)
e.g if($article->title=='new' ? $article->title : 'No Title')
#foreach($comments as $comment)
<tr>
<td> {{ $comment->id }}</td>
#foreach($comment->articles as $article)
<td>{{$article->title ? $article->title : 'No Title'}}</td>
#endforeach
<td>{{ $comment->body ? $comment->body : 'No Comment' }}</td>
</tr>
#endforeach
Note : Comment may or maynot have articles so do check for it
e.g
#foreach($comments as $comment)
<tr>
<td> {{ $comment->id }}</td>
#foreach($comment->articles as $article)
#if($article)
<td>{{$article->title ? $article->title : 'No Title'}}</td>
#else
<td>No Article</td>
#endif
#endforeach
<td>{{ $comment->body ? $comment->body : 'No Comment' }}</td>
</tr>
#endforeach
You have to print title if it's true in a ternary operator. if(condition ? true : false).
so condition be like
#foreach($comment->articles as $article)
<td>{{ $article->title != null ? $article->title : 'No Title' }}</td>
#endforeach
if the title will not null it will display title otherwise it will display 'No Title'.
You can change condition as per your requirement. like $article->title != null or $article->title != '' .

Resources