Categories
Programming Technology Web Development

Pagination in laravel relation

public function get_shop_products($shop_id)
{
$shop = Shop::query()
->where(‘id’ , $shop_id)
->first();
$shop->setRelation(‘products’ , $shop->products()->paginate(10));
return new ShopProductsResource($shop);
}

Masoud Hosseini

Categories
Programming Technology Useful Addresses Web Development

I love Frappe Framework

In this post, I want to write about a miracle in the software development world.

Imagine you can create your database entities with some clicks; you have many types of fields that are built in that software.

For a programmer who codes in pure PHP or python with a front-end framework, it takes more than 1 month to create a blog and shop website, but you can have all of that in one or two days with Frappe; Wow 🙂 What amazing!

You can read more about it on its official website: frappeframework.com

I also created a GitHub repository to keep some sample script codes that during working with frappe you probably need to use.

You can have it here (don’t forget to give it a start and communicate):

https://github.com/masoud-me/frappe-sample-scripts

Categories
Programming Technology

Masoud’s notes about Golang part 4 – Golang Commands

Execute project

go run .

Create an executable file of the project (build)

go build .

Download and install external package

go get {package path}

#### eg.
go get gorm.io/gorm

Display Current GOPATH

go env GOPATH

install new go package

go install test-app

Update package

go get -u test-app

List Packages Alongside Dependencies

go list
go list ./...

Display Version Information

go version

Maintain Module Dependencies

The go mod tidy command allows us to add missing dependencies or delete unused modules. This go command helps in maintaining module dependencies and keep your applications as lean as possible. Simply run the following simple command to do this.

Make sure to run this from your project’s root directory. You can check the go.mod file to see which dependencies were added or deleted. The -v flag, if used, causes tidy to display which modules were removed to the standard error.

go mod tidy [-v]

Verify Module Dependencies

go mod verify

Display Why Packages/Modules are Needed

You can see why certain modules or packages are required in your application. This is useful if you are working with someone else’s code or are trying to figure out what certain things do in a specific project. The “why” command of the mod tool allows us to do this

The first one is the general syntax, and the second one is an example. It prints out why the language and encoding packages are required in your application.

go mod why [-m] [-vendor] packages...
go mod why golang.org/x/text/language golang.org/x/text/encoding

Categories
Programming Technology

Masoud’s notes about Golang – Part 2

when you want to run or build a go file it needs to be the main package (I’ll write more about packages in go later).

let’s div into example code

package main
import "fmt"

func main(){

	for i := 0 ; i <= 500 ; i++{
		s := ""
		
		if i % 7 == 0{
			s = "Haftaeiash"
		}
		if i % 3 == 0{
			s = "Setayi"
		}
		if s != "" {
			fmt.Println(s)
		}else{
			fmt.Println(i)
		}
	}
}

To run package :

go run masoud.go

To check out environments :

go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/masoud/.cache/go-build"
GOENV="/home/masoud/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/masoud/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/masoud/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18.3"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3281051355=/tmp/go-build -gno-record-gcc-switches"

By default it prints the list as a shell script; however, if one or more variable names are given as arguments, it prints the value of each named variable.

$go env GOOS GOPATH
linux
/home/yourname
Categories
Best practice devops Programming Technology Web Development

Implement Rest APIs more efficient

Most web applications are implemented based on Rest Full APIs and as I researched huge part of them are not implemented based on best practices and clean code principles, so I decided to collect some points in a couple of posts to help make them more efficient APIs and Applications.

Let’s Look at other kinds of APIs

you can easily research and find out more about all of these kine of API design methodologies but here I’m going to just collect best part of them to create a document to implement Rest API in best practice.

Graphql

the best part of graphql is that you can design your API for the front end to just get whatever need, not more.

it can bring us better speed because less amount of packets is transferring over the network.

gRPC

Socket

Categories
Programming Technology

Masoud’s notes about Golang part 1

In this series article, I want to note some tips during watching Golang courses that are important for me.

Overall points :

  • golang is a compiled programming language
  • like as c and c++ it needs a main function
  • all built in functions are not available like as php and you need to import situble library
  • you cant define unused variable , function or even import unused library.
  • to print we need to import “fmt” library
  • it needs to specify variable type on definitions

some usefull commands:

go mod init mymodule

go run main.go

Golang Data types list

Masoud Hosseini | golang data types

Array and slice in golang

#array : 
var booking [50]string
or 
var booking = [50]string{"masoud"}

#slice 
var booking []string
or istead of "var" we can use ":="
booking :=[]string

add an index to a slice

append(booking, firstName + "- "+ lastName)

loop in Golang

we have just “for” loop in golang

to ignore a variable you dont want to use you can use “unser line” => “_”

so with Go you need to make unused variables explicit

for example :

firstNames := []string{}
		for _, bookingItem := range booking {
			var names = strings.Fields(bookingItem)
			firstNames = append(firstNames, names[0])
		}
Categories
Programming Technology Web Development

Using Types in PHP is one of the least used features

Using Types in PHP is one of the least used features, but very powerful features available in PHP. This is a feature that can save you and other developers a whole lot of stress (if you work with a team).

Of course, you can write function descriptions, but it becomes quite a daunting task to write function descriptions for all your functions and variables in a large project.

When someone else wants to use your function or even you want to use it after a couple of weeks later, it needs to check the function’s body to recognize which types you should pass to the function while using, if you don’t specify arguments types.

Ex:

function getItem(array $item) {
return allItems()[$item[0]];
}

This will make sure that whatever is passed in here is the type needed. You can read more from www.php.net

For more tips:
https://lnkd.in/eWgvUVKS