Target class [App\Sys\Http\Controllers\Api\LocationController] does not exist - laravel

I have setup Laravel 6 project but for some reason when php artisan route:list returns “Target class [App\Sys\Http\Controllers\Api\LocationController] does not exist." I'm new in Laravel and I can't understand why the controller doesn't work. Can anyone please help me?
Here are my code:
LocationController.php
<?php
namespace App\Http\Controllers\Api;
//use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Location;
class LocationController extends Controller
{
public function index(Request $request)
{
$per_page = $request->per_page ? $request->per_page : 5;
$sort_by = $request->sort_by;
$order_by = $request->order_by;
return response()->json(['locations' => Location::orderBy($sort_by, $order_by)->paginate($per_page)],200);
}
public function store(Request $request)
{
$location= Location::create([
'code' =>$request->code,
'name' =>$request->name,
'description' =>$request->description
]);
return response()->json(['location'=>$location],200);
}
public function show($id)
{
$locations = Location::where('code','LIKE', "%$id%")->orWhere('name','LIKE', "%$id%")->orWhere('description', 'LIKE', "%$id%")->paginate();
return response()->json(['locations' => $locations],200);
}
public function update(Request $request, $id)
{
$location = Location::find($id);
$location->code = $request->code;
$location->name = $request->name;
$location->description = $request->description;
$location->save();
return response()->json(['location'=>$location], 200);
}
public function destroy($id)
{
$location = Location::where('id', $id)->delete();
return response()->json(['location'=>$location],200);
}
public function deleteAll(Request $request){
Location::whereIn('id', $request->locations)->delete();
return response()->json(['message', 'Records Deleted Successfully'], 200);
}
}
My route file:
api.php
<?php
use Illuminate\Http\Request;
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::namespace('App\Sys\Http\Controllers')->group(function () {
Route::get('/menuslevel0',['uses' => 'MenuController#menus_level_0']);
Route::resource('locations','Api\LocationController');
});

You controller is in the App\Http\Controllers\Api, not in the App\Sys\Http\Controllers namespace. Remove the locations resource route in the namespace App\Sys\Http\Controllers group and create a new one.
Do this
...
Route::namespace('App\Sys\Http\Controllers')->group(function () {
Route::get('/menuslevel0',['uses' => 'MenuController#menus_level_0']);
});
Route::namespace('App\Http\Controllers')->group(function () {
Route::resource('locations','Api\LocationController');
});
...

Your controller is in App\Http\Controllers\Api and your route is pointing to App\Sys\Http\Controllers\Api.
You must change:
Route::namespace('App\Sys\Http\Controllers')->group(function () {
// Your routes
});
To:
Route::namespace('App\Http\Controllers')->group(function () {
// Your routes
});

Related

Problem with "Route" resource in laravel 8

My app was working fine until I wanted to replace my "get" routes with a single "resource" to be able to include a delete function. I remind you that I am a beginner in laravel so it is surely due to the fact that I can not be able to make a "resource" route.
This is my code :
Route::get('/', function(){
return view('front.pages.homepage');
})->name('homepage');
Route::get('/garages', [GarageController::class, 'index'])->name('garage.index');
Route::get('/garages/{garage}', [GarageController::class, 'show'])->name('garage.show');
Route::get('/garages_create', [GarageController::class, 'create'])->name('garage.create');
Route::post('/garages_create', [GarageController::class, 'garage_create'])->name('garage_create');
Route::resource('cars', [CarController::class]);
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Car;
use App\Models\Garage;
class CarController extends Controller
{
public function index()
{
$list = Car::paginate(10);
return view('cars', [
'list' => $list
]);
}
public function store()
{
$garages = Garage::orderBy('name')->get();
return view('carCreate/carCreate', [
'garages' => $garages,
]);
}
public function create(Request $request)
{
{
$request->validate([
'name' => 'required',
'release_year' => 'required',
'garage_id' => 'required', 'exists:garage_id'
]);
$car = new Car();
$car->name = $request->name;
$car->release_year = $request->release_year;
$car->garage_id = $request->garage_id;
$car->save();
return redirect('/cars');
}
}
public function show(Car $car)
{
return view('carShow/carShow', compact('car'));
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
//
}
}
And my error is : ErrorException
Array to string conversion

Method SendMoneyController::index does not exist

