Adsense

Popular Posts

Showing posts with label Laravel. Show all posts
Showing posts with label Laravel. Show all posts

Tuesday, February 8, 2022

React/Laravel: design header

We can use views to store header and footer (used in current coding)

resources/views/templates/

In resources/views/templates/view_layout.blade.php

        @include('templates.header_primary_menu')
        @include('templates.header_sub_menu')
we have header_primary_menu.blade.php and  header_sub_menu.blade.php under

/views/templates/

Bootstrap dropdown menu:

https://getbootstrap.com/docs/4.0/components/dropdowns/ 

<div class="dropdown show">
  <a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    Dropdown link
  </a>

  <div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
    <a class="dropdown-item" href="#">Action</a>
    <a class="dropdown-item" href="#">Another action</a>
    <a class="dropdown-item" href="#">Something else here</a>
  </div>
</div>

 data-toggle="collapse" to show/hide text

https://getbootstrap.com/docs/4.0/components/collapse/ 

<p>
  <a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
    Link with href
  </a>
  <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
    Button with data-target
  </button>
</p>
<div class="collapse" id="collapseExample">
  <div class="card card-body">
    Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident.
  </div>
</div>

 

data-toggle dropdown

https://www.w3schools.com/bootstrap/bootstrap_dropdowns.asp

<div class="dropdown">
  <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">Dropdown Example
  <span class="caret"></span></button>
  <ul class="dropdown-menu">
    <li><a href="#">HTML</a></li>
    <li><a href="#">CSS</a></li>
    <li><a href="#">JavaScript</a></li>
  </ul>
</div>

 

The .dropdown class indicates a dropdown menu.

To open the dropdown menu, use a button or a link with a class of .dropdown-toggle and the data-toggle="dropdown" attribute.

The .caret class creates a caret arrow icon (), which indicates that the button is a dropdown.

Add the .dropdown-menu class to a <ul> element to actually build the dropdown menu.

or we can use React

resources/js/components/Header.js

import React, {useRef} from 'react';
import { BrowserRouter as Router, Link, Route } from 'react-router-dom';
import { makeStyles } from '@material-ui/core/styles';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import IconButton from '@material-ui/core/IconButton';
import Button from '@material-ui/core/Button';
import Badge from '@material-ui/core/Badge';
import MenuItem from '@material-ui/core/MenuItem';
import Menu from '@material-ui/core/Menu';
import AccountCircle from '@material-ui/icons/AccountCircle';
import ContactSupportIcon from '@material-ui/icons/ContactSupport';
import NotificationsIcon from '@material-ui/icons/Notifications';
import MoreIcon from '@material-ui/icons/MoreVert';
import { connect } from 'react-redux';


import UserGuide from '../systems/UserGuide';


Laravel: get enum values of a column in MySQL table



    public function get_enum_values($table, $field){
        $enum = [];
        $test=DB::select(DB::raw("show columns from {$table} where field = '{$field}'"));
        if(!empty($test)){
            preg_match('/^enum\((.*)\)$/', $test[0]->Type, $matches);
            foreach( explode(',', $matches[1]) as $value )
            {
                $enum[] = trim( $value, "'" );
            }
        }
        return $enum;
    }

Laravel: Send Email



            $data = array(
                'userName' => $userName,
                'from' => $from,
                'to' => $to,
                'cc' => $cc,
                'bcc' => $bcc,
                'subject' => $subject,
                'content' => nl2br($content)
            );
            array_unique($bcc);
            Mail::send([], $data, function($message) use($data) {
                $message->to($data['to'])->subject($data['subject']);
                if($data['cc'] && is_array($data['cc']) && count($data['cc'])>0){
                    foreach($data['cc'] as $b){
                        $message->cc($b);
                    }
                }
                if($data['bcc'] && is_array($data['bcc']) && count($data['bcc'])>0){
                    foreach($data['bcc'] as $b){
                        $message->bcc($b);
                    }
                }
                $message->from($data['from'], $data['userName']);
                $message->setBody($data['content'],'text/html');
            });

Wednesday, November 24, 2021

Some Examples in Laravel Model



1. get List of years:


    public function getYearList(){
        $years = Application::select('year')->distinct()->orderBy('year')->get();
        $yearList = [];
        if(!empty($years)){
            foreach($years as $y){
                $yearList[] = ['label'=>$y->year, 'value'=>$y->year];
            }
        }
        return $yearList;
    }

2. Select and return an array:

public function getEmailTemplates(){
        return NotificationTemplate::where('status', 'active')->where('trigger_status', '<>', 'Submitted')->get()->toArray();
    }


3.Delete a row:

    public function delLogByAppId($appId){
        FundingLog::where('application_id', $appId)->delete();
    }

4. Use when and with

 Application::with(['funding:type'])

In Application model, there is funding function. In Funding model,  there is a column type

Application::with(['funding:type']) to get all the columns in Application model and type from Funding model

class Application extends Model
{
    use HasFactory;

    protected $table = 'funding_application';
    protected $primaryKey = 'application_id';

    function funding(){
        return $this->belongsTo(Funding::class, 'funding_id', 'funding_id');
    }
}

//when $computingId is true

when($computingId, function($query) use ($computingId) {

Full example:

       $applications = Application::with(['funding:type'])
                                    ->when($computingId, function($query) use ($computingId) {
                                            return $query->where('created_by', '=', $computingId);
                                        })
                                    ->when($fundingArr, function($query) use ($fundingArr) {
                                        return $query->whereIn('funding_id', $fundingArr);
                                    })
                                   ->orderBy('created_at', 'desc')
                                    ->get();


Tuesday, November 23, 2021

Laravel PHP: add Log



Model for table MyLog:

<?php

namespace App\Models\Log;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MyLog extends Model
{
    use HasFactory;

    protected $table = 'MyLog';
    protected $primaryKey = 'id';
}

?>

In another Log_model

<?php

namespace App\Models\Log;

use Illuminate\Foundation\Auth\User as Authenticatable;

use App\Models\Eloquent\Log\MyLog;
 

class Log_model  extends Authenticatable
{
    public function addLog($appId, $desc){
        $log = new MyLog;
        $log->application_id = $appId;
        $log->modified_by = cas()->user();
        $log->modified_at = date("Y-m-d H:i:s");
        $log->description = $desc;
        $log->timestamps = false;
        $log->save();
        return $log->id;
    }

?>