Search code examples
laravelvoyager

how to create view in voyager :::not connected to model


I need to create a view in laravel admin voyager to send notifications using firebase. so the question is how to create that view and how to create a custom link in admin menu. I tried to do that using laravel-admin using forms this is the code I used in laravel-admin

<?php

namespace App\Admin\Forms;

use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;

class sendnot extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $req)
    {
        //dump($request->all());
        $url = 'https://fcm.googleapis.com/fcm/send';
        $dataArr = array('click_action' => 'FLUTTER_NOTIFICATION_CLICK', 'id' => $req->id,'status'=>"done");
        $notification = array('title' =>$req->title, 'text' => $req->message, 'sound' => 'default', 'badge' => '1',);
        $arrayToSend = array('to' => "/topics/all", 'notification' => $notification, 'data' => $dataArr, 'priority'=>'high');
        $fields = json_encode ($arrayToSend);
        $headers = array (
            'Authorization: key=' . "XXXXXX",
            'Content-Type: application/json'
        );
   
        $ch = curl_init ();
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, true );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields );
   
        $result = curl_exec ( $ch );
        //var_dump($result);
        curl_close ( $ch );
        admin_success('Processed successfully.'. $result);

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->text('title')->rules('required');
        $this->textarea('message')->rules('required');
        
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        return [
            'title'       => '',
            'message'      => '',
            
        ];
    }
}

Then I tried to do the same in voyager but failed...

I hope my explanation helps


Solution

  • As I understand from your question that you are trying to add a new view in Voyager that belongs to admin but not connected to Model, you can do this by the following steps:

    1. Add your route in your web.php inside the admin routes group like:
    Route::group(['prefix' => 'admin'], function () {
        Route::get('notifications', function(){
            return view('notifications');
        });
        Voyager::routes();
    });
    
    1. create a new view for example here I called notifications that extends voyager master layout like:
    @extends('voyager::master')
    
    @section('css')
        <style></style>
    @stop
    
    @section('page_title', 'Your title')
    
    @section('page_header')
        <h1 class="page-title">
            <i class="fa fa-home"></i>
            Test
        </h1>
        @include('voyager::multilingual.language-selector')
    @stop
    
    @section('content')
    <div class="page-content container-fluid">
        <h1>Your content</h1>
    </div>
    @stop
    
    1. Finally, add what you want in content section and you can access this page by navigating: {url}/admin/notifications