namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
private static $token = 'PWPu3Wl71N39x3M';
public static function getToken() {
return self::token;
}
}
How can I get token?
I don't want made constant, I need private static $token = 'PWPu3Wl71N39x3M';
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Person extends Model
{
/** #var string */
private const TOKEN = 'text';
/**
* #return string
*/
public static function getToken(): string
{
return self::TOKEN; // text
}
}
/**
* Usage
*/
Person::getToken(); // text
Related
I have this model class (the filename is Audit.php):
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
abstract class AuditStatus
{
const UNKNOWN = "UNKNOWN";
const ERROR = "ERROR";
const WARNING = "WARNING";
const MSG = "MESSAGE";
const EXCHANGE_UPDATE = "EXCHANGE_UPDATE";
const PRICE_UPDATE = "PRICE_UPDATE";
}
class AuditCodes extends AuditStatus
{
}
class Audit extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = ['action', 'msg'];
public static function Add($action, $msg){
(new static)::insert(['action'=>$action, 'msg' => $msg]);
}
}
And im trying to make a new command like this:
<?php
namespace App\Console\Commands;
use App\Models\AuditCodes;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class PriceCreate extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'price:create';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Create prices';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
dd(AuditCodes::MSG);
}
}
but when I run the command the compiler throws this error:
Error
Class "App\Console\Commands\App\Models\Audit\Audit_Codes" not found
at C:\xampp\htdocs\bintest\app\Console\Commands\PriceCreate.php:46
Can anyone help me on how to declare my class?
Thanks!!!
Try to declare each class in its own file.
I mean if you have three classes: Audit, AuditCodes and AuditStatus
You will also have three different files in App\Models\ directory, like that App\Models\Audit.php, App\Models\AuditCodes.php and App\Models\AuditStatus.php.
It should be solve your problem.
I have a PhotoController that scan a directory and foreach images create a new record in a database and dispatch a job to manipulate the image, but can't make the job work!
Here the Photo Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Photo extends Model
{
protected $table = "photos";
protected $fillable = [
'org_path'
];
}
Here the PhotoController:
namespace App\Http\Controllers;
use App\Photo;
use App\Jobs\ProcessImage;
class PhotoController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
// Get Photos inside the private folder org_folder
$org_images = preg_grep('~\.(jpeg|jpg|png)$~', scandir(storage_path('app/images/')));
foreach ($org_images as $image) {
$post = new Photo;
$post->org_path = storage_path('app/images/').$image;
$post->pub_path = NULL;
$post->save();
$this->dispatch(new ProcessImage($post));
}
}
}
Here the Job:
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Image;
use App\Photo;
class ProcessImage implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $post;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct(Photo $post)
{
$this->post = $post;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$resized_image_path = storage_path('app/public/').rand(5, 100).'.jpg';
$image = Image::make($post->org_path);
$image->resize(200,200)->save($resized_image_path);
}
}
I can't access in some way to the image from the job. Can you please tell me what I'm missing?
You should be accessing the post object using $this->post on the 2nd line of the handle() method and not $post that you have assigned on the constructor. Hopefully that fixes the issue.
This is how I create helper (App\Helpers\Settings.php)
namespace App\Helpers;
use Illuminate\Database\Eloquent\Model;
class Settings {
protected $settings = [];
public function __construct() {
$this->settings['AppName'] = 'Test';
}
/**
* Fetch all values
*
* #return mixed
*/
public function getAll () {
return $this->settings;
}
}
Creating facade (App\Helpers\Facades\SettingsFacade.php)
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class Settings extends Facade {
protected static function getFacadeAccessor() {
return 'Settings';
}
}
Creating Service Provider (App\Providers\SettingsServiceProvider.php)
namespace App\Providers;
use Illuminate\Support\Facades\App;
use Illuminate\Support\ServiceProvider;
class SettingsServiceProvider extends ServiceProvider {
/**
* Bootstrap the application events.
*
* #return void
*/
public function boot() {
}
/**
* Register the service provider.
*
* #return void
*/
public function register() {
App::bind( 'Settings', function () {
return new \App\Helpers\Settings;
});
} */
}
Registering provider (App\Providers\SettingsServiceProvider::class)
Creating alias: 'Settings' => App\Facades\Settings::class
Running composer dump-autoload
Trying to use facade Settings::getAll();
Getting error Class 'App\Http\Controllers\Settings' not found
Can’t figure out why I cannot create facade and getting that error
try this one.
App\Helpers\Settings.php
namespace App\Helpers;
use Illuminate\Database\Eloquent\Model;
class Settings {
protected $settings = [];
public function __construct() {
$this->settings['AppName'] = 'Test';
}
/**
* Fetch all values
*
* #return mixed
*/
public function getAll () {
return $this->settings;
}
}
App/Http/Controllers/XyzController.php
use Facades\App\Settings;
class XyzController extends Controller
{
public function showView()
{
return Settings::getAll();
}
}
web.php
Route::get('/','XyzController#showView');
use Facades\App\Helpers\Settings;
Route::get('/direct',function() {
return Settings::getAll();
});
use laravel Real time facades
I generate one simple insert,update and delete application. when I run my application, I get error. my application files are below....
Route.php
Route::post('insertdata','ContactusController#store');
ContactusController.php
use App\ContactusModel;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
/* namespaces */
use App\User;
use Symfony\Component\HttpKernel\Client;
use Illuminate\Support\Facades\Redirect;
class ContactusController extends Controller {
public function __construct()
{
}
public function index()
{
return view('contact.contact');
}
public function store()
{
$input = Input::all();
ContactusModel::insertall($input);
return view('contact.contact');
}
}
ContactusModel.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class ContactusModel extends Model {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'contactus_models';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['fullname','mobileno', 'email', 'message'];
public static insertall($data)
{
return DB::table('users')->insert($data);
}
}
I run this code. Error will be display like this....
FatalErrorException in ContactusModel.php line 24:
syntax error, unexpected 'insertall' (T_STRING), expecting variable (T_VARIABLE)
public static insertall($data)
{
return DB::table('users')->insert($data);
}
You haven't added the word function
public static function
late night programming :P ?
I'm beginner in laravel. What I tried is composer dump-auto, but did not work.
This Code is in Laravel 5.0 Every answer will be appreciated.
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use View,
Response,
Validator,
Input,
Mail,
Session;
class UserController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
return view('pages.default');
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function insert()
{
$
$user = User::create(['u_name' => 'inputName', 'u_eml' => 'inputMail', 'u_contact' => 'inputContact']);
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* #param int $id
* #return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param int $id
* #return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return Response
*/
public function destroy($id)
{
//
}
}
This is my Model: User.php
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
protected $table = 'user';
public $timestamps = false;
}
Just add use App\User in your top list - like this:
use App\User;
Or you can change your controller code to be \App\User::create(... (notice the \ at the beginning)
Simply add
use App\user;
before the class declaration
use App\User in the controller like this.
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use App\User;
class User extends Model implements AuthenticatableContract,CanResetPasswordContract {
use Authenticatable, CanResetPassword;
protected $table = 'user';
public $timestamps = false;
}