Problems creating a ComposerServiceProvider in Laravel 5.4 - laravel

Im working on a Laravel 5 app and im trying to set up a ComposerServiceProvider to pass data to a couple of views (im trying now to add it to the layout/app.blade.php).
I did this following the documentation but the data im trying to add is still undefined..
In my config/app.php I added to the providers:
App\Providers\ComposerServiceProvider::class,
On ComposerServiceProvider.php
boot method:
View::composer(['layouts.app'], 'App\ViewComposers\LayoutAppComposer');
On the new created LayoutAppComposer.php
compose(View $view) method:
$metaTitle = 'MetaTitle';
$view->with('metaTitle', $metaTitle)
But When i access the url I still get:
Undefined variable: metaTitle (View: .../resources/views/layouts/app.blade.php)
Am I missing something here??
ServiceProvider
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
View::composer(['layouts.app'], 'App\ViewComposers\LayoutAppComposer');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
}
}
LayoutAppComposer
<?php
namespace App\ViewComposers;
use Illuminate\Support\Facades\Session;
use Illuminate\View\View;
class LayoutAppComposer {
protected $metaTitle;
public function __construct($metaTitle)
{
$this->metaTitle = $metaTitle;
}
/**
* Bind data to the view.
*
* #param View $view
* #return void
*/
public function compose(View $view) {
$this->metaTitle = 'MetaTitle';
$view->with('metaTitle', $this->metaTitle);
}
}

Try changing:
$metaTitle = 'MetaTitle';
$view->with('metaTitle', $metaTitle)
to
$this->metaTitle = 'metaTitle';
$view->with('metaTitle', $this->metaTitle)
setup $this->metaTitle as a protected class member and assign it in the composer constructor. it may be that $metaTitle is getting garbage collected before you use it since this being resolved at the service provider level.
Since you're registering the composer with your app layout, instead you may need to use the wildcard character in place of app.layout like such:
View::composer('*', function ($view) {
//
});
To resolve $metaTile for the View Composer, try binding in your AppServiceProvider:
$this->app->bind('metaTitle', 'the string i want displayed across all views');

Related

Laravel Service won't register

Followed every step from laravel 9 manual, and my service will not register consistently. Don't quite know why but it suddenly breaks and I receive a Target class [thumbnail] does not existerror out of nowhere.
Setup
Service:
namespace App\Services;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
class ThumbnailService
{
public function storeThumbnail($userId, $file, $scale) {
$thumbnail_image = Image::make($file);
$orig_width = $thumbnail_image->width();
$orig_height = $thumbnail_image->height();
$thumbnail_image->resize($orig_width * $scale, $orig_height * $scale);
$thumb_stream = $thumbnail_image->stream();
$thumb_path = env('MIX_AWS_IMAGES_DIRECTORY') . '/'. $userId . '/thumbnails';
$thumb_file = $thumb_path . '/' . 'thumb_1_4_' . $file->getClientOriginalName();
Storage::disk('s3')->put($thumb_file, $thumb_stream->__toString());
return $thumb_file;
}
}
Provider:
namespace App\Providers;
use App\Services\ThumbnailService;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
class ThumbnailServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->singleton('thumbnail', function() {
return new ThumbnailService();
});
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
}
public function provides()
{
return [ThumbnailService::class];
}
}
Facade:
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Thumbnail extends Facade
{
protected static function getFacadeAccessor() {
return 'thumbnail';
}
}
obviously, I did register in app.php in the providers array. But if I inspect the bindings, there is no service... Any help? This is the second time this happened and earlier, I got away with dumping autoload and config:clear and it is currently under my sail dev environment. I don't want this to happen in prod. So help would be appreciated.

How to overcome repeative blade component queries in laravel

