laravel model class not found - laravel

I'm getting class model not found error
In my model class
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $table = 'customers';
}
And in my controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use \app\Models\Customer;
use Illuminate\Support\Facades\Input;
class RoomsController extends Controller
{
public function index()
{
$room= Customer::all();
return view('rooms',compact('room'));
}
}
I even tried composer dump-autoload.
I'm still getting the same error

Use use in head of your controller
use App\models\Customer;
Than you can call it in your function:
Customer::find($id);//etc

use App\Customer;
Use this in head of the controller

Related

App\Http\Controllers\admin\GolbalModel in laravel 7

i am creating one Golbalmodel and i am calling this into ProfileController Constructor
Folder structure for models:
App/models/Golbalmodel.php
App/Http/Controllers/admin/ProfileControllers
Why my Golbalmodel is not access in Controller, it gives error as
Illuminate\Contracts\Container\BindingResolutionException
Target class [App\Http\Controllers\admin\GolbalModel] does not exist.
Golbalmodel.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DB;
class GlobalModel extends Model
{
use HasFactory;
}
Controller
ProfileController
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use App\Models\GlobalModel;
use Illuminate\Http\Request;
class ProfileController extends Controller
{
public function __construct(GlobalModel $model)
{
$this->model = $model;
}
public function index()
{
return view('admin.login.profile');
}
public function getAdminDetails(Request $request, $id)
{
$code=$request->id;
echo $code;
}
}
First, the className and filename need to be the same:
WRONG: Golbalmodel.php -> GlobalModel
RIGHT: GlobalModel.php -> GlobalModel
Second, the Namespaces need to have the first "letter" in uppercase:
WRONG: namespace App\Http\Controllers\admin;
RIGHT: namespace App\Http\Controllers\Admin;
Pay close attention to case-sensitive when writing the name of Classes and Files. See if with these fixes Laravel can load the Model :)

Controller not found media model in laravel 8

I tested to using laravel but i get some error unexpected, i already follows tutorial and some video. i think, i do it right. but i get this error. The problem controller not found media model.
Class "App\Media" not found
Route
Route::get('/', [MediaController::class, 'index']);
MediaController
namespace App\Http\Controllers;
use App\Media;
class MediaController extends Controller
{
public function index(){
return Media::all;
}
}
Media model
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Media extends Model
{
protected $table = 'media';
public $timestamps = false;
protected $primaryKey = 'Media_ID';
}
Your use statement for your Media model is incorrect. As of Laravel 8 the default namespace for models is App\Models where previously they where in the App namespace.
namespace App\Http\Controllers;
use App\Models\Media;
class MediaController extends Controller
{
public function index(){
return Media::all;
}
}
Ensure that the Media.php file exists in your app/Models directory and is capitalised correctly.
Then in your web.php file, don't forget to include the use statement for your MediaController at the top.
use App\Http\Controllers\MediaController;
Use this code:
Your Controller:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
class MediaController extends Controller {
public function index() {
return Media::all;
}
}
Your Route:
Route::get('/', 'MediaController#index');

Eloquent query doesn't use the where/first conditions. Why?

Ma simple eloquent query doesn't use the where condition.
Players::where('id_player',"=" ,3113)->first()->toSql();
Returns:
"select * from players"
My Controller:
namespace App\Http\Controllers\Office;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Players;
class OfficeController extends Controller{
public function playerLineup(Request $request){
$e = Players::where('id_player',"=" ,3113)->first()->toSql();
dd($e);
}
}
My Modal:
namespace App;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
class Players extends Model{
protected $primaryKey = 'idp';
}
Once you run ->first() the result is a fresh instance of the Player model. then ->toSql() on a fresh model is like Player::query()->toSql().
you need to call ->toSql() before that:
Players::where('id_player',"=" ,3113)->toSql();
or
Players::where('id_player',"=" ,3113)->limit(1)->toSql();
/try to do this/
namespace App\Http\Controllers\Office;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Players;
use DB;
class OfficeController extends Controller{
public function playerLineup(Request $request){
$e = DB::table('Players')->where('id_player',"=" ,3113)->get()->first();
dd($e);
}
}

Custom validation using make:request returns controller not found

Using php artisan make:request StoreUserData I've create my rules to the request:
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreUserData extends FormRequest {
public function rules(){
return [
'name'=>'required|integer',
'surname'=>'required|max:255|string',
];
}
}
And I'm trying to use it in controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserDataController extends Controller {
public function store(StoreUserData $request){
return 'valid';
}
}
Here the error I get: Class App\Http\Controllers\StoreUserData does not exist.
PS. Is not a problem of routing.
I'm following this documentation since I'm using Laravel 5.6 https://laravel.com/docs/5.6/validation#creating-form-requests
Actually when you use StoreUserData in controller method then you have to import that class otherwise it will assume the class is in App\Http\Controllers namespace and thats why it throw Class App\Http\Controllers\StoreUserData does not exist.
just add the below import at top of your controller class
use App\Http\Requests\StoreUserData

Laravel 5.6 error in One to Many relationship

My One to Many relationship is like One Sura has many ayah.
Sura Model is like below
Sura.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Sura extends Model
{
public function ayahs()
{
return $this->hasMany('App\Model\Quran', 'surah_id', '_id');
}
}
Quran Model is like below
Quran.php
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
class Quran extends Model
{
}
Sura Controller is like below
SuraController.php
<?php
namespace App\Http\Controllers;
use App\Model\Sura;
use Illuminate\Http\Request;
class SuraController extends Controller
{
public function show(Sura $sura)
{
return Sura::find($sura)->ayahs();
}
}
My route is like below
api.php
Route::apiResource('/suras', 'SuraController');
I am getting error like below
I am trying to browse using below URL.
http://127.0.0.1:8000/api/suras/2
SuraController.php
<?php
namespace App\Http\Controllers;
use App\Model\Sura;
use Illuminate\Http\Request;
class SuraController extends Controller
{
public function show($id)
{
return Sura::find($id)->ayahs();
}
}

Resources