I am using Laravel 8, and my application is working fine locally. Now I upload the app to a dedicated server and I'm getting the error below:
BadMethodCallException Method
App\Http\Controllers\User\SendMoneyController::index does not exist.
However, this method exists and it's working fine on the local server.
Controller
class SendMoneyController extends Controller
{
use AmlRulesVerification;
protected $sendMoneyInterface, $transactionInterface, $recipientInterface, $cardInterface, $homeInterface;
public function __construct(
SendMoneyInterface $sendMoneyInterface,
TransactionInterface $transactionInterface,
RecipientInterface $recipientInterface,
CardInterface $cardInterface,
HomeInterface $homeInterface
) {
$this->sendMoneyInterface = $sendMoneyInterface;
$this->transactionInterface = $transactionInterface;
$this->recipientInterface = $recipientInterface;
$this->cardInterface = $cardInterface;
$this->homeInterface = $homeInterface;
}
public function index(Request $request, $id = null)
{
$transaction = $this->sendMoneyInterface->getTransaction($request, $id);
if (isset($transaction['transaction']->card) && $transaction['transaction']->card) {
return redirect()->route('send.money.confirmation', ['id' => $id]);
}
return view('customers.send_money.index',
[
'data' => $transaction,
'countries' => $this->homeInterface->landingViewData($request)
]);
}
}
web.php
Route::group(['middleware'=>'languageSwitcher'],function () {
Auth::routes(['verify' => true]);
Route::get('/', [HomeController::class, 'landingView'])->name('landing.view');
Route::get('users/registration', [UserController::class, 'showRegisterForm'])->name('user.registration');
Route::get('localization/change/language', [LanguageSwitcher::class, 'changeLanguage'])->name('LangChange');
Route::post('/set/email/session', [UserController::class, 'setEmailInSession']);
Route::group(['middleware'=>'auth'],function () {
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('/send/money', [UserSendMoneyController::class, 'index'])->name('send.money.index');
)};
)};
Please Verify the namespace in the UserSendMoneyController
Route::get('/send/money', [User\UserSendMoneyController::class, 'index'])->name('send.money.index');
try these changes if commented this line then uncomment too In "app\provider\RouteServiceProvider"
protected $namespace = 'App\\Http\\Controllers';
Why are you using a variable whose value you have already set to null?
public function index(Request $request)
{
$transaction = $this->sendMoneyInterface->getTransaction($request);
if (isset($transaction['transaction']->card) && $transaction['transaction']->card) {
return redirect()->route('send.money.confirmation');
}
return view('customers.send_money.index',
[
'data' => $transaction,
'countries' => $this->homeInterface->landingViewData($request)
]);
}

Route [admin.departments.index] not defined

i am trying to return back to departments after add a new department but this what happens :
Route [admin.departments.index] not defined
this is my store function in the DepartmentController
class DepartmentController extends BaseController
{
public function store(Request $request)
{
$this->validate($request, [
'department_name' => 'required|max:191',
]);
$params = $request->except('_token');
$department = $this->departmentRepository->createDepartment($params);
if (!$department) {
return $this->responseRedirectBack('Error occurred while creating department.', 'error', true, true);
}
return $this->responseRedirect('admin.deparments.index', 'Department added successfully' ,'success',false, false);
}
}
this is the responseRedirect function in the base controller
class BaseController extends Controller
{
protected function responseRedirect($route, $message, $type = 'info',
$error = false, $withOldInputWhenError = false)
{
$this->setFlashMessage($message, $type);
$this->showFlashMessages();
if ($error && $withOldInputWhenError) {
return redirect()->back()->withInput();
}
return redirect()->route($route);
}
}
these are the routes
Route::group(['prefix' => 'departments'], function() {
Route::get('/', 'Admin\DepartmentController#index')->name('admin.departments.index');
Route::get('/create', 'Admin\DepartmentController#create')->name('admin.departments.create');
Route::post('/store', 'Admin\DepartmentController#store')->name('admin.departments.store');
Route::get('/{id}/edit', 'Admin\DepartmentController#edit')->name('admin.departments.edit');
Route::post('/update', 'Admin\DepartmentController#update')->name('admin.departments.update');
Route::get('/{id}/delete', 'Admin\DepartmentController#delete')->name('admin.departments.delete');
});
InvalidArgumentException
Route [admin.deparments.index] not defined.
The store function in your DepartmentController returns a typo: admin.deparments.index should be admin.departments.index.

I can't update the field in laravel 5

