Laravel 5.6 error in One to Many relationship - laravel-5.6

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

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

Request class injection in repository class

I have following repository class
<?php
namespace App\Repositories;
use App\Customer;
use App\Comment;
use App\CustomerCode;
use Illuminate\Support\Facades\Request;
class CustomerRepository {
use ValidatesRequests;
public function getAll($search=null,Request $request)
{
if($search){
return Customer::where('name','like',"%$search%")->paginate(5);
}
return Customer::paginate(5);
}
}?>
and controller class
<?php
namespace App\Http\Controllers;
use App\Repositories\CustomerRepository;
use Illuminate\Http\Request;
class CustomerController extends Controller{
private $customerRepo;
function __construct(CustomerRepository $customerRepo){
$this->customerRepo = $customerRepo;
}
function index(Request $request){
return $this->customerRepo->getAll($request->input('search',null));
}
}
this gives me error
Type error: Argument 2 passed to
App\Repositories\CustomerRepository::getAll() must be an instance of
Illuminate\Support\Facades\Request, none given, called in
C:\xampp\htdocs\avmaxapi\app\Http\Controllers\CustomerController.php
on line 21
i know i havenot passed 2nd argument but should not it inject automatically
and i donot want to pass request object everytime from controller;
use use Illuminate\Http\Request;
and not: use Illuminate\Support\Facades\Request;
at the following:
<?php
namespace App\Repositories;
use App\Customer;
use App\Comment;
use App\CustomerCode;
use Illuminate\Http\Request;
class CustomerRepository {
use ValidatesRequests;
public function getAll($search=null,Request $request)
{
if($search){
return Customer::where('name','like',"%$search%")->paginate(5);
}
return Customer::paginate(5);
}
}?>

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