As in laravel we make blade component to reuse that on multiple pages. so for example we have a cardcomponent that has a query in render function
User::count();
and we use that card component on almost every page so whenever a page is loaded the above query is executed again and again. is there a way to overcome it?
You can put the query in the AppServiceProvider as shown in the documentation here https://laravel.com/docs/9.x/views#sharing-data-with-all-views. The result will be available in all views and the query should only be executed once.
<?php
namespace App\Providers;
use Illuminate\Support\Facades\View;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
View::share('key', 'value');
}
}
Alternatively you can use a blade component: https://laravel.com/docs/9.x/blade#components

How to fire an event after data inserted using boot? (laravel)

I am finding a way to somewhat fire an event after the Eloquent has finished creating.
Here's my code in Branch model:
class Branch extends Model
{
//some code here
public static function boot() {
parent::boot();
self::created(function (HistoryLog $model) {
$model->tag = 'Created';
$model->description = 'This branch was created by '. ucwords(auth()->user()->name());
$model->save();
});
}
}
What I'm trying to do is, I want to create a history_log after branch was created.
But this code returns an error:
Symfony\Component\Debug\Exception\FatalThrowableError : Argument 1 passed to
App\Vehicle::App{closure}() must be an instance of App\HistoryLog, instance of
App\Vehicle given, called in D:\document\My Documents\optodph\vendor\laravel\fr
amework\src\Illuminate\Events\Dispatcher.php on line 347
Can someone point out to me what's wrong with that code? And what's the right way to achieve this?
Laravel way to do this.
Create an Observer:
php artisan make:observer BranchObserver --model=Branch
Add your logic to the Observer:
<?php
namespace App\Observers;
use App\Branch;
class BranchObserver
{
/**
* Handle the Branch "created" event.
*
* #param \App\Branch $branch
* #return void
*/
public function created(Branch $branch)
{
// Add your logic here
}
}
Register it in AppServiceProvider:
<?php
namespace App\Providers;
use App\Branch;
use App\Observers\BranchObserver;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Branch::observe(BranchObserver::class);
}
}
$model is a new record created. Is an instance of App\Vehicle not a App\HistoryLog.
Working code might look like this:
class Vehicle extends Model
{
//some code here
public static function boot() {
parent::boot();
self::created(function ($model) {
App\HistoryLog::create([...]);
});
}
}
You can also achieve this with Eloquent Observers https://laravel.com/docs/5.8/eloquent#observers

Display counter variable in my layout.app.blade in Laravel

i want to display the counter in my app.blade which being called in all pages like the screenshot below
I only have this function in my controller
class ReportsController extends Controller
{
public function invoiceTransaction()
{
$salespayments = Salespayments::where('type','=','check')->get();
$countUnread = Salespayments::select(DB::raw("SUM(unread) as unread"))->get();
return view('reports.invoiceTransactions')
->with('salespayments', $salespayments)
->with('countUnread', $countUnread);
}
}
And I am calling the counter in my blade by this {{$countUnread[0]->unread}}
How can I make that function be readable in my app.blade.php? thanks a lot!
In your AppServiceProvider you can share the sum result across all views by using view()->share();
Like this:
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot() {
$countUnread = Salespayments::sum('unread');
view()->share('countUnread', $countUnread);
}
Make a service provider first,
php artisan make:provider CounterServiceProvider
Then in your CounterServiceProvider file.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Views\Composers;
class CounterServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
// here define your master layout
$this->app['view']->composer(['master'], Composers\Counter::class);
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
Now in your App\Views\Composers folder add Counter class.
<?php
namespace App\Views\Composers;
use Illuminate\View\View;
class Counter {
public function compose(View $view)
{
$view->with('countUnread', session('countUnread'));
}
}
Make sure you add your CounterServiceProvider in config/app.php file's providers array.

Laravel pass object to view

When I'm trying to pass an object through a view with the AppServiceProvider it gives an error an says
Trying to get property of non-object
This is currently my App\Providers:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\User;
use Auth;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$users = Auth::user();
view()->share('user','users');
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
And when I say in the view:
{!! user->name !!}
It throws an error.
you are missing the '$' on users when passing the variable to view. So it can't be rendered on the view.
it must be:
$users = Auth::user();
view()->share('user', $users);

Resources