there does any one know how to change collection variable into Corban diffForHumans instance... e.g.
{!! $item['date']->diffForHumans() !!}
it gives an error of Call to a member function diffForHumans() on string
You could just create the object on the fly with Carbon::parse():
{{ Carbon::parse($item['date'])->diffForHumans() }}
Related
I have one to many relationships and need to show using where conditions.
When I use findOrFail() it's working as well.
$foo = Model::findOrFail(1);
on my template blade
#foreach($foo->bars as $index=>$bar)
{{ $bar->name }}
#endforeach
on my code above, it's working. but the reference to an id, that's not what I need.
I need it using where conditions. like this:
$foo = Model::where('conditon', 1)->get();
then I call it on my blade template with
#foreach($foo->bars as $index=>$bar)
{{ $bar->name }}
#endforeach
then I get an error:
ErrorException (E_ERROR) Property [bars] does not exist on this collection instance.
It seems after get() I cannot call child with $foo->bars
How do you get this to work?
The findOrFail() method returns an instance of the "Model".
The get() method returns a collection of instances of the "Model" even if there is only one result.
if you want just one result, use first() instead of get().
$foo = Model::where('conditon', 1)->first();
then in the blade template do
#if($foo)
#foreach($foo->bars as $index=>$bar)
{{ $bar->name }}
#endforeach
#endif
if you need multiple results, do another foreach().
#foreach($foo as $oneFoo)
#foreach($oneFoo->bars as $index=>$bar)
{{ $bar->name }}
#endforeach
#endforeach
if you are going with the "multiple" solution, i suggest you name your variable "foos".
$foos = Model::where('conditon', 1)->get();
and so
#foreach($foos as $foo)
#foreach($foo->bars as $index=>$bar)
{{ $bar->name }}
#endforeach
#endforeach
Try to use ->first() instead of ->get().
Because ->get() retrieve multiple results which fit the query criteria.
You can either loop through $foo results or use ->first() to retreive the first query match.
This is my helper function one which render input field in blade
{!! Helpers::render_input('settings[companyname]','Company Name',Helpers::get_option('companyname'),'text',array('autofocus'=>true)) !!}
I also tried this way, but it is not working.
{!! Helpers::render_input('settings[companyname]','Company Name',self::get_option('companyname'),'text',array('autofocus'=>true)) !!}
I am calling other function Helpers::get_option('companyname') in above function.Both function individually working fine.So i am finding a way to call one helper function within another helper function in laravel blade.
Is there anyway to call function this way?
I am showing data to back end users in a tabular format. I am using Laravel 5 and pagination can be handled with ease.
There is a small requirement when it comes to where to place those links of pages: I want to float them right. Considering that Laravel is using Bootstrap 3 to render the layout, I know I can simply add class "pull-right" into the element.
So I constructed a custom pagination presenter like so:
namespace App\Http\Presenters;
use Illuminate\Pagination\BootstrapThreePresenter;
class DatatablePaginationPresenter extends BootstrapThreePresenter{
public function render()
{
if ($this->hasPages())
{
return sprintf(
'<ul class="pagination pull-right">%s %s %s</ul>',
$this->getPreviousButton(),
$this->getLinks(),
$this->getNextButton()
);
}
return '';
}
}
And the code in the template file:
<div class="row">
<div class="col-md-4">Some text</div>
<div class="col-md-8">
{!! with(new App\Http\Presenters\DatatablePaginationPresenter($articles))->render() !!}
</div>
</div>
Laravel works without errors until I add some parameters in those links by calling appends(), for example:
{!! with(new App\Http\Presenters\DatatablePaginationPresenter($articles))->appends(['sort' => $column,'order' => $order,'term' => $term])->render() !!}
This time I got a FatalException saying Call to undefined method App\Http\Presenters\DatatablePaginationPresenter::appends()
I walked through some source code to find out how appends() works. It is declared in the \Illuminate\Contracts\Pagination\Paginator interface, and any class implements this interface should define it. Given that my Article class extends Eloquent, getting a paginated collection should get a paginator which already implements appends().
So it is really weird that appends() is not defined.
Here is the code of my repo/service layer returning paginated data to my controller.
$articles = Article::with('category')
->select($columns)
->orderBy($column,$order)
->paginate($itemPerPage);
return $articles;
I checked the Laravel source again and found out what's going wrong.
It was my mistake that thinking BootstrapThreePresenter implements the Paginator Interface, but it does not. So it is wrong to call appends() on Presenter object.
{!! with(new MyPresenter($paginatorObject))->appends(array())->render() !!}
Instead, I have to call appends() on a Paginator instance first, in my case the $articles, then pass it to custom presenter constructor.
{!! with(new MyPresenter($paginatorObject->appends(array())))->render() !!}
I would like to store my application settings in database.
In order to get a variable in template, I'm currently using
{{ Config::get('file.variable) }}
and settings are stored in config/file
I would like to create controller SettingsController with public static get and set methods and get variables in template in this way:
{{ Settings::get('var_name') }}
instead of
{{ SettingsController::get('var_name') }}
But I'm getting error: Class 'Settings' not found.
I've tried to set routes:
Route::controller('Settings', 'SettingsController'); and
Route::resource('Settings', 'SettingsController');
But none of the methods works.
Any ideas how to solve this problem?
This should be done using facades, which is already answered here:
How to create custom Facade in Laravel 4
I'm having a bit of an issue trying to access all of a objects properties.
In my UsersController:
public function edit($id)
{
return View::make('users.edit')->with('user', User::find($id));
}
In users/edit view I can access only some of the objects properties such as {{ $user->username }} and {{ $user->email }} however, I cannot access {{ $user->id }} or {{ $user->role_id }} ... the app complains about trying to get the property of a non-object. On the other hand if I use {{ dd($user->id) }} it returns the correct value as expected. Being new to Laravel and Eloquent I'm at a loss for why this might be.
Any help is appreciated.
Try change variable name user to something else. Some frameworks has $user variable reserved in they templating system.
It turns that I inadvertently overwrote the $user object which resulted in having access to only some of the object attributes. The non-object error made sense considering the second $user was no long the user object returned from the controller. It was a silly mistake and I feel foolish for overlooking it in the first place. Thank you everyone for the input!