I'm trying to get value from my tables with many to many relationship.
My relation are:
User:
public function roles()
{
return $this->belongsToMany('App\Role',"users_roles","usersid","rolesid");
}
Role:
public function users()
{
return $this->belongsToMany('App\User',"users_roles","usersid","rolesid");
}
I did this in my view:
#foreach($users as $user)
<?php $i++; ?>
<tr>
<td>{{ $i }}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->roles->role}}</td>
<tr>
#endforeach
I'm getting following error:
Undefined index: role
But when i do {{$user->roles}}, I get following:
[{"id":1,"role":"Administrator","created_at":null,"updated_at":null,"pivot":{"usersid":4,"rolesid":1}}]
Can anyone tell me where did i go wrong?
You can do
<td>
#if($user->roles)
{{$user->roles->first()->role}}
#else
No role for this user
#endif
</td>
You need to use Eager load when you are fetching the users
Related
I have a Many To Many relationship between User Model & Wallet Model:
Wallet.php:
public function users() {
return $this->belongsToMany(User::class,'user_wallet','user_id','wallet_id');
}
And User.php:
public function wallets() {
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id');
}
And the pivot table of this relationship goes like this:
So I can properly show Wallet name at the Blade:
#forelse($user->wallets as $wallet)
<tr>
<td>
{{ $wallet->name }}
</td>
<td>
{{ $wallet->balance }}
</td>
</tr>
#empty
<td colspan="5" class="text-center">
No wallet exist
</td>
#endforelse
But the wallet balance data does not appear (because it's in the pivot table and not the wallets table).
So how to show this custom column in this case?
use withPivot and mention additional column name
public function wallets()
{
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');
}
then in view
<td>{{ $wallet->pivot->balance }}</td>
I'm trying to make a show view with Laravel 8 but i can't show the detail, this is the code from the controller:
public function show($id)
{
$accesorios=DB::table('accesorio as acc')
->join('detalle_aparato as da','acc.idAccesorio','=','da.idAccesorio')
->select('acc.Nombre')
->where('da.idAparato','=',$id);
return view("almacen.aparato.show",["accesorio"=>Accesorio::findOrFail($id)]);
}
And this is the code from the view:
#foreach ($accesorio as $acc)
<tr>
<td>{{ $acc->Nombre}}</td>
</tr>
#endforeach
Error message
When I use:
#foreach ($accesorio as $acc)
<tr>
<td>{{ $acc}}</td>
</tr>
#endforeach
It prints: 1 for each record
Hope you can help me
In you're controller you're using DB::Table to set the $accesorios variable, but never using it.
You then are setting accesorio in your view to Accesorio::findOrFail($id) which will only return one instance of the object.
Either pass $accessorios into your view
return view("almacen.aparato.show",["accesorios"=>$accessorios]);
then loop through it
#foreach ($accesorios as $acc)
<tr>
<td>{{ $acc->Nombre}}</td>
</tr>
#endforeach
or since you're just sending one instance of the object to the view, remove the loop and you can render it like this.
<tr>
<td>{{ $accesorio->Nombre }}</td>
</tr>
I have my User Table, which has a ID and a comes_from (id), which points to the id column of the same user table.
for example:
User
id, comes_from
1, null
2, 1
So user with ID 2 comes from user with ID 1
Now I'm trying to build this.
I want to print out a table with all users I have and another table column which gives me the email of the user, that the current row user comes from.
My User model:
public function recruited()
{
return self::findOrFail($this->comes_from)->pluck('email', 'id');
}
Now in my Controller:
public function recruitedUsersList()
{
return view('admin.recruitedUsers')->with('users', User::all()->with('recruited'));
}
This tells me "Call to undefined method with"
In my blade:
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->recruited()->email }}</td>
</tr>
#endforeach
Well.. this doesn't work for me. Does someone has a idea?
The Idea is:
A user can come from only one person. He get the "comes_from (id) as he register himself"
A User can recruite as many people as he wants.
Note: I assume a person can recruit more than one other person in your application. So I think hasMany relationship best describes this.
Update the function in your User Model
public function recruited()
{
return $this->hasMany('App\User', 'comes_from');
}
Update your controller to this
public function recruitedUsersList()
{
return view('admin.recruitedUsers')->with('users', User::with('recruited')->get());
}
Update your view logic to this
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->name }}</td>
<td>
#foreach($user->recruited as $recruit)
{{ $recruit->email }},
#endforeach
</td>
</tr>
#endforeach
I have two tables one table for general information about the order named orders and a detail table for information like products, notes and delivery date.
Basically, I want to show in the same page the table orders and for each order can be multiple products, notes, dates from the table detail, and also I want to paginate in order to show exactly 20 orders on each page and every product, note, date row with the same id_order.
In conclusion, on each page there are 20 orders from table orders but there can be 40 rows from table detail with products, notes, delivery date, because one order can contain more than one product. in fact I want to use rowspan html in order to show on the same row the detail.
This is my Orders model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class order extends Model
{
public $timestamps = false;
protected $fillable = ['client','number_order','file'];
public function details()
{
return $this->hasMany('App\Detail','orders_id')->orderBy('id', 'desc');
}
}
Detail model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Detail extends Model
{
protected $table = 'detail';
public function order()
{
return $this->belongsTo('Detail','orders_id');
}
}
My controller:
public function index()
{
$orders = Order::first(); //This works only with one record I cant show all records, all() is not working
$orders->setRelation('details', $orders->details()->paginate(10));
return view('orders.index', compact('orders'));
}
View:
<tbody>
#foreach ($orders->details as $detail)
<tr id="{{ $detail->orders_id }}">
<td>{{ $detail->product }}</td>
</tr>
#endforeach
</tbody>
You can use eager load details on order like this
public function index()
{
$orders = Order::with('details')->paginate(20);
return view('orders.index', compact('orders'));
}
In view
<table>
<tbody>
#foreach($orders as $order)
<tr>
<td>{{ $order->created_at }}</td>
<td>... put any order info here</td>
<td>
<label>Order Details:</label>
<table>
#foreach ($order->details as $detail)
<tr id="{{ $detail->orders_id }}">
<td>{{ $detail->product }}</td>
<td>{{ $detail->note }}</td>
</tr>
#endforeach
</table>
<td>
</tr>
#endforeach
</tbody>
</table>
I have 3 tables which are user->id, name,password, roles->id,role, role_user->role_id, user_id
Now if I want to show the associated data in view page means role related to user ..How i do it in view?
means I want to show if the user has role it will be checked otherwise it will be unchecked
Here is my controller :
$users = User::all();
$roles = Role::all();
return view("Permission::assign_role",compact('users','roles'));
View page
<table class="table">
<thead>
<tr>
<th>SL No</th>
<th>Name</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach($users->roles() as $row)
<tr>
<td>{{$i}}</td>
<td>{{$row->name}}</td>
<td><input type="checkbox" name="role_id" checked> $role->role</td>
</tr>
<?php $i++; ?>
#endforeach
</tbody>
</table>
Eager load the data:
$users = User::with('roles')->get();
return view("Permission::assign_role", compact('users'));
And in the view, you can do something like this if you want to check if user has specific role:
#if ($user->roles->contains($roleId))
If you want to just iterate over user's roles:
#foreach($users->roles as $role)
{{ $role->name }}
This how I am using for my applications :
User Model :
public function roles() {
return $this->belongsToMany(Role::class);
}
Role Model :
public function user() {
return $this->belongsToMany(User::class);
}
In My Controller :
$users = User::all();
In My View
#foreach($users as $user)
#foreach($user->roles() as $role)
{{$role->name}}
#endforeach
#endforeach