I have the following code:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Enemy extends Model
{
// ...
static function fight($id)
{
if(Enemy::calcDist($id))
{
$model = Enemy::find($id);
if($model->status == 1)
{
$model->status = 2;
$model->save();
}
}
}
}
When I try to do App\Enemy::fight(1) in php tinker it shows error:
"Class 'App\App\Enemy' not found".
I tried with "calcDist($id)", with "self::calcDist($id)", also at find($id) function, but no result.
How I can solve this?
Edit: I found the problem; that error comes from another part of code...
When you are in namespace App you dont need to use App\Enemy in your call.
Simply use Enemy::fight(1), or use the absolute namespace \App\Enemy::fight(1)
When you use a static class by his name, the engine search the class into the current namespace. If no namespace is given, then it uses the namespace "\".
namespace App;
Enemy::fight(1); // \App\Enemy::fight(1) ok
App\Enemy::fight(1); // \App\App\Enemy::fight(1) wrong
Related
I want to create a directory for utils in Laravel 8. I have this but it doesn't work:
app/Utils/DateTime.php:
<?php
namespace App\Utils\DateTime;
const ISO8601_DATE_FORMAT = "Y-m-d\TH:i:s.uP";
function parseISO8601(string $time): \DateTime {
if ($time.endsWith("Z")) {
$time = $time.str_replace($time, "Z", "+00:00");
}
return \DateTime::createFromFormat(ISO8601_DATE_FORMAT, $time);
}
app/Http/Controllers/SearchController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
use function App\Utils\DateTime\parseISO8601 as parseISO8601;
class SearchController extends Controller
{
public function run(Request $request)
{
parseISO8601("2021-10-03T10:00:45.145126+01:00");
}
}
But I get an error:
Call to undefined function App\Utils\DateTime\parseISO8601()
What am I missing? It seems that it can't autoload DateTime for some reason. Do I need to manually configure an extra path somewhere in Laravel?
I changed it to a class and restarted PHP. It then gave me an error:
Class App\Utils\DateTime\DateTime located in ./app/Utils/DateTime.php does not comply with psr-4 autoloading standard. Skipping. which is explained here
I had incorrectly specified the namespace as App\Utils\DateTime even though the file was called DateTime.php. I just needed to change the namespace to App\Utils.
I have a CRUD app, everything works except updating tags
Here is the update function in my controller
namespace App\Http\Controllers;
use App\Tag;
use App\PageList;
use App\PageListTag;
use Illuminate\Http\Request;
public function update(Request $request, $id)
{
$pages = PageList::find($id);
$pages->pagetitle = $request->get('pagetitle');
$pages->articlelist = $request->get('articlelist');
$pages->status = $request->get('status');
$pages->save();
$pages->tags()->saveMany([
new App\Tag(),
new App\Tag(),
]);
return redirect('/pages')->with('success', 'pages updated!');
}
Here is the Tag model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
{
protected $fillable = ['page_list_id', 'page_list_tag_id'];
protected $with = ['tag'];
public function tag()
{
return $this->belongsTo('App\PageListTag', 'page_list_tag_id', 'id');
}
}
When I run my app I am getting the following error
Class 'App\Http\Controllers\App\Tag' not found
What am I doing wrong in my code?
You're resolving in the wrong way the models namespaces. Please have a look at the official PHP documentation
In your code you're resolving the Tag class as follows
use App\Tag; // <-- This is right
But in your method you're calling
$pages->tags()->saveMany([
new App\Tag(), // <-- And this is wrong!
new App\Tag(),
]);
You simply have to call new Tag() since the use at the top of your file has already included the class.
Otherwise PHP will try to resolve the class from the current namespace. That's why it's throwing
Class 'App\Http\Controllers\App\Tag' not found
To be right you should have added a \ before App\Tag, so PHP will resolve the class from the root. In this case, the use statement will be useless
Your namespace is App\Http\Controllers, so when you create a tag with the syntax new App\Tag() it is indeed translated into App\Http\Controllers\App\Tag.
So just replace your instructions new App\Tag() with new Tag().
Alternatively, you could also use the absolute notation:
new \App\Tag()
I'm starting Laravel and i'm trying to create an external class to use like a 'Library'.
I've searched a lot and came up with this solution :
I created a folder 'Services' in 'App' and made a class file like so :
App/Services/OvhApiHandlerClass.php
This file looks like so :
<?
namespace App\Services;
use App\OvhBill;
use App\OvhBillDetail;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Ovh\Api;
class OvhApiHandlerClass
{
public function ovh_get_bills(string $consumer_key, $from = null, $to = null)
{
// Do something
}
}
So now i want to use this class inside a controller.
But i'm getting an error saying my class does not exist.
Here is my Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Services\OvhApiHandlerClass;
class OperationsController extends Controller
{
public function index()
{
$from = new \Carbon\Carbon('first day of January 2019');
$ovhHandler = new OvhApiHandlerClass();
$ovhHandler->ovh_get_bills('K87u3410p89ijKLao', $from);
return view('operations.index');
}
}
I already did, of course
composer dump-autoload
I'm kinda lost, what am i missing?
Thank you very much for your time !
here is my code:
<?php
use Illuminate\Support\Facades\Session;
namespace App\CustomLibrary
{
class myFunctions {
public function is_login() {
if(Session::get('id') != null){
return TRUE;
}
}
}
}
?>
I'm new in laravel 5, i just added a new custom function. And inside that function i wanna check a session ('id)? But i've got an error like this
FatalErrorException in myFunctions.php line 8:
Class 'App\CustomLibrary\Session' not found
I need to know how to use session properly.
Add this clause to the top of the class, right after namespace part:
use Session;
Or use full namespace:
if (\Session::get('id') != null)
Or use the session() helper:
if (session('id') != null)
Your use needs to be after the namespace declaration:
//use Illuminate\Support\Facades\Session; //Not here
namespace App\CustomLibrary
use Illuminate\Support\Facades\Session; //Here
Anything that is used before the namespace is used within the global namespace which changes once a namespace is declared.
use Session; in Model and Controller
// Via a request instance...
$request->session()->put('key', 'value');
// Via the global helper...
session(['key' => 'value']);
for more details https://laravel.com/docs/5.1/session
I use subfolder in the Controller 'folder',which works fine..
but when I write the blow code ..php return the error said "Auth is not found ,and the Input'
<?php
namespace website;
use Auth;
use Input;
use View;
use Illuminate\Routing\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return View::make('wcsite.index');
}
public function saveHome()
{
$uid = Auth::user()->id;
$websiteData = Input::get('data');
return $uid;
}
}
but when I add 'use Auth,use Input',everything works fine...so ,anyone who can tell me ...is there any way to to this ,which "need not to use Auth,use Input in my subfolder Controllers' Thank you a lot!
and my route is
Route::post('/wcsite',array('uses' => 'website\HomeController#saveHome'))->before('auth');
Your question is a bit confusing. You're saying that the code above is not working because PHP can't find the Auth and Input global class references but your code clearly shows you're importing them correctly.
PHP can't use the global Auth and Input class references without importing them first (which you're doing in the above code). It's going to assume they're located under the website namespace by default.
If you don't want to import hem with use statements you could always reference the global namespace by using a backslash before the class name like the code below:
<?php
namespace website;
use Illuminate\Routing\Controllers\Controller;
class HomeController extends Controller {
public function index()
{
return \View::make('wcsite.index');
}
public function saveHome()
{
$uid = \Auth::user()->id;
$websiteData = \Input::get('data');
return $uid;
}
}
That being said, I prefer importing the classes first instead of using backslashes everywhere. It'll provide for much cleaner code.