I am trying to get data from my table and show it in my view and paginate this data. I received this problem and couldn't find any solution. I did the exactly same thing in my previous project and it worked well.
here is my controller
foreach ($userDetails->wanttoread as $key => $bookid) {
$bookDetails = DB::table('books')->where('_id', $bookid)->paginate(10);
array_push($wanttoreadArr, $bookDetails);
}
$data['wanttoread'] = $wanttoreadArr;
return view('frontend.user.userShelve')->with($data);
And this is my view
<tbody>
#foreach($wanttoread as $book)
<tr>
<td><img src="{{ URL::to($book->thumbnailLink) }}" class="img-thumbnail img-responsive"></td>
</tr>
#endforeach
</tbody>
You can solve this in two ways
1 solution-change blade
<tbody>
#foreach($wanttoread as $books)
#foreach($books as $book)
<tr>
<td><img src="{{ URL::to($book["thumbnailLink"]) }}" class="img-thumbnail img-responsive"></td>
</tr>
#endforeach
#endforeach
</tbody>
2 solution chage controller logic(best solution getting paginated result and all time same count items)
$bookDetails = DB::table('books')->whereIn('_id', $userDetails->wanttoread)->paginate(10);
$data['wanttoread'] = $wanttoreadArr;
return view('frontend.user.userShelve')->with($data);
you can try this (Leave your controller code as it is)
<tbody>
#foreach($wanttoread as $row)
#foreach($row->items() as $book)
<tr>
<td><img src="{{ URL::to($book['thumbnailLink']) }}" class="img-thumbnail img-responsive"></td>
</tr>
#endforeach
#endforeach
</tbody>
Assuming that you are getting a LengthAwarePaginator, you need to get the items that are currently being paginated, so you could do the following:
<tbody>
#foreach($wanttoread->items() as $book)
<tr>
<td><img src="{{ URL::to($book->thumbnailLink) }}" class="img-thumbnail img-responsive">
</td>
</tr>
#endforeach
The items() function will return the dataset. You can find out more at the official API docs.
Related
I have Many To Many relationships 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')->withPivot('balance');;
}
Then at Blade, I tried this:
#forelse($user->wallets as $wallet)
<tr>
<td>{{ $wallet->name }}</td>
<td>{{ $wallet->pivot->balance }}</td>
<td></td>
</tr>
#empty
<td colspan="5" class="text-center">No wallet exist</td>
#endforelse
As you can see I have passed two parameters as route name user.WalletTransaction which is a link:
{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$wallet->users()->usr_id]) }}
And on web.php:
Route::get('wallet/transaction/{wallet}/{user}', 'Wallet\UserWalletController#WalletTransaction')->name('user.WalletTransaction');
But it shows me this error:
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$usr_id
However usr_id is the id of users at users table:
And the pivot table structure, user_wallet, goes like this:
So how can I properly add the user id to that link?
I would really appreciate any idea or suggestion from you guys about this...
Thanks.
UPDATE #1:
Result of <a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" /> shows two links!
$wallet->users() returns a collection, so you need to iterate over it like you're iterating over wallets
#forelse($wallet->users() as $user)
<td>
<a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
</td>
[...]
I have a many-to-many relationship between my User and Wallet models.
Wallet Model
public function users()
{
return $this->belongsToMany(User::class, 'user_wallet', 'user_id', 'wallet_id');
}
User Model
public function wallets()
{
return $this->belongsToMany(Wallet::class, 'user_wallet', 'user_id', 'wallet_id')->withPivot('balance');;
}
Then in my Blade file, I tried the following.
#forelse($user->wallets as $wallet)
<tr>
<td>
{{ $wallet->name }}
</td>
<td>
{{ $wallet->pivot->balance }}
</td>
<td>
<a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
</td>
</tr>
#empty
<td colspan="5" class="text-center">No wallet exist</td>
#endforelse
It works fine, but the problem is, it shows two links in the table for one user. However, it should be showing one.
And if I do:
#forelse($user->wallets as $wallet)
{{ count(array($user)) }}
#empty
<td colspan="5" class="text-center">
No wallet exist
</td>
#endforelse
The result would be 1.
The problem is coming from 'userId' => $user->id of the link. Because if I remove it, only 1 link would appear, which is correct. But still, I do need the userId and don't know how to pass that into the route name.
So what's going wrong here? I need one link for each user, and I don't know why it prints two of them.
#forelse($user->wallets as $wallet)
<tr>
<td>
{{ $wallet->name }}
</td>
<td>
{{ $wallet->pivot->balance }}
</td>
<td>
<a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
</td>
</tr>
#empty
<td colspan="5" class="text-center">No wallet exist</td>
#endforelse`
Doesn't $user->wallets returns collection of all wallets associated with each user?
maybe you need to check if any user has more than one wallet or if you want to show only one you can user $user->wallets[0] to show the first wallet model since its a collection
Hello i'm trying to display the authenticated user but i'm getting this error :
Undefined variable: users (View:
C:\wamp64\www\Blan\resources\views\users\index.blade.php)
this is my code :
usercontroller :
public function index()
{
return view('users.index')->with('user', Auth::user());
}
the view :
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-header">
Update Your Profile
</div>
<div class="card-body">
<table class="table table-hover">
<thead>
<th>Image</th>
<th>Name</th>
<th>Update</th>
</thead>
<tbody>
#if( $users -> count() > 0 )
#foreach ($users as $user)
<tr>
<td>
#if(isset($user->profile->avatar))
<img src="{{ asset($user->profile->avatar)}}" width="60px" height="60px" style="border-radius: 50%" alt="">
#else
No image
#endif
</td>
<td>
{{$user->name}}
</td>
<td>
Update
</td>
</tr>
#endforeach
#else
<tr>
<th colspan="5" class="text-center">No Users </th>
</tr>
#endif
</tbody>
</table>
</div>
</div>
#stop
But if i return all the users using this code in the controller :
return view('users.index')->with('users', User::all());
its work fine.
so how i can return only the current authenticated user ?
You don't need to send the auth user via your controller.Auth is the global facade in laravel.Also you don't need foreach because there is only 1 authenticated user. Just type Auth::user()->name in the blade
where you want to display the user
Yea and btw the error ur getting is because you are doing foreach for $users but sending $user to blade but im pretty sure it wont work even if you correct that typo
In your example
public function index()
{
return view('users.index')->with('user', Auth::user());
}
You have used "user"
But tried to access variable as "users"
Unless you just made a typo in your example?
I am using Laravel 5.4 and I want to view my data in database from my view page (listpetani.blade.php).
Here is the code of my project:
HTML:
<div class="table-responsive">
<table class="table table-striped table-hover table-condensed">
<thead>
<tr>
<th><strong>No</strong></th>
<th><strong>Nama Petani</strong></th>
<th><strong>Alamat</strong></th>
<th><strong>No. Handphone</strong></th>
<th><strong>Lokasi</strong></th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</tbody>
</table>
</div>
PHP:
In my listpetani.blade.php I have an empty table and I want to show data from database tbl_user:
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
return view('listpetani', ['petani' => $petani]);
});
And the table in my page: view in browser
I want to show all the data from database into my view in laravel 5.4. Can anybody help me?
[SOLVE]
Thank you guys, I already solve this problem
This is the solved code
web.php (routes)
Route::get('listpetani', function () {
$petani = DB::table('tbl_user')->get();
return view('listpetani', ['petani' => $petani]);
});
and in my listpetani.blade.php
#foreach($petani as $key => $data)
<tr>
<th>{{$data->id_user}}</th>
<th>{{$data->nama_user}}</th>
<th>{{$data->alamat}}</th>
<th>{{$data->no_telp}}</th>
<th>{{$data->id_lokasi}}</th>
</tr>
#endforeach
You can get data from database in view also
#php( $contacts = \App\Contact::all() )
#php( $owners = \App\User::all())
<select class="form-control" name="owner_name" id="owner_name">
#foreach($contacts as $contact)
<option value="{{ $contact->contact_owner_id }}">{{ $contact->contact_owner }}</option>
#endforeach
</select>
It would be better if you pass data from controller to view.
return view('greetings', ['name' => 'Victoria']);
Checkout the docs:
https://laravel.com/docs/8.x/views#passing-data-to-views
Alternatively you can use #forelse loop inside laravel blade
#forelse($name as $data)
<tr>
<th>{{ $data->id}}</th>
<th>{{ $data->name}}</th>
<th>{{ $data->age}}</th>
<th>{{ $data->address}}</th>
</tr>
#empty
<tr><td colspan="4">No record found</td></tr>
#endforelse
In your controller:
$select = DB::select('select * from student');
return view ('index')->with('name',$select);
In Your view:
#foreach($name as $data)
<tr>
<th>{{ $data->id}}</th> <br>
<th>{{ $data->name}}</th> <br>
<th>{{ $data->age}}</th> <br>
<th>{{ $data->address}}</th>
</tr>
#endforeach
I hope this can help you.
#foreach($petani as $p)
<tr>
<td>{{ $p['id_user'] }}</td>
<td>{{ $p['username'] }}</td>
<td>{{ $p['alamat'] }}</td>
<td>{{ $p['no_telp'] }}</td>
<td>{{ $p['id_lokasi'] }}</td>
</tr>
#endforeach
**In side controller you pass this **:
$petanidetail = DB::table('tb1_user')->get()->toArray();
return view('listpetani', compact('petanidetail'));
and Inside view you use petanidetail variable as follow:
foreach($petanidetail as $data)
{
echo $data;
}
I hope you already know this, but try use Model and Controller as well,
Let the route take the request, and pass it to controller
and use Eloquent so your code can be this short:
$petani = PetaniModel::all();
return view('listpetani', compact('petani));
and instead using foreach, use forelse with #empty statement incase your 'listpetani' is empty and give some information to the view like 'The List is Empty'.
Try this:
$petani = DB::table('tbl_user')->pluck('id_user', 'username', 'alamat', 'no_telp', 'id_lokasi');
return view('listpetani', ['petani' => $petani]);
on your view iterate $petani using foreach() like:
foreach($petani as $data)
{
echo $data->id_user; // $petani is a Std Class Object here
echo $data->username;
}
Other answers are correct, I just want to add one more thing, if your database field has html tags then system will print html tags like < p > for paragraph tag. To remove this issue you can use below solution:
You may need to apply html_entity_decode to your data.
This works fine for Laravel 4
{{html_entity_decode($post->body)}}
For Laravel 5 you may need to use this instead
{!!html_entity_decode($post->body)!!}
I currently am using a Laravel View with the following HTML that displays data onto a table using the <td> tag.
#if($unrostered)
<table class="table table-hover table-striped">
<thead>
<tr>
<th>#</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
#endif
I am currently just using one associative list called $unrostered which contains the key and value called name and profile. This shows two columns of data, like this:
As you can see, the name variable is the name of the person, and the profile is a URL that links to their profile.
My HTML skills aren't good. I want to pass in multiple lists [of the same length] into my HTML table, so each list occupies their own column.
I tried juggling the row and data tags, however, all I was able to achieve were that all the data points occupied only one row instead of columns.
Assume the name of your other lists are $list1, $list2, ... and they have the same format as your $unrostered. Then your foreach code should be changed to:
<?php $index = 1; ?>
#foreach($unrostered as $name => $profile)
<tr>
<td>
<?= $index; $index++; ?>
</td>
<td>
<a
href="{{ $profile }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list1[$name] }}">{{ $name }}
</a>
</td>
<td>
<a
href="{{ $list2[$name] }}">{{ $name }}
</a>
</td>
</tr>
#endforeach