' I am new in Laravel and I am trying to send notification but it shows an error (Call to a member function notify() on string)'
$discussion->user['email']->notify(new newReplyAdded($discussion));
You need to use notify() on a model, not on a string. Like below
$user = User::find(1);
$user->notify(..)
And you have to use Notifiable trait in you model like
use Illuminate\Notifications\Notifiable;
class User extends Model
{
use Notifiable;
..........
}
In your case, you can try this
$discussion->user->notify(new newReplyAdded($discussion));
Related
I have a problem with Laravel notifications. I try to give a user notification about something, but Laravel cannot find notification class.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class NotificationController extends Controller
{
public function getNot(Request $request)
{
$user = Auth::user();
$user->notify(new NewPost('a'));
}
}
I've also created a notification with the name NewPost.php, the problem is:
Class 'App\Http\Controllers\NewPost' not found
this one, so in the User model already included Notifications and notifiable.
Add use statement before class definition.
use Illuminate\Http\Request;
use Auth;
use App\Notifications\NewPost;
I assume that you create notification by artisan, if no, then keep in mind that namespace could be different.
I am creating an api for student management system in laravel. I want to send email to the students when faculty enters their record in the database saying that their admission is successful. How can I do that?
Notifications!
In the method where you insert the record, you want to notify some users.
Final result would look like this:
use App\Notifications\StudentAdmitted;
...
public function doSomething(Request $request, Student $student)
{
$student->admitted = true;
$student->notify(new StudentAdmitted);
}
Create the notification
php artisan make:notification StudentAdmitted
Edit it
You want to modify the toMail method
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Congratulations!')
->line('you have been accepted')
->action('View', url('/url/to/page'))
->line('something here');
}
Moreover, the Student or User model should use the Notifiable trait.
...
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
....
I want use spatie to get Google+ avatar, but when I try get it there is an error.
$user = Socialite::driver('google')->user();
$usertest=User::whereEmail($user->getemail())->first();
if(! $usertest){
$usertest=User::create([
'name'=>$user->name,
'email'=>$user->email,
'password'=>bcrypt($user->id)
]);}
$usertest->addMediaFromUrl($user->avatar)->toMediaCollection('avatar');
auth()->loginUsingId($usertest->id);
return redirect('/');
error:
Type error: Argument 1 passed to Spatie\MediaLibrary\FileAdder\FileAdder::processMediaItem() must be an instance of Spatie\MediaLibrary\HasMedia\HasMedia, instance of App\User given,
It looks like you have not added HasMedia interface and HasMediaTrait to User class:
class User extends Authenticatable implements HasMedia {
use HasMediaTrait;
// ...
}
I'm trying to add notification system to my Laravel project. I' watched this video to understand the system : https://www.youtube.com/watch?v=iDDUxqpNgSc
Notification table, model et controller are created. Also, i have created the view with Vue.JS and Pusher. It's work well !
However, in the notification controller, when i try to user Auth::user() method it's return null. I read somewhere it's because the middleware 'auth' is not already load when the controller is.
This is my NotificationsController file :
<?php
namespace App\Http\Controllers;
use App\Notification;
use Illuminate\Http\Request;
use App\Idea;
use App\User;
use Illuminate\Support\Facades\Auth;
class NotificationsController extends Controller
{
public function __construct()
{
}
function get(){
$notification = Auth::user()->unreadNotifications()->get();
return $notification;
}
function read(Request $request){
Auth::user()->unreadNotifications()->find($request->id)->markAsRead();
}
}
Do you have any idea how to solve this ?
Thank's for your time !
The answer was not about the Auth::user (It's accessible). I just baldy defined the notifiable_type in my model. It was App\Idea, it should be App\User
I am using laravel eloquent model so there are three tables tempsocials, tempusers and tempdevices. so one user can have multiple devices and multiple social acounts.
I created a models for three of above table and trying to maintain relationship in between like following
This is my Tempuser model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tempuser extends Model
{
public function tempsocials(){
return $this->hasMany('App\Tempsocial');
}
public function tempdevices(){
return $this->hasMany('App\Tempdevice');
}
}
This is my Tempdevice model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tempdevice extends Model
{
public function tempusers(){
return $this->belongsTo('App\Tempuser');
}
}
And this one is last Tempsocial model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tempsocial extends Model
{
public function tempusers(){
return $this->belongsTo('App\Tempuser');
}
}
Now this is my controller where i want to retrive all the devices and social accounts of particular user
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Collection;
use App\Http\Requests;
use App\tempLogin;
use App\Tempdevice;
use App\Tempuser;
use App\Tempsocial;
class loginController extends Controller
{
public function check_credentials(Request $request){
$count=0;
if($request->header('content-type')=='application/json'){
$temp=new Tempuser;
$devices = $temp->tempdevices();
return $devices;
}
}
}
But i got following error:
Object of class Illuminate\Database\Eloquent\Relations\HasMany could
not be converted to string
You're making a new Tempuser(), it has no id, so your relation returns nothing as expected, you will need to pass an id to the method and with that id you could do something like:
Tempuser::find($id);
This will then return an actual model instead of creating a new one.
Also because when you call ->tempdevices() as a function it will return a query builder, instead when you do ->tempdevices it will return a collection, change it like this:
$devices = $temp->tempdevices;
Also, if you expect a json response (if thats not the case you can ignore this part), it might be better to also state it in your return by doing:
return response()->json($devices);