Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

satriamuda's avatar

Notifications

Illuminate\Database\Eloquent\Model::__construct(): Argument #1 ($attributes) must be of type array, int given, called in C:\xampp\htdocs\luma\app\Http\Controllers\HubungiController.php on line 62

controller :

<?php

namespace App\Http\Controllers;

use auth;
use App\Models\Logo;
use App\Models\User;
use App\Models\Contact;
use App\Models\Logoweb;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
use App\Notifications\Contact as NotificationsContact;

class HubungiController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home.contact',[
            'logo' => Logo::paginate(1),
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('home.contact',[
            'contact' => new Contact(),
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'nama' => 'required'
        ]);

        $contact = Contact::create([
            'nama'=> $request->nama,
            'subject'=> $request->subject,
            'pesan'=> $request->pesan,
            'email'=> $request->email,
            'hubungi'=> $request->hubungi
            
        ]);

        $user = User::where('id','!=',auth()->user()->id)->get();
        $user_create = auth()->user()->name;
        Notification::send($user,new contact($contact->id,$user_create));





        return redirect(url('hubungi'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

App\Notifications

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class Contact extends Notification
{
    use Queueable;

    private $contact_id;
    private $user_created;
    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($contact_id,$user_created)
    {
        $this->contact_id = $contact_id;
        $this->user_created = $user_created;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database'];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \Illuminate\Notifications\Messages\MailMessage
     */
    public function toMail($notifiable)
    {
        return (new MailMessage)
                    ->line('The introduction to the notification.')
                    ->action('Notification Action', url('/'))
                    ->line('Thank you for using our application!');
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
           
            'contact_id' =>  $this->contact_id,
            'user_created' =>$this->user_created,

        ];
    }
}
0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It looks like the issue is with the line Notification::send($user,new contact($contact->id,$user_create)); in your controller. The contact class is not being passed as an array, but as an integer.

You should change the line to Notification::send($user,new contact([$contact->id,$user_create])); to pass the class as an array.

webrobert's avatar

a couple things...

public function store(Request $request)
{
    ...

    // you already have the user.  auth()->user()
    // so there is no need to look them up, again.
    $user = User::where('id','!=',auth()->user()->id)->get();

    // you already have the users name so there is no need to pass it into the notification.
    $user_create = auth()->user()->name;
    Notification::send($user,new contact($contact->id,$user_create));

    return redirect(url('hubungi'));
}

so this method becomes...

public function store(Request $request)
{
    $this->validate($request, [
        'nama' => 'required'
    ]);

    $contact = Contact::create([
        'nama'=> $request->nama,
        'subject'=> $request->subject,
        'pesan'=> $request->pesan,
        'email'=> $request->email,
        'hubungi'=> $request->hubungi

    ]);

    auth()->user()->notify(new contact($contact->id))
 
    return redirect(url('hubungi'));
}

then in your notification...

class Contact extends Notification
{
    use Queueable;

    private $contact_id;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct($contact_id)
    {
        $this->contact_id = $contact_id;
    }

	...

    public function toArray($notifiable)
    {
        return [
            'contact_id' =>  $this->contact_id,
            'user_created' => $notifiable->name,
        ];
    }
}

Please or to participate in this conversation.