I had anonymous component resources\views\components\homepage\feedback.blade.php to render feedback on homepage. From the beginning it was just html. Then I decided to connect Class file. I already had another View Class component and I just copied it manually instead of using artisan command.
App\View\Components\Feedback.php
namespace App\View\Components;
use Illuminate\View\Component;
use App\Models\Feedback;
class Feedback extends Component
{
public $feedbacks;
public function __construct()
{
$this->feedbacks = Feedback::wherePublished(true)->take(5);
}
public function render()
{
return view('components.homepage.feedback');
}
}
And then {{ dd($feedbacks) }} in view file gives me error that this variable is not defined.
Undefined variable: feedbacks (View: C:\laragon\www\lara7\resources\views\components\homepage\feedback.blade.php)
If I try to create Test component with artisan command and put this code inside it works, but then I cannot rename it back to Feedback class. It gives me error
Symfony\Component\ErrorHandler\Error\FatalError
Cannot declare class App\View\Components\Feedback because the name is already in use
But old class already deleted, so I cannot understand what is wrong.
It seems like there is some hidden link between View Class and Blade components, which needs to be erased. But where is this link located?
When switching component type from anonymous to class and back, you have to clear compiled view files:
php artisan view:clear
That's because Laravel incorporate specific component type invocation into the compiled view code.
I found problem.
I got $feedbacks is undefined, because my anonymous component without variables initially was located in resources\views\components\homepage\feedback.blade.php and when I decide to create View Class for this component there was no link established.
Laravel creates automatic link between feedback.blade.php and app\View\FeedbackComponent.php only when blade file located directly in resources\views\components folder. And my component was in subfolder.
So laravel tried to render resources\views\components\homepage\feedback.blade.php with $feedback variable inside and it cannot find where $feedback is defined.
So I just manually register FeedbacksComponent class like that in appservice provider boot method
Blade::component('homepage-feedbacks', FeedbacksComponent::class);
and then use <x-homepage-feedbacks/> to render it
I would say documentation is not very clear. It says that outside of components folder automatic discovery is not working.
But it doesn't say that inside components subfolder automatic discovery is not working.
In Laravel 8 you can use and no need to declare the component
<x-homepage.feedback />
I think you're right I have been having the same issue and and I've been really struggling with it. Finally I found a workaround which is if you change the file name it works so I think it's a problem with the laravel framework and I think they need to address this issue
Related
I have created a laravel project with Rims and tyres. when clicking a Rim from the index page, i would like to link directly to a rim detail page, like a productdetails page based on the clicked product.
So far, it seems that my route and blade files are fine! but i have no idea how to make the controller function. i have tried with:
public function show($id)
{
$rimdetails = rimdetails::with('rimdetails')->findOrFail($id);
return View::make('rimdetails', compact($rimdetails));
}
and getting the error :
Class 'App\Http\Controllers\rimdetails' not found
try this one
for laravel 6
Route::get('rims','App\Http\Controllers\RimDetails#show');
for laravel 8
use App\Http\Controllers\RimDetails';
Route::get('rims',[RimDetails#show,'show']);
and also make sure you have exact name of file as you have in controller
You need to alias this rimdetails class if you want to reference it correctly. Currently you are in a declared namespace and referencing a class, rimdetails. PHP thinks you are referring to a class named rimdetails in the current namespace App\Http\Controllers so its looking for App\Http\Controllers\rimdetails as per the error.
Add this below the namespace declaration and before the class definition:
use App\rimdetails;
Assuming your model is in the app folder.
I am working on PHP in Code-igniter. I want to call function of one controller on another controller. I have tried following code for that-
$this->load->library('../controllers/Benchmarking');
$this->Benchmarking->init_benchmark();
But after doing this I got an error as unable to locate the specified class:session.php
(Here, benchmarking is my controller and init_benchmark is that function which I want to call)
Can anyone help me, please?
You cannot call a function of a controller from another controller. CI won't allow it.
If you need the user to "move" from a controller to another, your best bet is to set session flashdata with all the data you need to pass from one controller to the other and then use a simple redirect.
$this->session->set_flashdata('handoff_data');
redirect('second_controller/function');
There are other ways, some more powerful, some more complicated, but this is by far the simplest one
please check your class name and extends properly. and create a common controller in your core folder like
class MY_Controller extends CI_Controller {
}
and call this controller in another controller's extends.
I've created a helper file using the following method:
Created file in app/Http/helpers.php
Added file path in composer.json
Run this command: composer dumpautoload
Everything worked well but I want to use my models here so that I don't have to write code for getting data from database every time, but it's giving an error that model class not found.
function productImagePath($image_name) {
$generalSettings = \GeneralSettingsModel::GetGeneralSettings();
return $generalSettings;
}
My requirement is that I want to call some model's functions to get frontend (header/footer) design settings from database. So I've to write that code again and again in each controller. I'm very new in Laravel so if it's not a good approach to use helpers for such things, please guide me how it's possible then.
As your GeneralSettingModel class is in the App namespace you will need to include that:
function productImagePath($image_name)
{
$generalSettings = App\GeneralSettingsModel::GetGeneralSettings();
return $generalSettings;
}
Hope this helps!
I'm using Laravel to build a CMS and I have a View Site button that's available in my main layout file (if that's how you call it) app.blade.php. This file is where I yield my content and load all my css and js files.
I want the link of the View Site button to be dynamic, based on a base url set in my settings table. I can retrieve the variable like this:
$settings = DB::table('settings')->get();
return $settings->base_url;
But my question is: how do i retrieve this in my view the right way?
I'm having trouble doing this because usually I link a method to a specific page with the routes.php file, but since the app.blade.php is available everywhere I'm unable to do this (I guess).
You need a view composer. This is a function that can add data to a view every time it is being rendered. It makes the most sense to put your view composer inside of a service provider.
Make a new service provider inside app/Providers, then add your view composer inside of the boot() method.
ViewComposerServiceProvider
namespace App\Providers;
use DB;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider
{
public function boot()
{
view()->composer('app', function($view) {
$settings = DB::table('settings')->get();
$view->with('base_url', $settings->base_url);
});
}
}
This would inject a variable called $base_url into app.blade.php every time it was rendered. Of course, instead of adding the view composer as a closure, you could move it out into its own class that just gets included inside the service provider, but this should give you the right idea.
Then just make sure to register your service provider inside config/app.php:
providers => [
//All other service providers
App\Providers\ViewComposerServiceProvider::class,
]
This is the preferred "Laravel way" to inject data into views every time they are rendered. This is the way that is the most flexible and scalable because it allows you to modify your views in really any way before rendering them. Laracasts has a really nice free video on the topic.
The best way is to create a helper function and use it in the view template.
In your app folder, create a file: helper.php
function getBaseURL()
{
return \App\Setting::get()->base_url ;
}
Now in your blade template app.blade.php, you can get it like:
Visit Site
You need to add this helper.php file to the composer autoload.
For that, in your compose.json file,
"files":[
"app/helper.php"
],
Add this in the autoload part.
Now do a composer dump-autoload
I am using a Facedtector class for my work. I added the class in my Codeigniter libraries folder and just called from my controller class. In my controller I also loaded the class but run time it is showing following error:
An Error Was Encountered
Non-existent class: FaceDetector
Please anyone can help me about this.
Thanks
Add your class file inside you library folder of your main application folder.
And then you can use the auto load function of codeigniter
application/config/autoload.php
at the above location and then you can access the class directly or other way of using this use
$this->load->library('Your_Class_Name', 'Path\to\your\class');
This way you can access your class in codeigniter
You can follow below link for autoload of a class library.
https://ellislab.com/codeigniter/user-guide/general/autoloader.html