I have created the web route correctly.
use App\Http\Controllers\FrontendCourseController;
Route::get('update-item', [FrontendCourseController::class, 'update'])->name('update-item');
and add a method update. Also, I have created controller correctly.
<?php
namespace App\Http\Controllers;
use App\Helpers\CurrencyHelper;
use Auth;
use DB;
use App\Models\Course\Course;
use App\Models\Item;
class FrontendCourseController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function update()
{
dd('Page Update is working');
return view('update-item');
}
}
But I keep getting this error message
Method App\Http\Controllers\FrontendCourseController::update does not
exist.
Despite clearing caches using
php artisan route:cache
Where am I doing wrong?
Related
this is my parent class which is a user class that has the main crud operations
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Repositories\UserRepository; //<----------- Here
class UserController extends Controller
{
protected $model;
public function index()
{
$users = $this->model::all();
return view('users.index', compact('users'));
}
}
this is my child class which is one of my user roles , it have the same crud operation but it need some more functinality
<?php
namespace App\Http\Controllers;
use App\Models\Teacher;
use App\Http\Controllers\UserController;
class TeacherController extends UserController
{
public function __construct()
{
$this->model = Teacher::class;
}
}
when I try to access the route i get this error : Class name must be a valid object or a string
at :
$users = $this->model::all();
Well, it seems my Laravel project used old cached routes. Just run
php artisan route:clear
from time to time before debugging anything.
I'm trying to use a trait to handle image upload on my Laravel application, but none of the functions in my Trait can be called from the controller.
It throws a BadMethodCallException and says that the function couldn't be found.
I've tried using really simple functions to test if it is a problem with the trait or whether the function itself has an issue, but even a simple return function that only contains
return "sampletext";
has the same issue.
The path of the trait is under App/Traits/UploadTrait
and I've already checked the spelling on the use statement in my controller, which says use App\Traits\UploadTrait;
namespace App\Traits;
trait UploadTrait
{
public function test(){
return "testtext";
}
}
And the controller has
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use App\User;
use App\Profile;
use App\Traits\UploadTrait;
use Image;
class UserProfileController extends Controller
{
...
protection function updateProfile($args, Request $request){
...
return $this->test();
...
Of course I expect the function in my trait to be called, but this does not happen.
You need to use the trait inside your controller and move the $this->test() inside a class function:
<?php
use App\Traits\UploadTrait;
class UserProfileController extends Controller
{
use UploadTrait; // <-- Added this here
public function index()
{
return $this->test(); // <-- Moved this into a function
}
}
You have to put the use keyword to use that trait and its methods in the class
trait UploadTrait
{
public function test(){
return "testtext";
}
}
class Controller{
}
class UserProfileController extends Controller
{
use UploadTrait;
}
$ob = new UserProfileController();
echo $ob->test();
You can make a function to and call the trait function.
More Details
Use trait inside the class like:
use my/path/abcTrait;
Class My class{
use abcTrait;
}
Now, you can call trait functions with $this->functionName () in functions.
I keep getting an error:
Call to undefined method App\Facebook::fbLogin()** when
**handleProviderCallback()
Controller
namespace App\Http\Controllers;
use App\Facebook;
class FacebookController extends Controller
{
public function handleProviderCallback()
{
$user = Socialite::driver('facebook')->user();
$route = Facebook::fbLogin($user);
return redirect()->route($route);
}
}
Model
namespace App;
class Facebook extends Model
{
public static function fbLogin($user){
.......
}
}
Already spent hours looking for a solution. Please help.
Delete the model and recreate it again. maybe some kind of indexing issue. other than this run following command in the composer.
composer dump-autoload
My problem:
I have a controller that calls a method from another controller for some information. However, Laravel isn't able to locate the class in that controller.
FatalErrorException in TradesController.php line 35: Class 'Profile'
not found
What I have tried:
I am using Laravel 5.2 and have created the controller with php artisan make:controller Profile to ensure that any possible internal pointers (in lack of better vocabulary) are created - even though my understanding is that Laravel 5.2 does this automatically as long as controllers are in the \app directory.
They both reside within \app\Http\Controllers
My code
TradesController calls class Profile
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Profile;
use Session;
use Auth;
class TradesController extends Controller
{
public function __construct(Request $request){
$this->request = $request;
}
public function showInventory (Request $request){
.....
// Following three calls all fail
$profile = new Profile;
$profile = Profile()->linkToProfile();
$profile = Profile::linkToProfile();
.....
return($output);
}
}
MY class Profile-controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Session;
use Auth;
class ProfileController extends Controller
{
public function __construct(Request $request){
$this->request = $request;
}
public function linkToProfile (Request $request) {
return("test");
}
}
php artisan make:controller xxx creates a class xxxController therefore your class is ProfileController not Profile.
Laravel 5.1
This seems strange to me:
Route::group([
'middleware'=>['auth','acl:view activity dashboard'],
'prefix' => 'api/v1'
], function(){
Route::controller('investment-transactions', 'Api\V1\Investments\InvestmentTransactionsController');
Route::controller('investment-transactions/{offeringID}', 'Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering');
});
Seems pretty normal to me, the controller:
namespace App\Http\Controllers\Api\V1\Investments;
use App\Brewster\Models\Company;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class InvestmentTransactionsController extends Controller {
public function __construct() {
}
public function getIndex() {
echo 'Here';
}
public function getTransactionsForOffering($offeringID) {
echo $offeringID;
}
}
Ok so the action and the controller do exit, but when I run: php artisan routes:list I get:
[ReflectionException]
Class App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering does not exist
Well obviously App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController#getTransactionsForOffering is not a class, how ever: App\Http\Controllers\Api\V1\Investments\InvestmentTransactionsController is and getTransactionsForOffering is an action.
Whats going on?
I believe your problem is in the routes.php we can use controllers as follows
Route::get('investment-transactions', 'InvestmentTransactionsController#index');
Route::get('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
By default, our controllers are stored in App/http/controllers folder and laravel know it.
I believe you only need to reference the Class like so:
Route::controller('investment-transactions','InvestmentTransactionsController#Index'); //make sure you create a function for the index
Route::controller('investment-transactions/{offeringID}', 'InvestmentTransactionsController#getTransactionsForOffering');
Assuming you need to show a view for the route investment-transactions create the following function in your controller:
public function index()
{
return view('name-of-your-view-file');
}