Error to Retrieve data from database in Laravel - 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;

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

Laravel 5.5 connect your class in the controller

There is a file "MyClasses.php" in the folder "App":
namespace App;
use Illuminate\Database\Eloquent\Model;
class Model1 extends Model {}
class Model2 extends Model {}
How to connect it in the controller using use?
I suggest you to read about PSR-4 standards:
https://www.php-fig.org/psr/psr-4/
MyClasses.php is not a valid name for a model in this case, because a) none of the classes defined inside of it are called MyClasses and b) there are numerous class definitions in this file.
// App/Model1.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Model1 extends Model {
protected $table = 'some_table';
}
// Controller
use App\Http\Controllers\Controller;
use App\Model1;
class SomeController extends Controller
{
public function index()
{
$record = Model1::where('some_field', 1)->get();
}
}
EDIT: to clarify.
Both models should be in their own files, called Model1.php and Model2.php under the App folder. Also, you model names * should * correspond to the table names they are accessing. So if for example Model1 is going to be bound to table user_confirmations you should rename your file and class to UserConfirmations - this would be considered best practice.

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