Keeping revision history using VentureCraft/revisionable - laravel

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

Related

Laravel display collection items in view

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

Laravel blade creating url

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]) }}

How to the domain url in laravel5

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() }}

Get image name from database in laravel

I have database, with columns image and alttag. I want to use them in laravel blade view. I try something like this:
{{ HTML::image('images/{{ $item->image }}', $alt="{{ $item->alttag }}") }}
But syntax isn't correct. If i just echo image and alttag like this:
<h1>{{ $item->alttag }}</h1>
then they are correct. I wonder what is wrong in my code.
You used blade syntax in a PHP string. Watch the compiled blade templates to see where you did go wrong.
In short. Try this:
{{ HTML::image('images/'. $item->image, $alt = $item->alttag) }}
or equally short:
{{ HTML::image("images/{$item->image}", $alt = $item->alttag) }}

Laravel templating, don't check if relation exists

I have a relationship between two models which is define by :
class Appartement extends Eloquent {
public function lang()
{
return $this->hasOne('AppartementLang')->where('language_id', '=', '2');
}
}
So nothing big.
When I try to do something like :
#foreach ($appartements as $appartement)
{{$appartement->lang->title}}<br/>
{{$appartement->lang->subtitle}}<br/>
{{link_to('/appartements/'.$appartement->link_rewrite, Lang::get('Discover'))}}<br/>
#endforeach
And that the lang does not exists, then laravel throws an error, which is pretty logical.
I wanted to know if there is some magical way which would allow me not to automate the verification that the relation exists. Basically not turn it into a big "if exists then use" for each property of the class like that :
#foreach ($appartements as $appartement)
#if($appartement->lang){{$appartement->lang->title}}<br/>#endif
#if($appartement->lang){{$appartement->lang->subtitle}}<br/>#endif
{{link_to('/appartements/'.$appartement->link_rewrite, Lang::get('Discover'))}}<br/>
#endforeach
Thanks for the advice on that.
I am sure there are lots of ways to do it (or at least try)
There is this option to check for existing and supply default if none, that I think is new in 4.1 (or at least it is new to me):
#foreach ($appartements as $appartement)
{{ $appartement->lang->title or "default if doesn't exist" }}<br/>
{{ $appartement->lang->subtitle or "default text if doesn't exist" }}<br/>
{{ link_to('/appartements/'.$appartement->link_rewrite, Lang::get('Discover')) }}<br/>
#endforeach
or you could try one of these three if statements if you don't want anything to display for that particular record depending on existance of lang...
#foreach ($appartements as $appartement)
#if ($appartement->lang)
// or maybe:
// #if ($appartement::has('lang'))
// or also maybe:
// #if ($appartement->lang->count())
{{ $appartement->lang->title or "default if doesn't exist" }}<br/>
{{ $appartement->lang->subtitle or "default text if doesn't exist" }}<br/>
{{ link_to('/appartements/'.$appartement->link_rewrite, Lang::get('Discover')) }}
<br/>
#endif
#endforeach

Resources