I'm trying to use the code provided by the documentation of entrust in a controller but save() method wont execute and gives me the error
Method [save] does not exist on [App\Http\Controllers\role]
Here is the code:
$cityadmin = new Role();
$cityadmin->name= 'cityadmin';
$cityadmin->save();
Use full namespace:
$cityadmin = new \App\Role;
Or add this to the top of the controller:
use App\Role;
Related
I'm trying to automatically create a profile for a user when a user is created.
I'm using the created event and overriding the boot() method. But I when call the create() method on user->profile->create(), it says create was called on null. I checked and profile is null in this.
Here's the code:
static::created(function ($user) {
// it returns profile as null, thus create() can't be used on null.
$user->profile->create(['title' => $user->username,]);
});
Can anyone help me understand this? It's working in my tutor's code, and he is using Laravel 5.8 but I have version 7.1.
$user->profile returns the related model if any exists. You have to do $user->profile() which returns a query builder to query the relation. Try to do it like so:
$user->profile()->create(['title' => $user->username,]);
I tried to define a local scope in my project to show popular(most downloaded)in my Home Page...so this is my File Model codes:
public function scopePopular($query)
{
return $query->orderBy('file_download_num');
}
As you know "file_download_num" is the field in database im trying to get data from...
so this is how I used this scope in my HomeController:
$popularFiles = File::Popular()->get();
I didn't use count() method in my Controller or Model!But still getting Error!so any suggestion?
notice:php version is 7.3
I found that adding a toArray() method in controller can help count() method work correctly...it worker for me...so controller should be like this:
$popularFiles = File::Popular()->get()->toArray();
I'm using zizaco/entrust package in my laravel project beside multi Auth package name Hesto/multi-auth
Our project on laravel 5.4,
i get below error when i want to get current logged in users' role and permissions with this method:
Entrust::hasRole('role-name'); OR Auth::user()->hasRole('role-name');
But I can access users' Role with this method for example :
$user = User::find($userid);
dd($user->hasRole('admin')); // Return true
i followed exactly installation instruction but i get below error :
`Non-static method Zizaco\Entrust\Entrust::hasRole() should not be called statically`
How can i solve my problem,
Thanks in advance
In this error message you have answer to your problem:
Non-static method Zizaco\Entrust\Entrust::hasRole() should not be called statically
You called this method hasRole() statically, but this method is non static. It means that you need to create object of this class, but you instead that used a class.
In the example that you gave:
$user = User::find($userid);
dd($user->hasRole('admin')); // Return true
you create an object of class User, and class User (I think) implements class Entrust:
$user = User::find($userid);
$user is an object and it`s not static, you can use hasRole().
In other words, to use method hasRole() (literally - is someone has a role?) you need this someone:
$user->hasRole('admin') // Is this user has role `admin`?
Hope, I explained it. Sorry about my English (I'm just studying).
In your User.php file add EntrustUserTrait like
<?php
namespace App;
//...
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Authenticatable
{
use Notifiable, EntrustUserTrait;
//...
I'm try to create a fake data for testing, and have the following code on the Behat Context
$modelFake = factory(User::class)->create();
$client = new GuzzleHttp\Client();
$res = $client->request('GET', url($this->apiURL . '/user/' . $modelFake->id));
After the class declaration I have:
use DatabaseTransactions;
The user fake it's created and I get the current Id to do the request, but the code return an empty object, If I remove the use DatabaseTransactions; the user persist on the database and the request return the object, but I can't realize what is my issue.
I hope someone can help me with this.
Regards
Edit:
When I try to get the user User::find($modelFake->id) I'm getting the object :(
Why when I made the request I can't get the object?
Edit 2:
The main problem it's the data persist only on the context and the request belong to another instance of the DB
I am trying to pass data to a view using the boot method in the AppServiceProvider, and have followed the steps outlined in one of the Laracast fundamentals videos. However, I am getting an error in my controller when trying to use this variable.
In my AppServiceProvider:
public function boot()
{
view()->composer('home', function($view){
$view->with('current', Post::orderBy('created_at', 'desc')->first());
});
}
So this should pass the data to my home view as far as I understand, so that I don't need to keep referencing it in all the methods I have in the controller for that view. I then have a homeController with the following method:
public function index()
{
//Get user
$user = Auth::user();
//OMDB API setup - Get Movie name, remove spaces, add into API request
$movie_one = str_replace(" ", "+", $current->movie_one);
$movie_two = str_replace(" ", "+", $current->movie_two);
return view('home', compact(array('user')));
}
Previously I was adding a variable called current in this method to get an entry from my Post table, but wanted to add it to the AppServiceProvider as I will have to re-declare it in other methods as well. The issue I have is that I try to use the $current object again in this method, but it isn't available to it? I get the following error:
ErrorException in homeController.php line 30:
Undefined variable: current
What can I do here? Can I pass that data from to a controller as well as the home view? Or is that not possible?
the code you written inside the boot method, will only be accessible inside home view, if you want to access the Post object inside controller too, you have to follow this, through this you can access your Post object in controller, as will as view.
app()->singleton('current',function($app){
return Post::orderBy('created_at', 'desc')->first();
});
So in your code, you can access the Post object like this,
$post_obj = App::make('current');
$post_obj->movie_two;
or directly
App::make('current')->movie_two;