Class 'app\Models\Job' not found why ? What to do? - php

I have this command line that doesn't work but I tried namespace app\Models\Job; or use app\Models\Job; or namespace App\Models\Job; or use App\Models\Job;.
I have also tried directly adding App\Models\Job to the command line but it didn't seem to work.
namespace App\Http\Controllers;
use app\Models\Job;
use Illuminate\Http\Request;
class JobController extends Controller
{
public function __construct(){
$this->middleware(['employer','verified'],['except'=>array('index','show','apply','allJobs','searchJobs','category')]);
}
public function index(){
$jobs = Job::latest()->limit(5)->where('status',1)->get();
$categories = Category::with('jobs')->paginate(5);
$companies = Company::get()->random(6);
return view('welcome',compact('jobs','
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Job extends Model
{
use HasFactory;
protected $fillable = ['user_id','company_id','title','slug','description','roles','category_id','position','address','type','status','last_date','number_of_vacancy','experience','gender','salary'];
public function getRouteKeyName(){
return 'slug';
}
public function company(){
return $this->belongsTo('App\Company');
}
public function users(){
return $this->belongsToMany(User::class)->withTimeStamps();
}
public function checkApplication(){
return \DB::table('job_user')->where('user_id',auth()->user()->id)->where('job_id',$this->id)->exists();
}
public function favorites(){
return $this->belongsToMany(Job::class,'favourites','job_id','user_id')->withTimeStamps();
}
public function checkSaved(){
return \DB::table('favourites')->where('user_id',auth()->user()->id)->where('job_id',$this->id)->exists();
}
}

You should do this instead:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Job; // change app to App
class JobController extends Controller
{...

Related

Class App\Http\Controllers\CreateTagRequest does not exist

I am getting this error when submit value.
Here is my controller:
{ namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tag;
use App\Http\Requests\Tags\CreateTagsRequest;
use App\Http\Requests\Tags\UpdateTagsRequest;
}
This is TagRequest:
<?php
namespace App\Http\Requests\Tags;
use Illuminate\Foundation\Http\FormRequest;
class CreateTagsRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'name'=>'required|unique:tags'
];
}
}

Getting all the products in category

I'm using Laravel for a store project, I created the Models
Model : Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function products(){
return $this->hasMany(Product::class);
}
}
Model : Product.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public function category(){
return $this->belongsTo(Category::class);
}
public function comments(){
return $this->hasMany(Comment::class);
}
}
Controller : ProductController.php
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function getAll($idCategory)
{
$roducts = Category::findOrFail($idCategory)->products();
}
}
I got this error:
Method 'findOrFail' not found in \App\Category
I have tried to solve this by changing to :
$roducts = Category::query()->findOrFail($idCategory)->products();
And I got this error:
Method 'products' not found in \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|static|static[]
UPDATE
I'm using Laravel 5.7.24, and the errors are from the IDE (PhpStorm).

Use relationship with policy of another model

I am using Laravel v5.5.14.
I have an endpoint, for App\Message in routes/api.php:
Route::group(['middleware' => 'auth:api'], function() {
Route::put('messages/{message}', 'MessageController#update')->middleware('can:view,pet');
});
I want to apply the policy of can:view,pet which is defined in my PetPolicy.php here:
namespace App\Policies;
use App\User;
use App\Pet;
use Illuminate\Auth\Access\HandlesAuthorization;
class PetPolicy
{
use HandlesAuthorization;
public function view(User $user, Pet $pet)
{
return $user->pets()->where('pet_id', $pet->id)->exists();
}
}
Every App\Message has a App\Pet I declare that pets have many messages (one-to-many) relationship in my models. And every App\User can have many App\Pet so many-to-many.
app/Pet.php:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pet extends Model
{
protected $fillable = ['name'];
public function messages()
{
// one-to-many hasMany belongsTo
return $this->hasMany('App\Message');
}
public function users()
{
// many-to-many belongsToMany "
return $this->belongsToMany('App\User');
}
}
app/Message.php:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Message extends Model
{
protected $fillable = ['body', 'kind'];
public function pet()
{
// one-to-many hasMany belongsTo
return $this->belongsTo('App\Pet');
}
}
app/User.php:
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
// ....
public function pets()
{
// many-to-many belongsToMany "
return $this->belongsToMany('App\Pet');
}
public function messages()
{
// one-to-many hasMany belongsTo
return $this->hasMany('App\Message');
}
}
However my policy is failing, it is always giving unauthorized. Did I make a mistake somewhere?
This is my app/AuthServiceProvider.php:
namespace App\Providers;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
protected $policies = [
'App\Pet' => 'App\Policies\PetPolicy',
'App\Message' => 'App\Policies\MessagePolicy'
];
public function boot()
{
$this->registerPolicies();
}
}
Thanks
Test it with this query :
namespace App\Policies;
use App\User;
use App\Pet;
use Illuminate\Auth\Access\HandlesAuthorization;
class PetPolicy
{
use HandlesAuthorization;
public function view(User $user, Pet $pet)
{
return User::where('id', $user->id)
->whereHas('pets', function ($query) use($pet) {
$query->where('id', $pet->id);
})
->exists();
}
}
Or just like this if you want :
return $user->pets->contains($pet->id);
Or like this :
return DB::table('pet_user')
->whereUserId($user->id)
->wherePetId($pet->id)
->count() > 0;

Laravel Class App\Http\Composers\MainComposer does not exist

I'm new in laravel and i need some help with laravel 5, why is this erro? the error is in Composer class and ComposerServiceProvider
MainComposer class
<?php
namespace App\Htt\Composers;
use Illuminate\Contracts\View\View;
use App\Category;
class MainComposer {
public function compose(View $view){
$view->with('categories', Category::all());
}
}
ComposerServiceProvider.php
<?php
namespace App\Providers;
use View; // its saing class View does not exist
use Illuminate\Support\ServiceProvider;
class ComposerServiceProvider extends ServiceProvider{
public function boot(){
View::composer('*', 'App\Http\Composers\MainComposer');
}
public function register(){
}
}

Class 'Album' not found

I have a problem in my controller(AlbumController) , I want to use one of my model(Album) in this controller but I got the error Class 'Album' not found
AlbumController.php :
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
class AlbumController extends BaseController
{
public function getList()
{
$albums = \Album::find(1);
return View::make('index')
->with('albums' , $albums);
}
}
Model : Album.php (App/Models/Album)
<?php
namespace App\Models\Album;
class Album extends Model
{
protected $table = 'albums' ;
protected $fillable = ['name' , 'description' , 'cover_image'] ;
public function Photos()
{
return $this->hasMany('images');
}
}
?>
you are getting this error for namespace mismatch.There is 2 way to solve one is use your namespace in top of class
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use App\Models\Album;
class AlbumController extends BaseController
{
public function getList()
{
$albums = Album::find(1);
return View::make('index')
->with('albums' , $albums);
}
}
another is use full namespace in method
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
class AlbumController extends BaseController
{
public function getList()
{
$albums = \App\Models\Album::find(1);
return View::make('index')
->with('albums' , $albums);
}
}
You not use use Album; in yor controller
Try this
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use App\Album;
class AlbumController extends BaseController
{
public function getList()
{
$albums = Album::find(1);
return View::make('index')
->with('albums' , $albums);
}
}

Categories