It is OK in Get, Post, Delete in my laravel code.
But I can't update the field.
function update in BookController.php
$data = $this->request->all();
If show the dd($data), it is null.
What reason?
Help me please.
BookRequest.php Code:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class BookRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name' => 'required|max:255',
'coment' => 'required'
];
}
}
BookController.php Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Book;
use Illuminate\Http\Response;
use App\Http\Requests\BookRequest;
class BookController extends Controller
{
protected $request;
protected $book;
public function __construct(Request $request, Book $book) {
$this->request = $request;
$this->book = $book;
}
public function update(BookRequest $request, $id) {
$data = $this->request->all();
$book = $this->book->find($id);
$book->name = $data['name'];
$book->coment = $data['coment'];
$book->save();
return response()->json(['status' => Response::HTTP_OK]);
}
}
If i were you i would replace the Controller like below:
<?php
namespace App\Http\Controllers;
use App\Book;
use Illuminate\Http\Response;
use App\Http\Requests\BookRequest;
class BookController extends Controller
{
public function update(BookRequest $request, $id) {
$book = Book::find($id);
$book->update($request->all());
return response()->json(['status' => Response::HTTP_OK]);
}
}
If you have set up Route:model binding then you can simplify Code more better. Below code only works if you have a Route::model setup in your route file web.php.
Check this docs for more details:
https://laravel.com/docs/5.6/routing#route-model-binding
public function update(BookRequest $request, Book $book) {
$book->update($request->all());
return response()->json(['status' => Response::HTTP_OK]);
}
Try this:-
$request->all();
instead of
$this->request->all()
I have solved.
My request: http://127.0.0.1:8000/api/book
POST , key: _method: PUT
Update Code
$data = $request->all();
$book = Boook::find($id);
$book->name = $data['name'];
$book->coment = $data['coment'];
$book->save();
Regards.

InvalidArgumentException route notdefined

I have error like (1/1) InvalidArgumentException
Route [home] not defined. whenever i used the store function but i'm pretty sure that i use the redirect method right what could be the possible error, all i wanted was to redirect to home once the store method is done.
web.php
<?php
Route::get('/', function () {
return view('main');
});
Route::get('/create', 'BuildingController#createBuilding');
Route::post('/store', 'BuildingController#store');
Route::post('home', 'BuildingController#getAllBuilding');
Building.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Building extends Model
{
public $timestamps = false;
protected $fillable = [
'id',
'building_name',
'building_information',
'building_image'
];
}
BuildingController.php
<?php
namespace App\Http\Controllers;
use App\Building;
use Image;
use Illuminate\Http\Request;
use App\Repositories\Building\BuildingRepository;
class BuildingController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
private $building;
public function __construct(BuildingRepository $building)
{
$this->building = $building;
}
public function createBuilding()
{
return view('building.create');
}
public function store(Request $request)
{
$this->validate($request, array(
'building_name'=>'required',
'building_information'=>'required',
'building_image' => 'required'
));
$image = $request->file('building_image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/' .$filename);
Image::make($image)->resize(800,400)->save($location);
$buildings = array('building_name' => $request->building_name,
'building_information' => $request->building_information,
'building_image' => $filename);
$this->building->create($buildings);
return redirect()->route('home');
}
public function getAllBuilding()
{
$buildings = $this->building->getAll();
return view('building.home')->with('buildings', $buildings);
}
public function getSpecificRecord()
{
$buildings = $this->building->getById(1);
return view('building.show')->with('buildings', $buildings);
}
}
EloquentBuilding.php
<?php
namespace App\Repositories\Building;
use \App\Building;
class EloquentBuilding implements BuildingRepository
{
private $model;
public function __construct(Building $model)
{
$this->model = $model;
}
public function getById($id)
{
return $this->model->findOrFail($id);
}
public function getAll()
{
return $this->model->all();
}
public function create(array $attributes)
{
return $this->model->create($attributes);
}
public function update($id, array $attributes)
{
}
public function delete($id)
{
}
}
BuildingRepository.php
<?php
namespace App\Repositories\Building;
interface BuildingRepository
{
public function getById($id);
public function getAll();
public function create(array $attributes);
public function update($id, array $attributes);
public function delete($id);
}
Since you're using route(), you need to name the route. Also, make it get:
Route::get('home', 'BuildingController#getAllBuilding')->name('home');
Or:
Route::get('home', ['as' => 'home', 'uses' => 'BuildingController#getAllBuilding']);
You are trying to use route with post, replace it with get and also add/specify name attribute to call route using name.
Route::get('home', 'BuildingController#getAllBuilding')->name('home');
OR
Route::get('home', ['as' => 'home', 'uses' => 'BuildingController#getAllBuilding']);
Above both are comes with same output...

Resources