Skip to main content

Posts

Showing posts from October, 2022

has, whereHas,whereRelation condition on realtionship tables in laravel

 I  will give you very simple example of how to use wherehas condition with laravel eloquent relationship. you can also use with laravel 6, laravel 7, laravel 8 and laravel 9 version. Sometime we need to add where condition with relation table then you need to use wherehas() method. for example if you have users with country relation then you want to filter with country then you must have to use whereHas(). so here i will give you two way to add condition with relation model, one using whereHas and second using whereRelation().

What is the use of bootstrap folder in laravel ?

 This folder don't have concern with bootstrap framework. This folder belongs to laravel framework. First, understand the meaning of the term "Bootstrapping". It is the process of self start processing in the framework or in any system. These file automatically load the classes ans start out project. Let's understand which classes automatically load up, run, and boot up. Service Container, Facades, Middleware, Service Provider and Kernal. these are dependencies are set up so that laravel fulfill your request to the application.

Touch Many-to-Many Parent and sync methods in laravel

  Touch method 1) Most of the time user update data in the child table but also wants to update the user (parent) table updated_at field. For that, you don't need to update it manually by querying it. Use the touch method on the model object inside controller. This method will only update the updated_at field to the current date/time. $user -> touch (); 2) You could also update field automatically, lets say where there is new comment on post and you also want to update post table updated_at feild at the same time than what you need to do Inside Comment Model define belongtomany relationship to post model class and also specify a protected variable protected $touches = [ 'post' ]; //post is realtional method Sync() Method This method is use to add multiple id's of data into pivot table with many to many relationship. This method remove existing id's and add new id's. If you want existing id's remian in the pivot table than you will use this code. $book ...

Upload on GitHub

  Commands to upload project changes on github repos.... …or create a new repository on the command line echo "# react_native_help" >> README.md git init git add README.md git commit -m "first commit" git branch -M main git remote add origin https://github.com/adnandevinlara/react_native_help.git git push -u origin main …or push an existing repository from the command line git remote add origin https://github.com/adnandevinlara/react_native_help.git git branch -M main git push -u origin main my -r-n-h-t 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                                              Git Branch 1. Check the active/selected branch -git branch 2. Create new branch in repo -git branch newBranchName 3. List all branches -git branch 4. Select/Switch to newly created branch -git checko...

Collection methods examples in laravel

 Collection Method use to modfy the data or get data in our required format <?php namespace App\Http\Controllers ; use Illuminate\Http\ Request ; use Illuminate\Support\ Str ; class CollectionController extends Controller {     const PROJECT_TYPES = [         'action' ,         'base resource' ,         'A W S' ,         'lara beans'     ];     const MOVIES_TYPES = [         [ 'id' => 1 , 'name' => 'action' ],         [ 'id' => 2 , 'name' => 'base resource' ],         [ 'id' => 3 , 'name' => 'A W S' ],         [ 'id' => 4 , 'name' => 'lara beans' ]     ];     function mapWithSlugs () {         dump ( 'Convert name of arrays into slug using Slug function and iterate array with map function' );        ...

Guards, Gates and Policies code logic

  Guards Gates Gates are closure based means it can be access from throughout of our application. We define our Gates inside AuthserviceProvider under boot method. We can define many gates as we needed. we define gate for both roles and permissions. Now use Gate inside controller method, where you want to authorize user action. Policies In the policies, we map policy class to the specific model (Eluquant model). With the help of command we create policyClass like so. php artisan make:policy PostPolicy --model=Post  Define policy in the controller for a particular action. Define policy check  inside policy class view method. here we are checking is user_id is equal to the post user_id if check success  than it will return true and user can do this task. Authorization function in laravel But Laravel enables us to write policy to manage every policy check instead if writing checks in each method. For that, we will use laravel Authorize function. let make it make copy of...

Rest full api with well organised CRUD operation

What are types of RESTapi? RESTapi work with HTTP protocols. REST and SOAP api's are most popular api's and most use in the projects. So we will cover REST api in this blog.  What  is REST API? It is Representational State Transfer Application programming Interface. It support many formats like: JSON, TEXT, XML, user-defined. SOAP api only support XML format. Before using/hitting API request to server we have to tell which request method this API has. With HTTP request method, we can know the nature of api call/request of data.  There are many method RESTapi supports, these are. Note: Many develper called REST api as a RESTful api. Than we have to pass additional information with api called header. There are many header information types but important part is 1. header('Content-Type: application\json'); This header provide imformation about the type content we will get in api response 2. header('Access-Control-Allow-Methods:PUT'); This header use to tell what ty...

Sockets

 Traditional socket

Realtionship in laravel with pivot table

Create Many2many relationship with pivot table   1. Create Factory with the specific model Using this query we can create factory class for specific model. php artisan make:factory ProjectFactory --model=Project 2. Create Seeder class Using this query we can create seeder class. php artisan make:seeder ProjectSeeder Write below code in this class in a run method in ProjectSeeder class public function run () {         factory ( Project :: class , 10 )-> create ();         foreach ( Project :: all () as $key => $project ) {             $users = User :: inRandomOrder ()-> take ( rand ( 1 , 3 ))-> pluck ( 'id' );             $project -> users ()-> attach ( $users );         } } 3. Create a relationship to the User model using Project Model. Inside Project Model public function users (){         return $this -> belongsTo...