Call to undefined method Illuminate\Support\Facades\Log::save() - laravel

I created 'Log' class in app/models :
class Log extends Eloquent {
public function user() {
return $this->belongsTo('user');
}
}
When i try to save log object in my controller i got this error (Call to undefined method Illuminate\Support\Facades\Log::save() ) I thik because in (app/config/app) in providers laravel define Log class => 'Log'=> 'Illuminate\Support\Facades\Log',.
How can i resolve this problem without change class name ?

Yes the problem is indeed a conflict with the Log facade alias. To fix it use namespaces:
<?php namespace YourApp;
class Log extends Eloquent {
public function user() {
return $this->belongsTo('user');
}
}
And then you can use your class like so:
$log = new YourApp\Log();
You could of course rename the alias name, but namespacing your classes is a much better approach.

Related

Laravel :: call method in helper class with constructor

as I mentioned in last question I am beginner in Laravel therefore I need some help ,I have to make helper class to create random Id for some tables ,I create this class with table constructor to recieve deffirent tables :
<?php
namespace App\Helpers;
use Illuminate\Support\Facades\DB;
class RandomId
{
public function __construct($my_table)
{
$this->my_table=$my_table;
}
public function get_id()
{
$id = mt_rand(1000000000, 9999999999);
if($this->check_id($id)){
get_id();
}
return $id;
}
public function check_id($id)
{
$table=DB::table($this->my_table)->where('id',$id)->get();
if (count($course)==0){
return false;
}
return true;
}
}
then I add it as alias in config/app.php
'RandomId' => App\Helpers\RandomId::class,
now I want to call it in controller ,I did somethinf like this but don't work :
<?php
namespace App\Http\Controllers\course;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Helpers\RandomId;
class Course_controller extends Controller
{
public function add(Request $request)
{
$id=RandomId::get_id('courses');
dd($id);
}
}
I get this error :Non-static method App\Helpers\RandomId::get_id() should not be called statically
It's because get_id function is not static. You should add static keyword when defining static methods:
public static function get_id()
{
....

How do I make relations laravel in different folders?

My user model like this :
namespace App\Models\Auth;
...
class User extends Authenticatable
{
...
public function vendor()
{
return $this->belongsTo(Vendor::class, 'vendor_id', 'id');
}
}
My vendor model like this :
namespace App\Models;
...
class Vendor extends Model
{
...
public function users()
{
return $this->hasMany(User::class, 'id', 'vendor_id');
}
}
If the relation run, there exist error like this :
Class 'App\Models\Auth\Vendor' not found
It seems that the error occurred because the vendor model is not in the auth folder
How do I solve that error without moving the vendor model to the auth folder?
In simple word, you need to import Vendor class into the User class.
namespace App\Models\Auth;
use App\Models\Vendor; //code to be added
...
class User extends Authenticatable
{
...
public function vendor()
{
return $this->belongsTo(Vendor::class, 'vendor_id', 'id');
}
}
Pretty sure it's because you didn't explicitly load the Vendor class into the User file.
Add into your User file
use App\Models\Vendor;

What does the make() method do in Laravel?

In the Laravel documentation, I found the following - https://laravel.com/docs/5.4/container#the-make-method
but I am still confused as to what exactly the make() method does. I know the create() method uses the make() method and then persists them into the database, so does make() methods just temporarily save it in php tinker or something? Sorry, I'm Laravel noob. I'm trying to figure out these functions. Thank you! :)
The make method will return an instance of the class or interface you request.
Where you request to make an interface, Laravel will lookup a binding for that interface to a concrete class.
E.g.
$app->make('App\Services\MyService'); // new \App\Services\MyService.
One advantage to using the make method, is that Laravel will automatically inject any dependencies the class may define in it's constructor.
E.g. an instance of the Mailer class would be automatically injected here.
namespace App\Services;
use \Illuminate\Mail\Mailer;
class MyService
{
public function __construct(Mailer $mailer) {
$this->mailer = new Mailer;
}
}
I discovered recently that when you use make (), you are installing the class and you can access the methods of that class or model, this is a useful for the Test and validate that you are getting what you want Example:
User model
class User extends Authenticatable
{
public function getRouteKeyName ()
     {
         return 'name';
     }
}
Test user
class UserTest extends TestCase
{
public function route_key_name_is_set_to_name ()
     {
$ user = factory (User :: class) -> make ();
$ this-> assertEquals ('name', $ user-> getRouteKeyName ());
// When you access the getRouteKeyName method you get the return, that is 'name'
}
}
On the other hand if you use "create" that would give an error because you are creating a user

Laravel: Model not working if I named it "Auth"

Why when I name my model Auth it not working at all? But when I change name to different model work correctly?
Not working:
<?php
class Auth extends Eloquent {
public static function check()
{
return "working";
}
}
Working:
<?php
class MyAuth extends Eloquent {
public static function check()
{
return "working";
}
}
Laravel already has a built-in Auth class.
You could remove the line:
'Auth' => 'Illuminate\Support\Facades\Auth',
from app/config/app.php if you're not using Laravel's built-in Auth class.
Auth is a predefined class for user authorization in Laravel. To name a second class Auth too, you will need to put the new one in a different Namespace

how to call model in laravel from controller

How to call a model in laravel.
My code is:
use Jacopo\Authentication\Models\Guide;
class SampleController extends BaseController
{
public function index()
{
$model='Guide';
$guide=$model::where('guide_link','=',"guide")->get();
print_r($guide);
}
}
This will produce Class 'Guide' not found error.
If you added your class you should run in terminal
composer dump-autoload
to update your class map. Otherwise autoloader may not "see" your class and you are getting this error.
You need to add the namespace to your string:
class SampleController extends BaseController
{
public function index()
{
$model='Jacopo\Authentication\Models\Guide';
$guide=$model::where('guide_link','=',"guide")->get();
print_r($guide);
}
}
You could also resolve it from the IoC container, but you need to register it first:
App::bind('Guide', 'Jacopo\Authentication\Models\Guide');
And then you should be able to:
$model = App::make('Guide');
$guide = $model::where('guide_link','=',"guide")->get();
But this is not a very good option

Resources