How to call controller function inside blade php? - php

Im trying to figure out how to call function from controller.
When I call this in blade php its working fine
Todo::where('is_done', false)->count();
But Im trying to make less mess in blade php and call it like
{{ Todo::isDone(); }}
What im trying in controller is
public static function isDone()
{
return Todo::where('is_done', false)->count();
}
Getting error
Call to undefined method App\Models\Todo::isDone()

You could put it on the model or just use the full namespace. Try
{{ App\Http\Controllers\Todo::isDone() }}

Related

Laravel: When I pass a model to a controller it is always null. Why?

I'm trying to change my ways in Laravel, but I find it quite frustrating
Normally, in a controller I'd write something like this
public function edit($id) {
$question = Question::findOrFail($id);
return view('question.edit', compact('question');
}
This obviously works. In the HTML the route that calls this is {{ route('question.edit', $question->id) }}. Now I want to use the method it is written by Artisan when you create the controller. If I do:
public function edit(Question $question) {
return view('question.edit', compact('question');
}
This doesn't work (of course I'm changing the blade directive to {{ route('question.edit', $question) }}), this always passes an empty Question model, it doesn't have id or any of the other fields that were accessible in the blade file. If I do a dd() in the blade file, it'll show the correct model, when passed to the Controller is empty.
What am I doing wrong?
You need to match your type hinted variable name to the name of the route parameter if you want Implicit Model Binding to work, otherwise you are just asking for a dependency and it will inject a new instance of that model:
// vvvvvvvv
Route::get('question/{question}/edit', 'YourController#edit');
// vvvvvvvv
public function edit(Question $question)

Laravel 7 variable not passed to view

I have a strange error:
i pass a simple var from controller to view like this:
namespace App\Http\Controllers\BE;
class User extends Controller
{
// some stuff here
public function sendPasswordReminder(Request $request) {
return response()
->view('BE.user.passwordreset', ['name' => 'James'], 200);
// very simple, nothing special here, also tried return view() stuff
}
}
and an absolutly basic blade:
#extends('BE.templates.main')
#section('content')
{{ $name }}
#endsection
and i get the response
Facade\Ignition\Exceptions\ViewException
Undefined variable: name (View: /Users/modii/Work/l7/distr/resources/views/BE/user/passwordreset.blade.php)
i ended up with this absolute basic version while trying to figure out what is wrong. this should be very basic. its not working. tried to clear cache both with artisan and manually. no result.
i have NO IDEA what could be wrong. If anyone has an idea...

Variable is undefined Make the variable optional in the blade template after using view composer

I've been trying to define a variable globally into my project (laravel 7). I need this variable be available in all views but i am getting following error:
$count is undefined
Make the variable optional in the blade template. Replace {{ $count }} with {{ $count ?? '' }}
First step i created a new service provider TestSeriviceProvider
Second step I added following line to congif\app.php
App\Providers\TestServiceProvider::class,
Then i wrote following codes into boot method of TestServiceProvider
public function boot()
{
View::composer('*', function ($view) {
$view->with('count', 333);
});
}
Which part have i made mistake?
This can be done in the boot() method of the AppServiceProvider.
Just add view()->share('count', 333); and your $count is accessible on any blade page. If you have many shared data implemented, yes you can make a separate ServiceProvider for this. But keep in mind that your shared data may be overwritten in controllers.
Depending on the reason why you want to do this, you can look into a combination of middleware and the session() helper or combine shared variables in an array.

Call one helper function within another helper function in laravel blade

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?

How do I use a controller for a "partial" view in Laravel?

Here is my situation. I have a layout.blade.php which most of my pages use. Within this file, I have some partial pieces that I include, like #include('partials.header'). I am trying to use a controller to send data to my header.blade.php file, but I'm confused as to exactly how this will work since it is included in every view that extends layout.blade.php.
What I am trying to do is retrieve a record in my database of any Game that has a date of today's date, if it exists, and display the details using blade within the header.
How can I make this work?
I think to define those Game as globally shared is way to go.
In your AppServiceProvider boot method
public function boot()
{
view()->composer('partials.header', function ($view) {
view()->share('todayGames', \App\Game::whereDay('created_at', date('d')->get());
});
// or event view()->composer('*', Closure) to share $todayGames accross whole blade
}
Render your blade as usual, partial.header blade
#foreach ($todayGames as $game)
// dostuffs
#endforeach
In Laravel you can create a service class method that acts like a controller and use #inject directive to access this in your partial view. This means you do not need to create global variables in boot(), or pass variables into every controller, or pass through the base view layout.blade.php.
resources/views/header.blade.php:
#inject('gamesToday', 'App\Services\GamesTodayService')
#foreach ($gamesToday->getTodayGames() as $game)
// display game details
#endforeach
While it's different value you retrieved belong of the game chosen, you can do something like that:
Controller
$data = Game::select('id', 'name', 'published_date')->first();
return view('game')->with(compact('data'));
layout.blade.php
<html><head></head><body>
{{ $date }}
</body></html>
game.blade.php
#extend('layout')
#section('date', $data->date)
#section('content')
#endsection
The better solution would be this
Under your app folder make a class named yourClassNameFacade. Your class would look like this.
class yourClassNameFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'keyNameYouDecide';
}
}
Then go to the file app/Providers/AppServiceProvider.php and add to the register function
public function register()
{
$this->app->bind('keyNameYouDecide', function (){
//below your logic, in my case a call to the eloquent database model to retrieve all items.
//but you can return whatever you want and its available in your whole application.
return \App\MyEloquentClassName::all();
});
}
Then in your view or any other place you want it in your application you do this to reference it.
view is the following code:
{{ resolve('keyNameYouDecide') }}
if you want to check what is in it do this:
{{ ddd(resolve('keyNameYouDecide')) }}
anywhere else in your code you can just do:
resolve('keyNameYouDecide'))

Categories