i am using spatie permissions but it returns an error
There is no permission named `create` for guard `web`.
i don't know why please help
my code what am i doing is i create a new user then i create permissions then i make the user instructor have the permissions i create, all of that is in the seeder
here is my code seeder
public function run()
{
$user = User::create([
'name' => 'admin',
'email' => 'instructor#gmail.com',
'email_verified_at' => now(),
'password' => 'password', // password
'remember_token' => Str::random(10),
'type' => User::TYPE_INSTRUCTOR,
]);
$permissions =
[
[
'name' => 'methods_create',
'display_name' => 'create',
'key' => 'methods',
],
[
'name' => 'methods_update',
'display_name' => 'update',
'key' => 'methods',
],
[
'name' => 'methods_delete',
'display_name' => 'delete',
'key' => 'methods',
],
];
$role = Role::create(['name'=>'Instructor']);
foreach($permissions as $permission){
Permission::create($permission);
}
$role->givePermissionTo($permissions);
$user->assignRole($role);
}
In my users table i have additional column that stores the information to which specific scope user belongs and for our application needs a different scope can have same email and password.
My problem is that when i call /oauth/token URL i can get a token for a wrong user if there are 2 rows with same email and password.
Is there an way to call that method and passing additional custom field?
For example, if i have:
$response = $http->request('POST', env('APP_URL') . '/api/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $oClient->id,
'client_secret' => $oClient->secret,
'username' => $request->email,
'password' => $request->password,
'scope' => 'business',
],
]);
is there any way to add additional parameter in there like
$response = $http->request('POST', env('APP_URL') . '/api/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => $oClient->id,
'client_secret' => $oClient->secret,
'username' => $request->email,
'password' => $request->password,
'user_role' => 'example_role'
'scope' => 'business',
],
]);
In my Laravel 7.6 app I use sendgrid for email sending with code in control like :
\Mail
::to($newContactUs->author_email)->
send(new SendgridMail('emails/contact_us_was_sent', $newContactUs->author_email, '', $subject, $additiveVars, $attachFiles));
with class in app/Mail/SendgridMail.php :
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Sichikawa\LaravelSendgridDriver\SendGrid;
use App\Settings;
use App\Http\Traits\funcsTrait;
class SendgridMail extends Mailable
{
use Queueable, SerializesModels;
use SendGrid;
use funcsTrait;
private $m_view_name;
private $m_to;
private $m_cc;
private $m_subject;
private $m_additiveVars;
private $m_attachFiles;
public function __construct( $view_name, $to= [], $cc= '', $subject= '', $additiveVars= [], $attachFiles= [] )
{
$this->m_view_name= $view_name;
$this->m_to= $to;
$this->m_cc= $cc;
$this->m_subject= $subject;
$all_emails_copy = \Config::get('app.all_emails_copy');
if ( empty($this->m_cc) and !empty($all_emails_copy)) {
$this->m_cc= $all_emails_copy;
}
$additiveVars['site_home_url'] = \URL::to('/');
$additiveVars['site_name'] = Settings::getValue('site_name');
$additiveVars['noreply_email'] = Settings::getValue('noreply_email');
$additiveVars['support_signature'] = Settings::getValue('support_signature');
$additiveVars['medium_slogan_img_url'] = config('app.url').config('app.medium_slogan_img_url');
$this->m_additiveVars= $additiveVars;
$this->m_attachFiles= $attachFiles;
}
/**
* Build the message.
*
* #return $this
*/
public function build( )
{
$mailObject= $this
->view( $this->m_view_name)
->subject($this->m_subject)
->to([$this->m_to])
->cc([$this->m_cc])
->with( $this->m_additiveVars )
->sendgrid( $this->m_additiveVars );
foreach( $this->m_attachFiles as $next_attach_file) {
if ( file_exists($next_attach_file) ) {
$mailObject->attach($next_attach_file);
}
}
return $mailObject;
}
}
and template resources/views/emails/contact_us_was_sent.blade.php:
...
<div class="wrapper">
#inject('viewFuncs', 'App\library\viewFuncs')
<h4 class="email_title">
Hello, {!! $to_user_name !!} !
</h4>
...
#include( 'emails.app_footer')
#include( 'emails.emails_style')
</div>
and it works for me now, but now with "Multiple Mail Drivers" I added mailtrap to my .env :
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=NNNNNNNN
MAIL_PASSWORD=NNNNNNNN
and I want to use mailtrap while testing the app using the same
template resources/views/emails/contact_us_was_sent.blade.php
and switching from mailtrap to sendgrid as easy as possible.
I tried something like :
\Mail::mailer('smtp')
->to($newContactUs->author_email)
->send( \Mail('emails/contact_us_was_sent', $newContactUs->author_email, '', $subject, $additiveVars, $attachFiles) );
But got error as \Mail does not support templates.
Are there something to use support templates for mail Method? Some wrapper?
Updated:
Priorly I worked with sendgrid and for this in file config/mail.php I
wrote all sendgrid parameters.
Now I want to write 2 emeil servers and fi=or this in .env I wrote:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=mailtrip_id
MAIL_PASSWORD=mailtrip_password
SENDGRID_HOST=smtp.sendgrid.net
SENDGRID_PORT=587
SENDGRID_ENCRYPTION=tls
SENDGRID_USERNAME=sendgrid_user
SENDGRID_PASSWORD=sendgrid_user_password
and I remade config/mail.php (I got a sample from):
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello#example.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
'log_channel' => env('MAIL_LOG_CHANNEL'),
'mailers' => [
'smtp' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'sendgrid' => [
'transport' => 'sendgrid',
'host' => env('SENDGRID_HOST', 'smtp.sendgrid.net'),
'port' => env('SENDGRID_PORT', 587),
'encryption' => env('SENDGRID_ENCRYPTION', 'tls'),
'username' => env('SENDGRID_USERNAME'),
'password' => env('SENDGRID_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'ses' => [
'transport' => 'ses',
],
'mailgun' => [
'transport' => 'mailgun',
],
'postmark' => [
'transport' => 'postmark',
],
'sendmail' => [
'transport' => 'sendmail',
'path' => '/usr/sbin/sendmail -bs',
],
'log' => [
'transport' => 'log',
'channel' => env('MAIL_LOG_CHANNEL'),
],
'array' => [
'transport' => 'array',
],
],
];
I am not sure that this config file is valid? Are mail config parameters are read from mailers array ?
Looks like default(mailtrip) mail config is used always. Is it invalid format ?
In my control Ido:
$email_mode= 'live';
// $email_mode= 'debug';
if( $email_mode== 'debug' ) {
\Log::info( '-10 Send to mailtrap ::' );
\Mail
::mailer('smtp')
->to('myemail#yahoo.com') // DEBUG
->send(new TestEmail); //
\Log::info( '-10 Send to mailtrap AFTER::' );
}
// sendgrid
if( $email_mode== 'live') {
\Log::info( '-11 Send to sendgrid ::' );
\Mail
::mailer('sendgrid')
->to('myemail#yahoo.com') // DEBUG
->send(new SendgridMail('emails/contact_us_was_sent', $newContactUs->author_email, '', $subject, $additiveVars, $attachFiles));
\Log::info( '-11 Send to sendgrid AFTER::' );
}
I check in logs that live flow is run but anyway I got email at mailtrap.
Thanks!
The short answer is you are using \Mail() which is a native php function
see: https://www.php.net/manual/en/function.mail.php and try to embed that in a laravel Mailer.
This is not how you should use it, this is really not advised.
Some more detail:
As you write mailables, already you should consider, the Mailable should not define the driver, so it's not encouraged to name it SendGridMailA.. see it as MailA send with Mail::mailer($mailDriver)
See:https://laravel.com/docs/7.x/mail#writing-mailables
Long answer, see this video, explains how to implement multiple mail drivers:
https://www.youtube.com/watch?v=HCONO0cwsoI
However it looks like you using it as a debug method. This is not why multiple mail drivers where introduced in laravel 7. It's more like you should use a different driver for bulk mails for example and for password resets...
That makes sense..
For debuging, it more usefull to make the default driver, depend on the APP_DEBUG configuration for example, or introduce an own ENV var, to toggle your production app in debug mode...
I found decision with modifying config/mail.php :
<?php
return [
'default' => env('MAIL_MAILER', 'sendgrid'),
'mailers' => [
'mailtrap' => [
'transport' => 'smtp',
'host' => env('MAIL_HOST'),
'port' => env('MAIL_PORT'),
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
'sendgrid' => [
'transport' => 'smtp',
'host' => env('SENDGRID_HOST', 'smtp.sendgrid.net'),
'port' => env('SENDGRID_PORT', 587),
'encryption' => env('SENDGRID_ENCRYPTION', 'tls'),
'username' => env('SENDGRID_USERNAME'),
'password' => env('SENDGRID_PASSWORD'),
'timeout' => null,
'auth_mode' => null,
],
and having 2 groups 'mailtrap' and 'sendgrid' in .env I wrote in control :
\Mail
::mailer(true ? 'sendgrid' : 'mailtrap')
->to($newContactUs->author_email)
->send(new SendgridMail(
'emails/contact_us_was_sent',
$newContactUs->author_email, '', $subject,
$additiveVars,
$attachFiles)
);
I have configured two database connections:
'sqlsrv' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST', '####'),
'port' => env('DB_PORT', '####'),
'database' => env('DB_DATABASE', '####'),
'username' => env('DB_USERNAME', '####'),
'password' => env('DB_PASSWORD', '####'),
'charset' => 'utf8',
'prefix' => 'pref1_',
'prefix_indexes' => true,
],
'sqlsrv1' => [
'driver' => 'sqlsrv',
'host' => env('DB_HOST1', '####'),
'port' => env('DB_PORT1', '####'),
'database' => env('DB_DATABASE1', '####'),
'username' => env('DB_USERNAME1', '####'),
'password' => env('DB_PASSWORD1', '####'),
'charset' => 'utf8',
'prefix' => '',
],
With the normal user model and 2 custom models:
Synergy:
class Synergy extends Model
{
protected $connection = 'sqlsrv1';
protected $table = 'humres';
protected $primaryKey = 'res_id';
public $timestamps = false;
public function user(){
return $this->belongsTo('App\User', 'employee_id', 'res_id');
}
PushNotification:
class PushNotification extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
I want to check if the user has a permission for push notification and if another column is set on true.
I try it with this query:
$users = User::with('PushNotification')->with('Synergy')
->whereHas('PushNotification', function($q){
$q->where('type', 'news')->where('active', 1);
})
->whereHas('Synergy', function($q1){
$q1->where('freefield20', 1);
})
->get();
return $users;
It throws the following error:
SQLSTATE[42S02]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name 'pref1_humres'. (SQL: select * from [pref1_users] where exists (select * from [pref1_push_notifications] where [pref1_users].[id] = [pref1_push_notifications].[user_id] and [type] = news and [active] = 1) and (exists (select * from [pref1_humres] where [pref1_users].[employee_id] = [pref1_humres].[res_id] and [freefield20] = 1)))
Why does it use the prefix of sqlsrv while with sqlsrv1 no prefix is entered?
Any ideas how to build this query correctly?
I created a form that when I submit I get an email. I've used mailtrap and gmail and both of them worked, but when I tried to use my own server email I don't get the email. I know that the code works because I've used it before.
I don't know if this played a roll in why it isn't working anymore but I had to reinstall windows on my laptop and reinstall xampp. I also had to change my password that I was using for my server email.
My .env
MAIL_DRIVER=smtp
MAIL_HOST=lin01.hkdns.co.za
MAIL_PORT=587
MAIL_USERNAME=info#myserver.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=
my mail.php
<?php
return [
'driver' => env('MAIL_DRIVER', 'smtp'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'info#myserver.com'),
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('MAIL_USERNAME'),
'password' => env('MAIL_PASSWORD'),
'sendmail' => '/usr/sbin/sendmail -bs',
'markdown' => [
'theme' => 'default',
'paths' => [
resource_path('views/vendor/mail'),
],
],
];
My UsersController store method
public function store()
{
$users = new User();
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if($validation->fails()){
return redirect()->route('admin.users.create')
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors');
}
if($validation->passes()){
$users->name = Input::get('name');
$users->email = Input::get('email');
$users->image = Input::get('image');
$password = $users->generatePassword();
$users->password = bcrypt($password);
$data = array(
'name' => Input::get('name'),
'email' => Input::get('email'),
'password' => $password,
);
Mail::send('templates::emails.login', $data, function($message){
$message->to(Input::get('email'), Input::get('name'))->subject('Your login details');
});
$users->save();
$users = User::all();
return view('users::admin.index', compact('users'));
}
}
Also no errors show up in my storage/logs