I'm trying to write a test command in Laravel 9. In the below code the new user will create successfully But after creating need to redirect the Dashboard page
public function test_an_action_that_requires_authentication()
{
$user = $this->artisan('make:user',[
'name' => "username",
'email' => "useremail",
'password' => Str::random(8)
]);
}
How to redirect to the route after success created
We will use the assertExitCode method to assert that the command completed with a given exit code.
$this->artisan('module:import')
->expectsConfirmation('Do you really wish to run this command?', 'no')
->assertExitCode(1);
for more information you can find here
https://laravel.com/docs/9.x/console-tests
N.B. This code does not work in test. You can not redirect in an artisan command.
Said that, in laravel, to redirect to the home page, you can use this code in a controller function:
return redirect(url('/'));
Related
I have a simple dashboard in which users can register and do some stuff, now I would like to generate a file with username and his id; something like widget/obama2030.js in Laravel when a user is registered
my app structure:
myapp
-app
-bootstrap
-database
-resource
.....
-widget
-------
in my user registration controller, I have the following.
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
File::makeDirectory('/widget/'.$user->name.Auth::user()->id, 0755, true);
return $user;
}
Now when user click the button register I am getting the following error
ErrorException (E_NOTICE)
Trying to get property of non-object
what is wrong with my code????
You've created a user, but you haven't logged them in as that newly created user.
As such, Auth::user() is null, and (null)->id won't work.
After creating your user, log them in:
Auth::login($user)
and your code should work.
That said, it's typically not necessary (and it's quite messy) to actually create files for each user - you can almost certainly handle this via a Laravel route, instead.
Auth::user() isn't returning anything, so it flakes out when you try and access ->id
any body has any idea how to pass the authentication of laravel ? or to add an admin from database or the source code ? assuming you have created an admin panel and an admin user but you lost your database and now you just want to create a new admin user for your panel . i used default laravel authentication and middle ware for admin panel . here is the middle ware added in kernel.php
'admin' => \App\Http\Middleware\AuthAdmin::class,
and here we got role controller
if(Auth::user()->isSuperAdmin()) {
$objects = Role::select($this->dt_fields_db)->where('id','<>',1)->where('id','<>',3);
} else {
$objects = Role::select($this->dt_fields_db)->where('id','>',3);
}
Make a new Seeder class, create a user and the super admin role, assign the role to the user and be a super admin forever and ever.
public class SuperAdminSeeder {
public function run () {
// modify to following commands fit your table structure
$role = Role::create(['name' => 'super_admin'];
$user = User::create(['email' => 'your#email.com', 'password' => bcrypt('secret')]);
DB::table('role_user')->insert(['user_id' => $user->id, 'role_id' => $role->id]);
}
}
Call it via the command line:
php artisan db:seed --class=SuperAdminSeeder::class
Just run php artisan make:auth and php artisan migrate. Then, navigate your browser to http://your-app.dev/register or any other URL that is assigned to your application. These two commands will take care of scaffolding your entire authentication system.
Recently I'm learning about laravel. So, I'm try to create a simple registration form with an email confirmation. Here is what I try to send the email. Here is my script
Mail::to($email)->send(new Confirmation([
'link' => URL::route('confirm', $email),
'username' => $username]));
But with my script above I get an error
Route [confirm] not defined.
After I read the message, I'm trying to add Route, because the error say route
Route::get('/confirm/$email', 'Loginsite#confirm');
You will need to name your route, so Laravel knows where to look when you're referring URL::route('confirm')
Try this:
Route::get('/confirm/$email', 'Loginsite#confirm')->name('confirm');
Edit: You should also pass your parameters into your route correctly, see https://laravel.com/docs/5.4/routing#required-parameters
The above should read:
Route::get('/confirm/{email}', 'Loginsite#confirm')->name('confirm');
I try to use the package Laravel\Socialite in my system in Lumen (5.1)
I added this in the config\services.php file :
<?php
//Socialite
'facebook' => [
'client_id' => '##################',
'client_secret' => '##################',
'redirect' => 'http://local.dev/admin/facebook/callback',
],
In bootstrap\app.php file :
class_alias(Laravel\Socialite\Facades\Socialite::class, 'Socialite');
$app->register(Laravel\Socialite\SocialiteServiceProvider::class);
Then I created a controller for the facebook authentication :
<?php
namespace App\Http\Controllers\Facebook;
use App\Http\Controllers\Controller;
use Laravel\Socialite\Contracts\Factory as Socialite;
class FacebookController extends Controller
{
public function redirectToProviderAdmin()
{
return Socialite::driver('facebook')->scopes(['manage_pages', 'publish_actions'])->redirect();
}
public function handleProviderCallbackAdmin()
{
$user = Socialite::driver('facebook')->user();
}
}
And in the routes.php :
$app->get('/admin/facebook/login', 'App\Http\Controllers\Facebook\FacebookController#redirectToProviderAdmin');
$app->get('/admin/facebook/callback', 'App\Http\Controllers\Facebook\FacebookController#handleProviderCallbackAdmin');
I just followed the documentation, changing according to my needs. When I go to page http://local.dev/admin/facebook/login, I get the following error :
Non-static method Laravel\Socialite\Contracts\Factory::driver() cannot be called statically, assuming $this from incompatible context
Indeed, according to the code, driver function must be instanciate.
EDIT : And if I try to instanciate this class, I get the following error :
Cannot instantiate interface Laravel\Socialite\Contracts\Factory
How do you make this module to work?
here's how that works in my case
in services.php file
'facebook' => [
'client_id' => '***************',
'client_secret' => '***************',
'redirect' => ""
],
i left redirect empty cause my site is multilingual (so, it fills in a bit later with sessions). if you use only one language, put there your callback absolute path. for example
"http://example.com:8000/my-callback/";
also check your config/app.php. in providers array
Laravel\Socialite\SocialiteServiceProvider::class,
in aliases array
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
my routes look like this
Route::get('facebook', 'Auth\AuthController#redirectToProvider');
Route::get('callback', 'Auth\AuthController#handleProviderCallback');
here's auth controllers methods. put in top
use Socialite;
//იობანი როტ
public function redirectToProvider(Request $request)
{
return Socialite::with('facebook')->redirect();
}
public function handleProviderCallback(Request $request)
{
//here you hadle input user data
$user = Socialite::with('facebook')->user();
}
my facebook app
giga.com:8000 is my localhost (so its the same localhost:8000)
as you can see in Valid OAuth redirect URI, you should put there your callback. in my case i use three urls cause i have three languages. in your case it should be just
http://your-domain-name.com:8000/callback
if you work on localhost, you should name your domain in config/services.php
mine look like this
'domain' => "your-domain.com",
after everything run these commands
php artisan cache:clear
php artisan view:clear
composer dump-autoload
restart your server, clear your browser cookies. hope it helps
I am using Laravel 5 to generate a PDF from a subscription generated from Cashier. The docs say this is as simple as calling:
return $user->downloadInvoice($invoice->id, [
'vendor' => 'Your Company',
'product' => 'Your Product',
]);
Unfortunately I'm getting an odd error:
No hint path defined for [cashier]
The code I am actually using is as follows:
Route::get('billing/invoices/download/{id}', function($id){
$user = Auth::user();
//$invoice = $user->invoices()->find($id);
return $user->downloadInvoice($id, [
'vendor' => 'Certify Me',
//'product' => $invoice->lines->data[0]['plan']->name,
'product' => 'Subscription',
]);
});
The docs make me assume that the PDF is automatically generated. I'd then assume I could override the PDF layout if I chose to.
I just ran into this (L5.1, Cashier 6.0). This seems to be caused by the service provider not being correctly loaded.
Here is how I fixed it:
Check that you have added the correct service provider, at the time of writing that is Laravel\Cashier\CashierServiceProvider to your config/app.php
If it still doesn't work, go run php artisan config:clear to make sure that the service provider is picked up.
Happy invoicing!
I'm going to resurrect this beast.
I had a similar issue because the service provider was not loaded. If you checkout CashierServiceProvider you'll see it adds the necessary 'namespace' for the 'cashier' prefixed views.
public function boot()
{
$this->loadViewsFrom(__DIR__.'/../../views', 'cashier');
$this->publishes([
__DIR__.'/../../views' => base_path('resources/views/vendor/cashier'),
]);
}
Add Laravel\Cashier\CashierServiceProvider to your config/app.php file and inside the providers key.
For anyone who runs across this like we did.