Controller not found media model in laravel 8 - laravel

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');

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 :)

Error to Retrieve data from database in Laravel

I have a database name lrvesikhon and there is a table named 'person'. I created a model named 'Person' and also create a controller named 'PersonsController'. I also config the .env file. But some error occurs. The error:
My code -
PersonController -
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class PersonsCotroller extends Controller
{
public function index(){
$person_list = Person::all();
dd($person_list);
}
}
Web -
<?php
Route::get('/', 'PersonsCotroller#index');
Person Model -
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
protected $table = "person";
}
Write bellow code after use App\Http\Request;
use App\Models\Person;
If your model Person is not in Models directory change to use App\your-directory\Person;
Your haven't hooked up the model class.
Add in your controller after namespace App\Http\Controllers
use App\Models\Person;

Laravel: Class declaration Compatible error

I am using laravel 5.6, when create controller and when run controller through route, I am facing error like
Declaration of App\Http\Controllers\XyzController::xyz(Illuminate\Http\Request $request) should be compatible with App\Http\Controllers\Controller::xyz($job)
My Code is
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class XyzController extends Controller
{
public function xyz(Request $request)
{
return view('xyz.xyz');
}
}
Missing route parameter: $job
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class XyzController extends Controller
{
public function xyz(Request $request, $job)
{
return view('xyz.xyz');
}
}
The base Controller that XyzController extends from defines a method named xyz with a different signature than the one you are defining.
You will need to adjust the method in XyzController to match the signature of xyz in the base Controller or adjust the base Controller to have a different signature.
Example of the problem:
class A
{
public function xyz($obj) {}
}
class B extends A
{
public function xyz(Illuminate\Http\Request $request) {}
}
Declaration of B::xyz(Illuminate/Http/Request $request) should be compatible with A::xyz($obj)
You forgot to use controller?
use App\Http\Controllers\Controller as Controller

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();
}
}

laravel model class not found

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

Resources