Method [validate] does not exist - laravel

Method validate does not exit. I tried to validate my form values as shown in the figure. But it give me this error. Please tell me . I am new to laravel.
[

You aren't extending the correct Controller.
class UserController extends Controller {
...
}
Controller then extends BaseController.
If you open up Controller which resides in the same namespace as your other controllers, you will see it uses the trait ValidatesRequests which is what provides the validate method.
You can also remove the line use Illuminate\Routing\Controller as BaseController;. There should be no reason to import that.

Related

use functtion in Controller extend

Controller 1:
namespace App\Http\Controllers;
class HomeController extends Controller
Controller 2:
namespace App\Http\Controllers\Games;
class ExchangeController extends Controller
i have problem when using function in controller, in 1st controller on same level as Controller is extended then $this->rollback method works, but with 2nd controller i cant access it but still can access If I rename the function, why can't I call the function named rollback in case 2 but can use in case 1

How to use different method of a controller in laravel application

In my laravel application i should use some create and update method of some controller in another controller
According to my search is not a good thing to call a method from controller in another
I cant see the why don't call a controller method in another controller
I'm doing this way :
class Controller extends BaseController
{
protected $variable;
public function __construct()
{
$this->variable = "Hello";
}
}
and
class ClientController extends Controller
{
public function __construct()
{
parent::__construct();
}
}
The __constructor is a magic method of class. It calls when you trying to create instance of class. So there is no way to use constructor without creating an instance or extendeding from another class. If you have a common code in different classes there a best way to use traits. thats give you an opportunity to include your trait and use methods ,making your code beutiful , flexible , readable following principes DRY,KISS.
you can create a base class with constructor and extend other controller of it
or you can put your code in to Http\Controllers\controller.php ('main controllers constructor')
also you can use trait

Laravel model existed but return Class 'App\Entities\OrignalTripModel' not found

Model included in my Controller as
use App\Entities\OrignalTripModel;
But it returns
Class 'App\Entities\OrignalTripModel' not found
i am using it into controller as
OrignalTripModel::create($inputs);
Kindly include it in your controller like this:
use App\Entities\OrignalTripModel;
And please check namespace for the model OriginalTripModel to see if it has namespace App\Entities; and that is extends model like this class OriginalTripModel extends Model

How to override a Laravel package function

I'm using https://github.com/artesaos/seotools package for seo.
I am trying to override getCanonical function located at https://github.com/artesaos/seotools/blob/master/src/SEOTools/SEOMeta.php and make it's output as lowercase. Could you please guide me how can I do this?
You can try following :
Step 1:
Create a child class extending SEOMeta class and override the getCanonical function.
Class XyzSEOMeta extends SEOMeta {
public function getCanonical () {
// Write your logic here
}
}
Step 2:
Create the Service Provider for overridden class. First parameter of bind function must be same as facade accessor of SEOMeta Facade (check here). Register this facade in config/app.php after the service provider of seotools package. :
Class XyzSEOMetaServiceProvider extends ServiceProvider {
public function register(){
$this->app->bind('seotools.metatags', function(){
return new XyzSEOMeta($this->app['config']);
})
}
}
You are all set. Hope this will help.
EDIT:
Above mention method will just override the single class. If you want to change the logic of more than one class. Best way is to fork the project. Change the code and push it to your fork. Use forked project as your composer dependency. Follow the link to know how to use private repository as composer dependency : https://getcomposer.org/doc/05-repositories.md#using-private-repositories
It's very simple just like we overriding any parent class function in derived class.
Create your own class and extend your package class SEOMeta and re-declare function that you want to override and put your logic inside. Don't forget to use namespace of your package class SEOMeta at upper your custom class.
Now use your custom class instead of package class inside your controller.
e.g
use Path\to\SEOMeta;
class urclassname extends SEOMeta{
public function overridemethodname(){
// put ur logic here.
}
}

Why "Request::" (Request Static class) gives me different methods from $resquest-> (Request object) in Laravel 5.4?

I was trying to grab the current path that was asked by the user.
Following the doc I have created these 2 methods:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use GuzzleHttp\Client;
class TestController extends Controller
{
public function reqTest1(Request $req) {
dd($req->path());
}
public function reqTest2() {
dd(Request::path());
}
}
The reqTest1 works good, but I had to inject the Request object inside the function. There I can grab path() method, and I have the response as expected.
But if I dont wan to inject dependencies I should be able to get the same method only calling the static class Request. (I dont know).
I just realise that we dont have the same methods available in these 2 scenarios.
The methods list for the injected object is much bigger than the static class that generate the same object, for the Request class..
So, why Request class has different methods from a Request object created by a dependency injection?
A Laravel facade is a class which provides a static-like interface to services inside the container, i.e. every method you call statically on the Request facade is rerouted to the actual request object. This is the reason the Request facade is a object with few methods itself.
If you want to call the facade in your code change this line:
dd(Request::path());
to this:
dd(\Request::path());
or you are calling the static path() function on Illuminate\Http\Request not the Request facade, because of this line:
use Illuminate\Http\Request;
In my view, it's better to use the helper function request() instead of the \Request facade.

Resources