laravel public variable for different controllers - php

I want to set a public variable so I can use it in different controllers. I tried this but it is not successful.
class HomeController extends Controller
{
public $TravelId;
public function __construct()
{
$this->TravelId=0;
}
}
then I used the same variable in another contorller
class HomeController extends Controller
{
public function index($cus_id)
{
//unique key for each user
$this->TravelId = $cus_id;
}
}

All your controllers extend App\Http\Controllers\Controller class by default, so you can add a property on this class and access it on all your controllers:
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
/**
* Your static property
*/
public static $travelId;
}
Then you can access it on another controller:
self::$travelId = $cus_id;

Related

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

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
{...

Laravel 5.5 - Class Custom BaseController not found but exists

Laravel 5.5 Custom BaseController not found even though it exists. Have checked the other questions on StackOverflow regarding the BaseController not found but they are referring to the default BaseController which isn't the same in mine case.
Here is my implementation
Controller.php
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as CheckController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class Controller extends CheckController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}
Custom BaseController (BaseController.php)
use App\Http\Controllers\Controller;
class BaseController extends Controller
{
/**
* Setup the layout used by the controller.
*
* #return void
*/
public $data = array();
public function __construct()
{
if (Sentinel::check()) {
// User is not logged in, or is not activated
$this->data['admin'] = Sentinel::getUser();
}
}
protected function setupLayout()
{
if (!is_null($this->layout)) {
$this->layout = View::make($this->layout);
}
}
}
Extending a class named HomeCtontroller to Custom BaseController
class HomeController extends BaseController {
protected $layout = 'master';
public function main()
{
...
}
}
And then it gives the following error
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Class 'BaseController' not found
Would appreciate any sort of pointers.
I believe you haven't included full namespaces.
Make sure in BaseController you have:
namespace App\Http\Controllers;
and in HomeController make sure you are using:
use App\Http\Controllers\BaseController;
all those controllers should be located in app/Http/Controllers directory.
If you are sure you have valid directories and namespaces run composer dump-autoload in console

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

How to access a class variable from same class

In this snippet:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product_variant extends Model
{
protected $primaryKey='variant_id';
public $translationForeignKey = $this->primaryKey;
}
This rule is not working:
public $translationForeignKey = $this->primaryKey;
How can we access this variable that is in the same scope of this class?
Either set the value in a constructor or create a getter to return the value.
// option 1
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product_variant extends Model
{
protected $primaryKey='variant_id';
public $translationForeignKey = '';
// option 1
public function __construct()
{
$this->translationForeignKey = $this->primaryKey;
}
}
// option 2, you dont even need the other property in this method, unless its value may change during execution
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product_variant extends Model
{
protected $primaryKey='variant_id';
// option 2
public function getTranslationForeignKey()
{
return $this->primaryKey;
}
}
At the time of defining the class you can only assign constant values to the class properties. Variables are not allowed here.
You need to do assignment part in the constructor.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product_variant extends Model
{
protected $primaryKey='variant_id';
public $translationForeignKey;
public function __construct()
{
$this->translationForeignKey = $this->primaryKey;
}
}

Categories