public function student(){
$users=DB::select('select * from details');
return view('welcome',['users'->$users]);
}
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email}}</td>
<td>{{ $user->status}}</td>
<td>{{ $user->subject}}</td>
</tr>
#endforeach
don't use raw sql query instead you can do it like this
public function student()
{
$users=Details::all();
return view('welcome',compact('users'));
}
or like this
public function student()
{
$users = DB::table('details')
->select('*')
->get();
return view('welcome', compact('users'));
}
and then in your blade you could get it like this
#isset($users)
#foreach ($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->status }}</td>
<td>{{ $user->subject }}</td>
</tr>
#endforeach
#endisset
How can I get the result as drawn on the right
in Controller
$programs = Program::with(['activities', 'activites.subactivities'])->get();
in Blade View
#foreach ($programs as $programKey => $program)
#foreach ($program->activities as $activity)
#foreach ($activity->subactivities->toArray() as $subactivity)
<tr>
<td>{{ $programKey + 1 }}</td>
<td>{{ strtoupper($program->name) }}</td>
<td>{{ $activity->name }}</td>
<td>{{ $subactivity['name'] }}</td>
</tr>
#endforeach
#endforeach
#endforeach
I would say to save in a variable and if different the show
Like this:
#foreach ($programs as $programKey => $program)
#php
$newProgram = true;
#endphp
#foreach ($program->activities as $activityKey => $activity)
#php
$newActivity = true;
#endphp
#foreach ($activity->subactivities->toArray() as $subactivity)
<tr>
<td>
#if($newProgram == true)
{{ $programKey + 1 }}
#endif
</td>
<td>
#if($newProgram == true)
{{ strtoupper($program->name) }}
#php
$newProgram = false;
#endphp
#endif
</td>
<td>
#if($newActivity == true)
{{ $activity->name }}
#php
$newActivity = false;
#endphp
#endif
</td>
<td>
{{ $subactivity['name'] }}
</td>
</tr>
#endforeach
#endforeach
#endforeach
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'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?
I am using laravel 5.2 and I am facing some issue with pagination link. When I am adding this code:
{{ $wachat->links() }}
After foreach I am getting this error:
Call to undefined method Illuminate\Database\Query\Builder::links() (View: /Applications/MAMP/htdocs/kc/kyo-webservice/resources/views/wechat/show.blade.php)
In my controller I am using this code:
public function wechatshows($d1, $id, $name, Request $request)
{
$users = Keyuser::where('imception_id', '=', $d1)->first();
$user = UserKey::where('id', '=', $id)->first();
if ($name == "all") {
$wachat = Wechat::where('key', '=', $d1)->orderBy('id', 'DESC')->paginate(5);
} else {
$wachat = Wechat::where('key', '=', $d1)->where('groupName', '=', $name)->orderBy('id', 'DESC')->paginate(5);
}
$wechatcontact = Wechat::select('groupName')->groupBy('groupName')->get();
return view('wechat.show', ['wachat' => $wachat, 'user' => $user, 'product' => $d1, 'users' => $users, 'keyUser' => $d1, 'wechatcontact' => $wechatcontact]);
}
I am using this code in my views:
<tbody id="chatListBody" style="overflow-y: scroll;">
#foreach ($wachat as $wachat)
<tr>
<input type="hidden" class="wechatname" data-id="{{ $wachat->id }}">
<td>{{ $wachat->id }}</td>
<td>{{ $wachat->wxid }}</td>
<td>{{ $wachat->username }}</td>
#if ($wachat->type === 'image')
<td><a class="example-image-link" href="{{ url('/images/'.$wachat->imgPath.'') }}" data-lightbox="{{$wachat->imgPath}}"
data-title="{{$wachat->imgPath}}"><img class="imgwhatsapp" data-lightbox="roadtrip" src="{{ url('/images/'.$wachat->imgPath.'') }}"></a></td>
#elseif ($wachat->type === 'video')
<td>{{$wachat->imgPath}}</td>
#elseif ($wachat->type === 'audio')
<td> {{$wachat->imgPath}} </td>
#elseif ($wachat->type === 'text')
<td>{{$wachat->message}}</td>
#else
<td>no data</td>
#endif
<td>{{ $wachat->created_at }}</td>
</tr>
#endforeach
</tbody>
{{ $wachat->links() }}
</div>
</table>
When I am putting {{ $wachat->links() }} above foreach it is working I don't know why it is happening.
Perhaps, $wachat is getting override inside #foreach($wachat as $wachat). so, it can not find links() method.
you should use #foreach($wachat as $singleWachat) or as per your convenience.
When you loop through your collection, Your collection become object. After the loop when you do {{ $wachat->links() }}. Your Code try to paginate a object and error shown.
Avoid this kind of problem try to use your collection variable name plural and your object name singular.
public function wechatshows($d1, $id, $name, Request $request)
{
$users = Keyuser::where('imception_id', '=', $d1)->first();
$user = UserKey::where('id', '=', $id)->first();
if ($name == "all") {
$wachats = Wechat::where('key', '=', $d1)->orderBy('id', 'DESC')->paginate(5);
} else {
$wachats = Wechat::where('key', '=', $d1)->where('groupName', '=', $name)->orderBy('id', 'DESC')->paginate(5);
}
$wechatcontact = Wechat::select('groupName')->groupBy('groupName')->get();
return view('wechat.show', ['wachats' => $wachats, 'user' => $user, 'product' => $d1, 'users' => $users, 'keyUser' => $d1, 'wechatcontact' => $wechatcontact]);
}
and
<tbody id="chatListBody" style="overflow-y: scroll;">
#foreach ($wachats as $wachat)
<tr>
<input type="hidden" class="wechatname" data-id="{{ $wachat->id }}">
<td>{{ $wachat->id }}</td>
<td>{{ $wachat->wxid }}</td>
<td>{{ $wachat->username }}</td>
#if ($wachat->type === 'image')
<td><a class="example-image-link" href="{{ url('/images/'.$wachat->imgPath.'') }}" data-lightbox="{{$wachat->imgPath}}"
data-title="{{$wachat->imgPath}}"><img class="imgwhatsapp" data-lightbox="roadtrip" src="{{ url('/images/'.$wachat->imgPath.'') }}"></a></td>
#elseif ($wachat->type === 'video')
<td>{{$wachat->imgPath}}</td>
#elseif ($wachat->type === 'audio')
<td> {{$wachat->imgPath}} </td>
#elseif ($wachat->type === 'text')
<td>{{$wachat->message}}</td>
#else
<td>no data</td>
#endif
<td>{{ $wachat->created_at }}</td>
</tr>
#endforeach
</tbody>
{{ $wachats->links() }}
</div>
</table>