I'm new in laravel and I need some help with laravel 5. why do I get this error? The error is in Composer class and View conposer.
Here is error
<?php
namespace App\Providers;
use DB;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
View::composer(
'index','APP\Http\ViewComposers\CompanyComposer'
);
}
}
<?php
namespace App\Http\ViewComposers;
use Illuminate\Contracts\View\View;
class CompanyComposer
{
/**
*
* #param View $view
* #return void
*/
public function compose(View $view)
{
//here is data
$view->with('success', 'success');
}
}
Related
I want to make a Collection macro function in the service provider.
The problem is after importing the Collection class i can not access the collection funtion $this->values()
But when I use the same code in the controller is working fine.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Collection;
class CollectionExtensions extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
Collection::macro('transpose', function () {
$items = array_map(function (...$items) {
return $items;
}, ...$this->values());
return new static($items);
});
}
}
This is how I create helper (App\Helpers\Settings.php)
namespace App\Helpers;
use Illuminate\Database\Eloquent\Model;
class Settings {
protected $settings = [];
public function __construct() {
$this->settings['AppName'] = 'Test';
}
/**
* Fetch all values
*
* #return mixed
*/
public function getAll () {
return $this->settings;
}
}
Creating facade (App\Helpers\Facades\SettingsFacade.php)
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Settings extends Facade {
protected static function getFacadeAccessor() {
return 'Settings';
}
}
Creating Service Provider (App\Providers\SettingsServiceProvider.php)
namespace App\Providers;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
class SettingsServiceProvider extends ServiceProvider {
/**
* Bootstrap the application events.
*
* #return void
*/
public function boot() {
}
/**
* Register the service provider.
*
* #return void
*/
public function register() {
App::bind( 'Settings', function () {
return new \App\Helpers\Settings;
});
} */
}
Registering provider (App\Providers\SettingsServiceProvider::class)
Creating alias: 'Settings' => App\Facades\Settings::class
Running composer dump-autoload
Trying to use facade Settings::getAll();
Getting error Class 'App\Http\Controllers\Settings' not found
Can’t figure out why I cannot create facade and getting that error
try this one.
App\Helpers\Settings.php
namespace App\Helpers;
use Illuminate\Database\Eloquent\Model;
class Settings {
protected $settings = [];
public function __construct() {
$this->settings['AppName'] = 'Test';
}
/**
* Fetch all values
*
* #return mixed
*/
public function getAll () {
return $this->settings;
}
}
App/Http/Controllers/XyzController.php
use Facades\App\Settings;
class XyzController extends Controller
{
public function showView()
{
return Settings::getAll();
}
}
web.php
Route::get('/','XyzController#showView');
use Facades\App\Helpers\Settings;
Route::get('/direct',function() {
return Settings::getAll();
});
use laravel Real time facades
I am trying to use Laravel view composer. I have registered my class in config/app.php but I keep getting the following error:
"Class App\Http\ViewComposers\PostComposer does not exist
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use View;
class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
View::composer('plain','App\Http\ViewComposers\PostComposer');
}
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
}
my post composer class
<?php
namespace App\Http\ViewComposer;
use Illuminate\View\View;
use App\Post;
class PostComposer
{
public function comspose(View $view)
{
$posts = Post::all();
$view->with('postha', $posts );
}
}
and here is the screenshot of my browser:
![folder structure in my app][]
Your namespace is wrong.
You're importing from (plural):
App\Http\ViewComposers\PostComposer
but the namespace of your ViewComposer isn't plural:
App\Http\ViewComposer
try it : namespace App\Http\ViewComposer To namespace App\Http\ViewComposers
I have made a header.blade.php which is extended to many View pages. in header menu items come from database.So I used ViewComposer.
Here is my ComposerServiceProvider.php
<?php
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(['cart.layouts.header'], 'App\Http\ViewComposers\CategoryComposer#compose');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
here is the CategoryComposer Class:
<?php
namespace App\Http\ViewComposers;
use Illuminate\View\View;
class CategoryComposer
{
public $categoryList = [];
/**
* Create a movie composer.
*
* #return void
*/
public function __construct()
{
$this->categoryList = \DB::table('categories')
->select('categories.*')
->get();
}
/**
* Bind data to the view.
*
* #param View $view
* #return void
*/
public function compose(View $view)
{
$view->with('categories', end($this->categoryList));
}
}
I have also registered the Service Provider in config/app
But the problem is I got error in other pages like Undefined variable: categories
Can you suggest me what could be the Error here?
I have the following files:
ModelObserverProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Models\User;
use App\Models\Profile;
use App\Observers\UserObserver;
use App\Observers\ProfileObserver;
class ModelObserverProvider extends ServiceProvider
{
public function boot()
{
User::observe(UserObserver::class);
Profile::observe(ProfileObserver::class);
}
public function register()
{}
}
UserObserver.php
<?php
namespace App\Observers;
use App\Models\User;
class UserObserver
{
public function creating(User $user)
{
dd('Creating...');
// Hash the users password when creating.
$user->password = bcrypt($user->password);
// Create a new profile for the user.
$user->profiles()->create(['name' => 'guest-' . time()]);
}
}
I have loaded the ModelObserverProvider in my config/app.php, however, notice the dd('Creating...'); ? When creating a new user via User::create($request->all()) this does not appear to be firing at all? Any ideas?
Try this
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Models\User;
use App\Models\Profile;
use App\Observers\UserObserver;
use App\Observers\ProfileObserver;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
User::observe(UserObserver::class);
Profile::observe(ProfileObserver::class);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{}
}