I'm trying to develop a simple freelancing platform with laravel breeze. When i tried to implement a massaging feature between freelancer and admin-(jobs are posted by admin not clients) im getting this error.
Illuminate\Database\Eloquent\Relations\BelongsToMany::save(): Argument #1 ($model) must be of type Illuminate\Database\Eloquent\Model, null given, called in C:\Users\user\freelance_graphicwebefef\app\Http\Controllers\ThreadController.php on line 23
Thread Model
class Thread extends Model
{
use HasFactory;
use SoftDeletes;
public function Users(){
return $this->belongsToMany('App\Models\User');
}
public function Messages(){
return $this->hasMany('App\Models\Message','thread_id');
}
public function Job(){
return $this->belongsTo('App\Models\Job');
}
}
User Model
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Laratrust\Traits\LaratrustUserTrait;
class User extends Authenticatable
{
use LaratrustUserTrait;
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function role(){
return $this->belongsTo('App\Models\Role');
}
public function hasRoleCustom($Role){
return null !== $this->role()->where('name',$Role)->first();
}
public function jobs(){
return $this->hasMany('App\Models\Job','user_id');
}
public function Favorites(){
return $this->hasMany('App\Models\Favorites');
}
public function Threads(){
return $this->belongsToMany('App\Models\Thread');
}
public function Message(){
return $this->hasMany('App\Models\Message','user_id');
}
}
ThreadController
class ThreadController extends Controller
{
public function createThread(Request $request){
$this->validate($request,[
'job_id'=>'required|integer'
]);
$Thread=new Thread;
$Thread->job_id=$request->input('job_id');
$Thread->save();
$Freelancer=Auth::user();
$Admin=$Thread->Job->user;
$Thread->Users()->save($Freelancer);//**line 22**
$Thread->Users()->save($Admin);//**line 23**
return redirect('/threads/'.$Thread->id);
}
public function createMessage(Request $request,$id){
$this->validate($request,[
'message'=>'required|string',
]);
$Message=new Message();
$Message->message=$request->input('message');
$Message->thread_id=$id;
$User=Auth::user();
$User->Message()->save($Message);
return redirect('/threads/'.$id)->with('success','Message Saved');
}
public function showThread($id){
$Thread=Thread::find($id);
return view('threads.show')->with('Thread',$Thread);
}
}
PHPStorm gives me this message on line 22
$Thread->Users()->save($Freelancer);
Expected parameter of type '\Illuminate\Database\Eloquent\Model', '\Illuminate\Contracts\Auth\Authenticatable' provided
Thread Table and thread_users table show some records.