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?
Related
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.
I have done one-to-many relationships but when I loop through it in my view only one subject displays even when I have more than one subjects.
This is what I have done in my controller
$broadsheet = Broadsheet::with(['subject', 'session', 'term', 'student', 'classarm'])
->where('session_id', $request->sessions)
->where('term_id', $request->terms)
->where('class_arm_id', $request->classarms)
->selectRaw('sum(total) as totals, count(subject_id) as countrow, student_id, subject_id, session_id, term_id, class_arm_id, total')
->groupBy('student_id')
->orderBy('totals', 'desc')
->get();
return view('broadsheet.index')->with('broadsheet', $broadsheet);
This is the index view
#extends('layouts.app')
#section('content')
{{--#foreach($broadsheet as $subjects)
#foreach ($subjects->subject as $sub)
<th> {{ strtoupper($sub->subject->subject_name) }}</th>
#endforeach
#endforeach--}}
<div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
#foreach($broadsheet as $subject)
<th> {{ strtoupper($subject->subject->subject_name) }}</th>
#endforeach
<th>TOTAL</th>
<th>AVERAGE</th>
<th>POSITION</th>
</tr>
</thead>
<tbody>
#foreach($broadsheet as $result)
<tr>
<td>{{ $result->total }}</td>
<td>{{ $result->totals }}</td>
<td>{{ number_format($result->totals/$result->countrow,2) }}</td>
<td>{{ $i++ }}</td>
<td>{{ $result->exams }}</td>
<td>{{ $result->total }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
and this is my Broadsheet schema
class Broadsheet extends Model
{
use HasFactory;
protected $fillable = [
'student_id',
'subject_id',
'session_id',
'term_id',
'class_arm_id',
'subject_total',
];
public function student()
{
return $this->belongsTo(Student::class, 'student_id');
}
public function subject()
{
return $this->belongsTo(Subject::class, 'subject_id');
}
public function session()
{
return $this->belongsTo(Session::class, 'session_id');
}
public function term()
{
return $this->belongsTo(Term::class, 'term_id');
}
public function classarm()
{
return $this->belongsTo(ClassArm::class, 'class_arm_id');
}
}
I want to display students results like this
Intended result
Please, I would really need assistance to get this working as any help would be greatly appreciated.I just started learning laravel recently. I don't really know where the issue is coming from
I am working in Laravel Framework, i have tried to fetch the image from the database. But it's not getting pulled properly from the DB, can anyone help me on this?
Here is my code-
public function getAllPost()
{
$posts = DB::table('posts')
->join('categories','posts.category_id','categories.id')
->select('posts.*','categories.name')
->get();
return view('posts.all_posts', compact('posts'));
}
public function getIndividualPosts($id)
{
$posts = DB::table('posts')->
join('categories','posts.category_id','categories.id')
->select('posts.*','categories.name')
->where('posts.id', $id)->first();
return view('posts.view_post', compact('posts'));
}
view.blade.php
#foreach($posts as $post)
<tr>
<td>{{ $post-> id}}</td>
<!--The name came from the categories table-->
<td>{{ $post-> name}}</td>
<td>{{ $post-> title}}</td>
<td>{{ $post-> details}}</td>
<td><img src="{{ URL::to($post->image)}}" style="height: 70px; width: 100px"></td>
<td>
Edit
Delete
View
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
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
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