I'm using a package called cybercog/laravel-love. This package includes a method that returns a collection of users who liked model
$sheet->collectLikers()
When I add this to my view it returns a collection of users just fine but how do I get just the users username for example.
[{"id":1,"first_name":null,"last_name":null,"email":"mem1#email.com","username":"mem1","email_verified_at":null,"created_at":"2018-10-15 16:45:14","updated_at":"2018-10-15 16:45:14"},{"id":2,"first_name":null,"last_name":null,"email":"mem2#email.com","username":"mem2","email_verified_at":null,"created_at":"2018-10-15 16:45:14","updated_at":"2018-10-15 16:45:14"}]
{{ $sheet->collectLikers()->username }} returns nothing as does
{{ $sheet->collectLikers('username') }}
I was able to get this... pretty easy actually. Needed to loop through.
#foreach($sheet->collectLikers() as $userLike)
{{ $userLike->username }}
#endforeach
Related
How to show TeamController#index and ProductController#index both show list of team and product inside one view main.blade.php
Looks like you want to show two datasets on one page. Basically, it means you have to execute two controller methods but it's not necessary to follow each and everything that official documentation says.
For example, if Products belong to a team, you can execute only TeamController#index and show products as given below.
#foreach($teams as $team)
#foreach($team->products as $product)
{{ $product->name }}
#endforeach
#endforeach
If no teams and products are two different entities and does not have any relation, you can just pass teams and products like this:
TeamController.php
public function index()
{
$teams = Team::all();
$products = Product::all(); // Don't forget to include 'use App\Product'
return view('index',compact(['teams','products']);
}
and then you can show teams and products like this:
index.blade.php
#foreach($teams as $team)
{{ $team->name }}
#endforeach
#foreach($products as $product)
{{ $product->name }}
#endforeach
Getting information from two different models does not mean you have to execute two different controller functions.
Still, if you want to get data from two different controllers, you can setup index.blade.php and create two ajax requests that will get data from two different URLs (two different controller methods).
Let me know if you have any more questions.
You can't show results from two controllers like that. Create a view that includes both the view that TeamController#index and ProductController#index return. be aware that both might be extending a layout which will probably try to load your page twice, so keep in mind to split the views into smaller components and include only those.
More info here
https://laravel.com/docs/5.6/views#creating-views
I have a blade view for logged in users
To display my users name and email I do this
<p>{{ Auth::user()->name }}</p><br>
<p>{{ Auth::user()->email }}</p>
But what happens in the backround?
Does Laravel do two querys? One to fetch the users name and one to fetch the email?
I just wonder if I should contionue to do like the above code or just fetch alla data in need in the controller with one query.
It will do only one query. To remind my self of this I usually stick the user in the route and pass to the blade template:
$user = Auth:user();
return view('yourview', ['user' => $user]);
Then in blade you can use:
<p>{{$user->id}}</p>
<p>{{$user->name}}</p>
<p>{{$user->email}}</p>
Either way is fine i just find this way cleaner.
If you want to see all the queries your app runs when you visit a page check out Laravel Debugbar
Laravel Debugbar
I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:
#foreach ($websites as $website)
<a class="websites" href=" {{ asset ($website->name )}}"> {{ asset ($website->name )}}
</a>
#endforeach
Which gives for example: http://localhost/name
Howver links needs to be like this:
http://localhost/website/name how can I add /website into my URL using blade template in laravel?
Try this:
{{ url('website/' . $website->name) }}
This have some improvement on #Laran answer regarding best practices.
You would better use url parameters instead of concatenating the $name parameter
{{ url('website', [$name]) }}
And using named routes will be better to decouple the routing from the views.
// routes/web.php
Route::get('website')->name('website');
and write inside your {{ route('website', [$name]) }}
I have to get the domain url (ex. http://www.example.com/) in laravel blade. I've tried using {{ url() }} but it returns the path to my public directory. Is there any one line function to get this? How do I get the domain in blade? Need help. Thanks.
You can also try
{{ Request::server ("SERVER_NAME") }}
{{ Request::server ("SERVER_NAME") }}
Or go with
{{ Request::root() }}
I have to display history of changes that are made in my app (like update, insert->who did it , what field changed and when). I am using laravel 4 and I also downloaded this. But the problem is that I am not clear of how to use it. Where to put the folder VentureCraft ? and how to get username or id of the person who did the action. Is there any other way to keep track of history in laravel?
EDIT:
Car model
namespace MyApp\Models;
class Car extends Eloquent {
use \Venturecraft\Revisionable\RevisionableTrait;
}
View
#foreach($description->revisionHistory as $history )
<li>{{ $history->userResponsible()->username }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}</li>
#endforeach
and it shows :
Undefined variable: description
Am i missing something in controller?
Check out the README, it looks fairly self explanatory. Install with composer, migrate, then use the RevisionableTrait in your models that you wish to keep the update history of. You can should then be able to get the history of an object with:
$object->revisionHistory;
They even have a simple example of usage within a blade template:
#foreach($account->revisionHistory as $history )
<li>{{ $history->userResponsible()->first_name }} changed {{ $history->fieldName() }} from {{ $history->oldValue() }} to {{ $history->newValue() }}</li>
#endforeach