How to call createDeleteForm in CRUD controller for another entity - php

I have a basic CRUD controller using the "php bin/console generate:doctrine:crud AppBundle:Product" command. So I have the basic generated productController.php along with a Product.php entity that I made before, the generated CRUD product views, etc.
In ProductController.php there is a showAction method that has this line:
$deleteForm = $this->createDeleteForm($product);
I am trying to create a settings controller that embeds the product's showAction method but, as you can guess, trying to use
$this->createDeleteForm($product) yields
"Attempted to call an undefined method named "createDeleteForm" of class "AppBundle\Controller\SettingsController""
What's the best way to incorporate a settings controller and, if I'm on the right track, how can I generate the delete form for the product within the settings controller?
Thanks so much for any help you can offer!

Related

How to access to laravel routing resource methods?

I am using laravel to have access to my database. I have used the command php artisan make:controller CategoriesController --resource in the terminal to create a class where I can access different methods in one route.
The routing code for the class is: Route::apiResource("categories", CategoriesController::class);. With /categories I can get to the index() method (via get), where I can show my table values. But I do not know how I can use other methods. For example I have created test() with a simple return ["Result"=>"Working"].
I have tried /categories/test /categoriestest /categories%test but I can not show the result from test().
Simple routing works fine when I use specific routes for every method, but I want to make a more clear code so I would like to use the --resource to only have one route.

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

Access Laravel Resource Controller from HTTP Controller

I'm implementing a small project to maintain a list of books. I'm using PHP 7, Laravel 5.5, Eloquent and SQLite.
I created a Model class book and the respective resource controller BookController. For the sake of simplicity, a book only has to public properties: title and author.
Furthermore, I created an AdminController that creates an admin page. I want to use this page to add books to the database and remove other ones.
My BookController has a store() function:
public function store(Request $request)
{
// Validate the request...
$book = new Book;
$book->title = $request->title;
$book->author = $request->author;
$book->save();
}
My AdminController has a HTML form with input fields for new books (one for title, one for author) as well as a submit button. This button calls AdminController#post.
Now I wonder how to actually add the book from there.
Should I call the BookController from the AdminController and pass the request object to the BookController? Is this the way, controllers communicate in Laravel? Or should I avoid the store function and add the functionality to the AdminController directly?
I think you should separate two controller for two purposes. You can declare variable as Book model in other controllers and work with it.
You can using BookController for both, just need to define two routes for one controller and write some logic in the store method, my viewpoint... that is not good way. Because it need check some vars to detect which request from admin and from user. I don't want to make some thing complex between workflow of user & admin.

custom method in laravel 4.1 model not working

I am trying to use custom method in User model with class name User in laravel4.1. i changed the $table attribute to my table name and added a custom method names 'public function abc' in user model. Then in my user controller i tried like this :-
$u= new User;
$u->abc();
but its not working and giving following error :-
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::abc()
and i dnt know why this happening everything seems fine,help me out in this guys.
UPDATE :SOLVED ,Done Nothing
I DO NOT KNOW WHAT IS THE PROBLEM WITH LARAVEL
$u= new user;
$u->abc();
i just changed User to user and its started working and i dnt even know why ,anyone know reason??
Every method on a Model get passed on to a new QueryBuilder
User::where()
User::find()
User::{relationship}()
If you instantiate a model like this
$user = User::find()->method();
it will work. Don't try to make your Eloquent models too fat.
Just create a Repository to make your Controllers as thin as possible and your Eloquent just as intelligent as it can be by using the tools given by Eloquent (relationships, hidden attributes, accessors & mutators, $this->appends, ...)
Everything else belongs in your Repositories.
Try running
composer dumpautoload
If this doesn't work, be sure that there is only one class called User in your whole project. There may be a package, migration or something with the same name. Try putting your custom User class in a namespace.

Change default view of controller using view-list member: How to set view models?

I'm building a Joomla 2.5 component, and I want to change the view being rendered after a 'save' item action. This is easy enough (in the JControllerAdmin extended class constructor)
$this->view_list = "myview"
However, myview needs to load a couple of models to work. Elsewhere in the code this is done by a controller, for example controller.php (the component's master controller in the component root):
$view = &$this->getView('myview', 'html');
$view->setModel($this->getModel('myviewSpecialModel'),false);
$view->display();
My question: I'm not sure where in JControllerAdmin I can put this code so the necessary models are loaded before the view is displayed. Any advice?
If I understand your questions right, you have a controller that extends the JControllerAdmin.
So the code you mentioned earlier should be inside the display() method in the controller's class.
Does this answer your question?
Also also helpful might be the Joomla! Documentation: Using multiple models in an MVC component

Categories