Class 'App\Http\Controllers\rimdetails' not found? - php

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.

Related

Action not defined unless I put the complete namespace to controller method in Laravel

Hi i hope you can help me with this. I couldn't find an explanation in the documentation but I had this problem:
if I put the controller like this I got an error telling me the method was not defined
<a href={{ action('MovieController#create') }}>Create Movie</a></h3>
so I get to solve it like this, puting the complete namespace
<a href={{ action('App\Http\Controllers\MovieController#create') }}>Create Movie</a></h3>
My question is; is there any way I don't have to put the complete namespace to get it to work?
If you want a namespace prefixed to the Controller when generating URLs to "actions" you will need to define this in your RouteServiceProvider:
protected $namespace = 'App\Http\Controllers';
Also if you do not want the prefix assigned to your Controllers for your routes by default you will have to make sure the route groups do not have this $namespace variable used to assign a namespace with the namespace method in that Service Provider.
Reference for namespace prefixing in Laravel 8: Target class controller does not exist - Laravel 8

hidden magic in Laravel blade components

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

(1/1) FatalErrorException Class 'App\User' not found in HomeController.php (line 28)

I'm actually making a laravel CRUD app.
So there is this model called User which has been created, but when I try to use it in a controller (In this case, HomeController.php), it says:
Here is line 28 from the controller:
I'm sorry if this question already exists but I've searched everywhere for a solution but could not find it.
Thank you.
Ideally, you should post the line you are importing the model, and the first few lines of your model.
But I will try to explore the possibilities of error.
Is your model inside a specific folder (just like app/MyModels/User.php) or just inside the default place when you use php artisan make:model? Remember to put the exact path in namespace line inside of model.
In the first line of your model you should have the path of your model with namespace. If the model is in its default directory, you should have the line below, but if it is inside of a folder, you have to put the folder path after de App:
namespace App;
Verify if you model is extends of Model:
class User extends Model {
In your controller the use line have the same path of namespace line in model, if you model is in default path, should be like this:
use App\User;
Remember that: the App is the namespace of your project, if you used the php artisan app:name command your namespace is no longer an App, and you should change it if you have the old name somewhere. If you using linux, remeber of the case sensitive.
If it does not work, put into the post the codes of the files I've listed so we can help.
in the begining
<?php
use App\User;

How to use Laravel revisionable with morphMaps?

I have a morphMap setup in my AppServiceProvider boot method that is firing when the Laravel revisionable package calls for the record so nothing is being returned. I am looking to figure out how to make revisionable work with morphMaps. Please note that revisionable is storing the full namespace when saving the record but when calling the record revisionable is using the value I put in for the morphMap.
For example revisionable saves App\Models\Organization\Organization but my morphMap translates that to just organization when calling for the revisionHistory().
I know this is an old question but I see Venturecraft/Revisionable developers are not giving answers to this problem.
The error ocurrs when the package try to instatiate a new model with the value of the property $this->revisionable_type in Venturecraft\Revisionable\Revision, because the class does't exist when using the key of $morphClass array instead of the real namespace of the model.
I figured out that the problem is solved when I override the value of this property with an Laravel Accessor like this:
use Illuminate\Database\Eloquent\Relations\Relation;
public function getRevisionableTypeAttribute($value)
{
$morphedModel = Relation::getMorphedModel($value);
return !empty($morphedModel) ? $morphedModel : abort(400, "The namespace {$value} hasn't been added to the Relation::morphMap() method.");
}
Now, I don't know how to add this Accessor to the Revision model without editing the one that comes in the vendor folder.
If you can give a try and share what you get will be helpful.

Laravel - Call to undefined method TrainingFacade::save()

I'm building a Laravel 4.2 app and I'm using Creolab's Modules package. It's an app for managing company trainings, so my module is called Trainings. All works well except saving newly created trainings. The error I receive when I submit the Create New form is:
Call to undefined method Roche\Trainings\Facades\TrainingFacade::save()
These are my TrainingsController, Training Model, TrainingFacade, Trainings Service Provider and routes. Nothing I try seems to fix it.
You don't need to change your facade alias here, but you have other error here.
In your AdminTrainingsController you want to use Training model so you import Training before your class definition this way:
use Training;
but model Training is not in root namespace, in root namespace there is TrainingFacade because you probably define it in your app.php file this way:
'Training' => 'Roche\Trainings\Facades\TrainingFacade',
That's ok, you don't need to change this facade alias.
So now, Laravel inject into your constructor not Training model but TrainingFacade (because Training in root namespace is alias for TrainingFacade).
What you need here is importing correct namespace for your Training model.
When we look at your Training model class we see the code is:
<?php namespace Roche\Trainings\Models;
class Training extends \Eloquent {
protected $guarded = array('id');
}
so it's in Roche\Trainings\Models namespace.
So to make it work in AdminTrainingsController you need to change:
use Training;
into:
use Roche\Trainings\Models\Training;
Now Laravel will inject into constructor Training model (and not TrainingFacade as it was doing).
EDIT
You could also make some tests, for example changing in your app.php your TrainingFacade alias, for example to:
'TrainingFacade' => 'Roche\Trainings\Facades\TrainingFacade',
but if you had use Training as you had, you would get the following error:
Class Training does not exist
because there would be no Training class in global namespace now (you changed alias for facade).
And if you removed use Training; completely, you would get:
Class Roche\Trainings\Controllers\Admin\Training does not exist
because by default class is loaded from current namespace, and your AdminTrainingsController is in Roche\Trainings\Controllers\Admin namespace
I hope now everything is clear, if it's not you could also look at How to use objects from other namespaces and how to import namespaces in PHP where I explained more about namespaces

Categories