CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

12 December, 2020

Multiple where laravel in Codefunda

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 




// Multiple where laravel in Codefunda

// laravel where multiple conditions
$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])


laravel where multiple conditions on single colmn
// here, i have used two different where condition on a single column
$data = Users::where('type',1)
        ->where(function($query) {
            return $query->whereDate('updated_at','!=', Carbon::today())
            ->orWhere('updated_at',null);
         })
         ->get();

         
www.Codefunda.tk

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Multiple where laravel in Codefunda

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 




// Multiple where laravel in Codefunda

// laravel where multiple conditions
$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])


laravel where multiple conditions on single colmn
// here, i have used two different where condition on a single column
$data = Users::where('type',1)
        ->where(function($query) {
            return $query->whereDate('updated_at','!=', Carbon::today())
            ->orWhere('updated_at',null);
         })
         ->get();

         
www.Codefunda.tk

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Multiple orwhere condition in laravel in Codefunda

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 




// Multiple orwhere condition in laravel in Codefunda

// multiple orwhere condition in laravel

public function index()
    {
        $search = "Har";
  
        $users = User::select("*")->where('status', 1)
                  ->where(function($query) use ($search){
                   $query->where('first_name', 'LIKE', '%'.$search.'%')
                   ->orWhere('last_name', 'LIKE', '%'.$search.'%')
                   ->orWhere('email', 'LIKE', '%'.$search.'%');
                    })->get();
  
        dd($users);
    }

# With Join
  public function index()
    {
        $search = "Har";
  
        $users = User::select("*")->where('status', 1)
                  ->where(function($query) use ($search){
                   $query->where('first_name', 'LIKE', '%'.$search.'%')
                   ->orWhere('last_name', 'LIKE', '%'.$search.'%')
                   ->orWhere('email', 'LIKE', '%'.$search.'%');
                    })
          ->join('users_roles', 'users.id', '=', 'users_roles.user_id')
          ->where('users_roles.role_id', '=', Role::USER_PARTICIPANT)->get();
  
        dd($users);
    }


    // laravel where multiple conditions
    $query->where([
        ['column_1', '=', 'value_1'],
        ['column_2', '<>', 'value_2'],
        [COLUMN, OPERATOR, VALUE],
        ...
    ])

www.Codefunda.tk

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Multiple orwhere condition in laravel in Codefunda

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 




// Multiple orwhere condition in laravel in Codefunda

// multiple orwhere condition in laravel

public function index()
    {
        $search = "Har";
  
        $users = User::select("*")->where('status', 1)
                  ->where(function($query) use ($search){
                   $query->where('first_name', 'LIKE', '%'.$search.'%')
                   ->orWhere('last_name', 'LIKE', '%'.$search.'%')
                   ->orWhere('email', 'LIKE', '%'.$search.'%');
                    })->get();
  
        dd($users);
    }

# With Join
  public function index()
    {
        $search = "Har";
  
        $users = User::select("*")->where('status', 1)
                  ->where(function($query) use ($search){
                   $query->where('first_name', 'LIKE', '%'.$search.'%')
                   ->orWhere('last_name', 'LIKE', '%'.$search.'%')
                   ->orWhere('email', 'LIKE', '%'.$search.'%');
                    })
          ->join('users_roles', 'users.id', '=', 'users_roles.user_id')
          ->where('users_roles.role_id', '=', Role::USER_PARTICIPANT)->get();
  
        dd($users);
    }


    // laravel where multiple conditions
    $query->where([
        ['column_1', '=', 'value_1'],
        ['column_2', '<>', 'value_2'],
        [COLUMN, OPERATOR, VALUE],
        ...
    ])

www.Codefunda.tk

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Multiple logical condition in laravel query in Codefunda

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 




// Multiple logical condition in laravel query in Codefunda

// multiple logical condition in laravel query

// ...
$q->where(function ($query) {
    $query->where('gender', 'Male')
        ->where('age', '>=', 18);
})->orWhere(function($query) {
    $query->where('gender', 'Female')
        ->where('age', '>=', 65);   
})



www.Codefunda.tk

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Multiple logical condition in laravel query in Codefunda

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 




// Multiple logical condition in laravel query in Codefunda

// multiple logical condition in laravel query

// ...
$q->where(function ($query) {
    $query->where('gender', 'Male')
        ->where('age', '>=', 18);
})->orWhere(function($query) {
    $query->where('gender', 'Female')
        ->where('age', '>=', 65);   
})



www.Codefunda.tk

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel where condition with if in Codefunda

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 




// laravel where condition with if

// laravel where condition with if


$query = DB::table('user_ads')
            ->join('ads', 'users_ads.ad_id', '=', 'ads.id')
            ->orderBy($column, $method);

if ($input['search']) {
    $query->where('short_description', $input['search']);
}

if ($input['category']) {
    $query->where('category', $input['category']);
}

$query->join('users', 'users_ads.user_id', '=', 'users.id')
    ->select('ads.id', 'ads.img1', 'ads.short_description', 'ads.category', 'ads.product', 'ads.price', 'users.city')

$result= $query->get();

return $result;






www.Codefunda.tk

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel where condition with if in Codefunda

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 




// laravel where condition with if

// laravel where condition with if


$query = DB::table('user_ads')
            ->join('ads', 'users_ads.ad_id', '=', 'ads.id')
            ->orderBy($column, $method);

if ($input['search']) {
    $query->where('short_description', $input['search']);
}

if ($input['category']) {
    $query->where('category', $input['category']);
}

$query->join('users', 'users_ads.user_id', '=', 'users.id')
    ->select('ads.id', 'ads.img1', 'ads.short_description', 'ads.category', 'ads.product', 'ads.price', 'users.city')

$result= $query->get();

return $result;






www.Codefunda.tk

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel where condition on relationship in Coderfunda.tk

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 



// laravel adding condition to relation

class Game extends Eloquent {
    // many more stuff here

    // relation without any constraints ...works fine 
    public function videos() {
        return $this->hasMany('Video');
    }

    // results in a "problem", se examples below
    public function available_videos() {
        return $this->videos()->where('available','=', 1)->get();
    }
}


// laravel where condition on relationship

App\Request::where('id',4)
    ->whereHas('quotes', function ($query) {
        $query->where('status','=','3');
    })
    ->with('quotes','sourceTable','destinationTable')
    ->get(); 



?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel where condition on relationship in Coderfunda.tk

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 



// laravel adding condition to relation

class Game extends Eloquent {
    // many more stuff here

    // relation without any constraints ...works fine 
    public function videos() {
        return $this->hasMany('Video');
    }

    // results in a "problem", se examples below
    public function available_videos() {
        return $this->videos()->where('available','=', 1)->get();
    }
}


// laravel where condition on relationship

App\Request::where('id',4)
    ->whereHas('quotes', function ($query) {
        $query->where('status','=','3');
    })
    ->with('quotes','sourceTable','destinationTable')
    ->get(); 



?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel multiple group by Coderfunda.tk

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 



// “laravel multiple group by” Coderfunda.tk

//laravel multiple group by

$routes = DB::table('route')
->groupBy('rte_origin', 'rte_destination')
->get();



?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel multiple group by Coderfunda.tk

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 



// “laravel multiple group by” Coderfunda.tk

//laravel multiple group by

$routes = DB::table('route')
->groupBy('rte_origin', 'rte_destination')
->get();



?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel if else condition in query in Coderfunda

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 



// laravel if else condition in query

$query = SocialMediaFeed::where('location_id', $location_id);
if(!$filters) {
    $query = $query->where('self', '<>', true);
} else {
    $query = $query->orWhere('self', true);
}

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel if else condition in query in Coderfunda

 Programing Coderfunda     December 12, 2020     Laravel     No comments   

 <?php 



// laravel if else condition in query

$query = SocialMediaFeed::where('location_id', $location_id);
if(!$filters) {
    $query = $query->where('self', '<>', true);
} else {
    $query = $query->orWhere('self', true);
}

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

11 December, 2020

Laravel eloquent where two conditions in Coderfunda

 Programing Coderfunda     December 11, 2020     Laravel     No comments   

 <?php 



// laravel where multiple conditions

$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel eloquent where two conditions in Coderfunda

 Programing Coderfunda     December 11, 2020     Laravel     No comments   

 <?php 



// laravel where multiple conditions

$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])

?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

How to use multiple where condition in codeigniter in Coderfunda

 Programing Coderfunda     December 11, 2020     Laravel     No comments   

 

 <?php

// How to use multiple where condition in codeigniter ;

$data = array('name' => 'Rockky');
        $multiClause = array('id' => $ids, 'name' => $name, 'status' => 1 );

        $this->db->where($multiClause);     
        $query = $this->db->get('table-name', $data);   
        return $query->result();
        


?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

How to use multiple where condition in codeigniter in Coderfunda

 Programing Coderfunda     December 11, 2020     Laravel     No comments   

 

 <?php

// How to use multiple where condition in codeigniter ;

$data = array('name' => 'Rockky');
        $multiClause = array('id' => $ids, 'name' => $name, 'status' => 1 );

        $this->db->where($multiClause);     
        $query = $this->db->get('table-name', $data);   
        return $query->result();
        


?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Count with condition laravel in coderfunda

 Programing Coderfunda     December 11, 2020     Laravel     No comments   

 <?php 


count with condition laravel

$user = User::where('user_id' , 10);
$user->count();




?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Count with condition laravel in coderfunda

 Programing Coderfunda     December 11, 2020     Laravel     No comments   

 <?php 


count with condition laravel

$user = User::where('user_id' , 10);
$user->count();




?>
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel where with multiple conditions query

 Programing Coderfunda     December 11, 2020     Laravel     No comments   

 


 i have used two different where condition on a single column
$data = Users::where('type',1)
        ->where(function($query) {
            return $query->whereDate('updated_at','!=', Carbon::today())
            ->orWhere('updated_at',null);
         })
         ->get();



 Laravel where multiple conditions
         $query->where([
            ['column_1', '=', 'value_1'],
            ['column_2', '<>', 'value_2'],
            [COLUMN, OPERATOR, VALUE],
            ...
        ])
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel where with multiple conditions query

 Programing Coderfunda     December 11, 2020     Laravel     No comments   

 


 i have used two different where condition on a single column
$data = Users::where('type',1)
        ->where(function($query) {
            return $query->whereDate('updated_at','!=', Carbon::today())
            ->orWhere('updated_at',null);
         })
         ->get();



 Laravel where multiple conditions
         $query->where([
            ['column_1', '=', 'value_1'],
            ['column_2', '<>', 'value_2'],
            [COLUMN, OPERATOR, VALUE],
            ...
        ])
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

WordPress Cheat Sheet (For Beginners & Developers)

 Programing Coderfunda     December 11, 2020     Wordpress     No comments   

 


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

WordPress Cheat Sheet (For Beginners & Developers)

 Programing Coderfunda     December 11, 2020     Wordpress     No comments   

 


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

10 December, 2020

How to partition a hard drive in Windows 10, and keep your most important files separate from the rest

 Programing Coderfunda     December 10, 2020     No comments   

 1. Press the Windows key + R on your keyboard to open the Run utility, or search for it in the Start menu.

2. Type "diskmgmt.msc" and press "OK." You can also copy and paste that from this article into the Run utility's text field.



3. A menu will appear with a list of all your hard drives. Right-click on the drive you want to partition and select "Shrink Volume."

4. You'll be asked how much you want the drive to shrink. The amount you enter will be the amount of space left for your new partition. The "total size after shrink" will be the space left in the original partition.

5. Still in the disk management utility, right-click the new box that appears at the bottom of the window — it should say much data you shrunk, and the word "Unallocated." In the right-click menu, select "New Simple Volume."

6. Follow the instructions in the New Simple Volume wizard to create the partition. This will allow you to set the partition's size, and to select the letter it will be assigned.

7. You may be asked which format you want the partition to be in. In nearly all cases, you should select "NTFS." In some other cases, like if you're trying to partition a USB flash drive, you'll want to pick "FAT32." 

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

How to partition a hard drive in Windows 10, and keep your most important files separate from the rest

 Programing Coderfunda     December 10, 2020     No comments   

 1. Press the Windows key + R on your keyboard to open the Run utility, or search for it in the Start menu.

2. Type "diskmgmt.msc" and press "OK." You can also copy and paste that from this article into the Run utility's text field.



3. A menu will appear with a list of all your hard drives. Right-click on the drive you want to partition and select "Shrink Volume."

4. You'll be asked how much you want the drive to shrink. The amount you enter will be the amount of space left for your new partition. The "total size after shrink" will be the space left in the original partition.

5. Still in the disk management utility, right-click the new box that appears at the bottom of the window — it should say much data you shrunk, and the word "Unallocated." In the right-click menu, select "New Simple Volume."

6. Follow the instructions in the New Simple Volume wizard to create the partition. This will allow you to set the partition's size, and to select the letter it will be assigned.

7. You may be asked which format you want the partition to be in. In nearly all cases, you should select "NTFS." In some other cases, like if you're trying to partition a USB flash drive, you'll want to pick "FAT32." 

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

How can we Create Multiple Where Clause Query Using Laravel Eloquent?

 Programing Coderfunda     December 10, 2020     Laravel     No comments   

 I'm using the Laravel Eloquent query builder and I have a query where I want a WHERE clause on multiple conditions. It works, but it's not ingenious.

Example:

$results = User::where('this', '=', 1)
->where('that', '=', 1)
->where('this_too', '=', 1)
->where('that_too', '=', 1)
->where('this_as_well', '=', 1)
->where('that_as_well', '=', 1)
->where('this_one_too', '=', 1)
->where('that_one_too', '=', 1)
->where('this_one_as_well', '=', 1)
->where('that_one_as_well', '=', 1)
->get();

Is there a better way to do this, or should I stick with this method?

Thank you!!

You can use Conditions using Array:

$users = User::where([
'column1' => value1,
'column2' => value2,
'column3' => value3
])->get();

That Will produce query like bellow:

SELECT * FROM TABLE WHERE column1=value1 and column2=value2 and column3=value3;

Conditions using Antonymous Function:

$users = User::where('column1', '=', value1)
->where(function($query) use ($variable1,$variable2){
$query->where('column2','=',$variable1)
->orWhere('column3','=',$variable2);
})
->where(function($query2) use ($variable1,$variable2){
$query2->where('column4','=',$variable1)
->where('column5','=',$variable2);
})->get();

That Will produce query like bellow:

SELECT * FROM TABLE WHERE column1=value1 and
 (column2=value2 or column3=value3) and (column4=value4 and column5=value5);

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

How can we Create Multiple Where Clause Query Using Laravel Eloquent?

 Programing Coderfunda     December 10, 2020     Laravel     No comments   

 I'm using the Laravel Eloquent query builder and I have a query where I want a WHERE clause on multiple conditions. It works, but it's not ingenious.

Example:

$results = User::where('this', '=', 1)
    ->where('that', '=', 1)
    ->where('this_too', '=', 1)
    ->where('that_too', '=', 1)
    ->where('this_as_well', '=', 1)
    ->where('that_as_well', '=', 1)
    ->where('this_one_too', '=', 1)
    ->where('that_one_too', '=', 1)
    ->where('this_one_as_well', '=', 1)
    ->where('that_one_as_well', '=', 1)
    ->get();

Is there a better way to do this, or should I stick with this method?

Thank you!!

You can use Conditions using Array:

$users = User::where([
       'column1' => value1,
       'column2' => value2,
       'column3' => value3
])->get();

That Will produce query like bellow:

SELECT * FROM TABLE WHERE column1=value1 and column2=value2 and column3=value3;

Conditions using Antonymous Function:

$users = User::where('column1', '=', value1)
               ->where(function($query) use ($variable1,$variable2){
                    $query->where('column2','=',$variable1)
                   ->orWhere('column3','=',$variable2);
               })
              ->where(function($query2) use ($variable1,$variable2){
                    $query2->where('column4','=',$variable1)
                   ->where('column5','=',$variable2);
              })->get();

That Will produce query like bellow:

SELECT * FROM TABLE WHERE column1=value1 and
 (column2=value2 or column3=value3) and (column4=value4 and column5=value5);

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg

Laravel Query Builder & Eloquent ORM

 Programing Coderfunda     December 10, 2020     Laravel     No comments   

 An application always needs to interact with a database and Laravel makes this task hassle free. Few tools that make Laravel an awesome framework is the inclusion of “Query Builder and Eloquent ORM”. Through this blog I intend to share few quick pointers on these concepts.

 Query Builder:

In LaravelLaravel the database query builder provides an easy way to create and run database queries. It can be used to perform all the database operations in your application, from basic DB Connection, CRUD, Aggregates etc and it works on all supported database systems like a champ.

 The notable factor about query builder is that, since it uses the PHP Data Objects (PDO), we need not worry about SQL injection attacks (Make sure we don’t inadvertently remove this protection). We can avoid all those lines of code to sanitise the data before feeding it to the DB.

 So, how do we create a simple select query to fetch all values from the users table?

1
$users = DB::table('users')->get();

 DB::table is responsible to begin a fluent query against a database table. The table from which the value has to be selected is mentioned inside the brackets within quotes and finally the get() method gets the values. Similarly to fetch a single row we can modify the above code by adding a where clause

1
$user = DB::table('users')->where('name', 'shivam')->first();

Here, we are trying to fetch a row that has the value Shivam in its name column. The first() method will only return the first find. What if we need only the user id of Shivam. Instead of returning the entire result array we can simply pluck out that specific column?

1
$user_id = DB::table('users')->where('name', 'shivam')->pluck('id');

For specifying more than one column we can use the select clause

1
$users = DB::table('users')->select('name', 'email')->get();

I now believe you are getting the grip. Things get more interesting further down.

We often write queries against certain ‘where conditions’. So how do we fetch the list of users whose user_id is less than 10?

1
$users = DB::table('users')->where(id, '<', 10)->get();

Yes, we split up the operator and the operands as three parameters and feed it to the where conditions. Now we have a situation, we need to fetch all those users whose user_id falls between 10 and 20.

1
$users = DB::table('users')->whereBetween('id', array(10, 20))->get()

Laravel has the whereBetween(), whereNotBetween(), wherein() and whereNotIn() methods to which we can pass values as an array.

Why are we passing values as an array and not as comma separated parameters?

At the start of this blog I did mention about SQL injection attacks. Let’s say that the values 10 and 20 are taken as user inputs. As programmers we cannot trust what the user types into the input field. He can be a valid user who enters proper values or someone trying to enter false values and crash your DB.

1
$users = DB::table('users')->whereBetween('id', array($from, $to))->get()

Here $from and $to are user inputs. If we look in to the Laravel’s database connection class for the select() method this array is wrapped around PDO connection and it is responsible to sanitize the data before the query is executed. So you have clean queries!!

Using Query builder we can also write raw SQL queries

1
DB::select(DB::raw(“SELECT * FROM `users` WHERE name = ‘$name’ ”));

Here $name is obtained from user input. $name may contain malicious code therefore we need to alter the above code to make it SQL friendly.

1
DB::select(DB::raw("SELECT * FROM `users` WHERE `name` = :username"),
array('username' => $name));

So the array value when passed through the PDO connection gets sanitized.

What is Eloquent in Laravel ?

Eloquent is object that is representative of your database and tables,in other words it’s act as controller between user and DBMS

For eg,

  • DB raw query,

select * from post LIMIT 1

  • Eloquent equivalent,

$post = Post::first();

$post->column_name;

What is ORM?

ORM or Object Relational Mapper is a technique to access objects without having to consider how those objects are related to their source.

What is Eloquent?

The ORM included in Laravel is called Eloquent and it enables us to work with the database objects and relationships using an expressive syntax. It is similar to working with objects in PHP. In Laravel, each database table has a corresponding “Model”. Eloquent ORM provides Active Record implementation which means that each model we create in our MVC structure corresponds to a table in our database.

Creating an Eloquent model is similar to creating a class. All the model files should be inside the app/models folder.

class Group extends Eloquent { }

All Eloquent models extends from the Eloquent class. The lower-case, plural name of the class will be used as the table name unless another name is explicitly specified. Eloquent will also assume that each table has a primary key column named “id” unless specified. We can specify a table as follows:

class Group extends Eloquent

1
2
3
4
5
{
 
protected $table = 'group_list';   // will point to group_list table if mentioned
 
}

Here, Group model will correspond to groups table (by default). We can access the data in the groups table using the basic CRUD operations.

By default Eloquent models will have auto-incrementing keys.

Create:

1
2
3
4
5
6
7
8
9
10
11
$new_group = new Group;
$new_group->name = 'NewGroup';
$new_group->description = 'Awesome Group';
$new_group->save();
<read:< pre="">
 
<strong>// To get all groups</strong>
 
Group::all();
 
// To find a group by passing the group id etc.
1
Group::find($id);

// Try to retrieve a model by primary key else throw an exception

1
2
$model = User::findOrFail(1);
$model = User::where('id', '>', 5)->firstOrFail();

Update:

// Retrieve and update

1
2
3
$group = Group::find(1);
$group->name = ‘Group01’;
$group->save();

// Using a WHERE clause

1
Group::where('name', '=', ‘Group01’)-date(array('name' => ‘Group1’));

// Delete one record

1
2
$group = Group::find(1);
$group->delete();

// Delete several

1
2
Group::destroy(1, 2, 3);
Group::where('id', '<', 10)->delete();

There are more topics to cover under Eloquent, hopefully in the next blog.

Conclusion:

It’s way of representing your database values with object you can easily use with

your application.

by: webkul.com

Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Older Posts Home

Meta

Popular Posts

  • Generate Migrations from an Existing Database With the Migration Generator Package
    Laravel Migration Generator Migration Generator for Laravel is a package by Bennett Treptow to generate migrations from existing database ...
  • Search Through Models with Laravel Searchable
      Laravel Searchable   is a package by   Spatie   to search through models and other sources pragmatically. Using this package, you can get ...
  • Laravel Razorpay Integration | Payment gateway integration Laravel in 30 mins | Laravel Razorpay
    1 Integration of Razorpay with Laravel. In this tutorial, I have taught how to integrate payment gateway with laravel with mini-project. If...
  • .Net 8 XUnit: Use an In-Memory DBConnection for testing as a replacement for the real MySqlConnection
    I'm creating tests for my .Net 8 API, and as I want to test with fake self created data (instead of using the real MySql connection) I...
  • Vue.js Render functions
        Vue.js Render functions Vue.js recommends us to use templates to build HTML. Here, we can use the render function as a closer-to-the-co...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (69)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • ►  2026 (12)
    • ►  07/26 - 08/02 (11)
    • ►  06/28 - 07/05 (1)
  • ►  2025 (4)
    • ►  07/06 - 07/13 (2)
    • ►  06/29 - 07/06 (2)
  • ►  2024 (486)
    • ►  09/15 - 09/22 (30)
    • ►  09/08 - 09/15 (35)
    • ►  09/01 - 09/08 (35)
    • ►  08/11 - 08/18 (2)
    • ►  08/04 - 08/11 (33)
    • ►  07/28 - 08/04 (30)
    • ►  07/07 - 07/14 (11)
    • ►  06/30 - 07/07 (35)
    • ►  06/23 - 06/30 (5)
    • ►  06/02 - 06/09 (31)
    • ►  05/26 - 06/02 (20)
    • ►  05/05 - 05/12 (29)
    • ►  04/28 - 05/05 (26)
    • ►  04/07 - 04/14 (10)
    • ►  03/31 - 04/07 (34)
    • ►  03/24 - 03/31 (10)
    • ►  03/03 - 03/10 (35)
    • ►  02/25 - 03/03 (15)
    • ►  02/04 - 02/11 (22)
    • ►  01/28 - 02/04 (30)
    • ►  01/07 - 01/14 (8)
  • ►  2023 (484)
    • ►  12/31 - 01/07 (35)
    • ►  12/24 - 12/31 (10)
    • ►  12/03 - 12/10 (33)
    • ►  11/26 - 12/03 (20)
    • ►  11/05 - 11/12 (35)
    • ►  10/29 - 11/05 (20)
    • ►  10/22 - 10/29 (9)
    • ►  10/15 - 10/22 (7)
    • ►  10/08 - 10/15 (9)
    • ►  10/01 - 10/08 (10)
    • ►  09/24 - 10/01 (9)
    • ►  09/17 - 09/24 (9)
    • ►  09/10 - 09/17 (7)
    • ►  09/03 - 09/10 (9)
    • ►  08/27 - 09/03 (9)
    • ►  08/20 - 08/27 (8)
    • ►  08/13 - 08/20 (8)
    • ►  08/06 - 08/13 (8)
    • ►  07/30 - 08/06 (8)
    • ►  07/23 - 07/30 (7)
    • ►  07/16 - 07/23 (8)
    • ►  07/09 - 07/16 (7)
    • ►  07/02 - 07/09 (8)
    • ►  06/25 - 07/02 (7)
    • ►  06/18 - 06/25 (7)
    • ►  06/11 - 06/18 (7)
    • ►  06/04 - 06/11 (11)
    • ►  05/28 - 06/04 (7)
    • ►  05/21 - 05/28 (8)
    • ►  05/14 - 05/21 (11)
    • ►  05/07 - 05/14 (7)
    • ►  04/30 - 05/07 (7)
    • ►  04/23 - 04/30 (8)
    • ►  04/16 - 04/23 (9)
    • ►  04/09 - 04/16 (7)
    • ►  04/02 - 04/09 (4)
    • ►  03/26 - 04/02 (21)
    • ►  03/19 - 03/26 (2)
    • ►  03/12 - 03/19 (9)
    • ►  03/05 - 03/12 (26)
    • ►  02/26 - 03/05 (25)
    • ►  01/15 - 01/22 (7)
    • ►  01/08 - 01/15 (1)
  • ►  2022 (1037)
    • ►  12/11 - 12/18 (13)
    • ►  12/04 - 12/11 (1)
    • ►  11/27 - 12/04 (40)
    • ►  11/06 - 11/13 (1)
    • ►  10/16 - 10/23 (13)
    • ►  09/04 - 09/11 (5)
    • ►  08/21 - 08/28 (24)
    • ►  08/14 - 08/21 (24)
    • ►  07/03 - 07/10 (9)
    • ►  06/19 - 06/26 (3)
    • ►  05/29 - 06/05 (3)
    • ►  05/22 - 05/29 (3)
    • ►  05/15 - 05/22 (109)
    • ►  05/01 - 05/08 (7)
    • ►  04/24 - 05/01 (7)
    • ►  04/17 - 04/24 (64)
    • ►  04/10 - 04/17 (115)
    • ►  04/03 - 04/10 (73)
    • ►  03/27 - 04/03 (77)
    • ►  03/13 - 03/20 (2)
    • ►  03/06 - 03/13 (25)
    • ►  02/27 - 03/06 (18)
    • ►  02/20 - 02/27 (153)
    • ►  02/13 - 02/20 (187)
    • ►  01/30 - 02/06 (45)
    • ►  01/23 - 01/30 (15)
    • ►  01/16 - 01/23 (1)
  • ►  2021 (412)
    • ►  10/24 - 10/31 (2)
    • ►  07/25 - 08/01 (1)
    • ►  07/11 - 07/18 (10)
    • ►  06/13 - 06/20 (29)
    • ►  05/23 - 05/30 (1)
    • ►  05/02 - 05/09 (24)
    • ►  04/25 - 05/02 (24)
    • ►  04/18 - 04/25 (112)
    • ►  04/11 - 04/18 (1)
    • ►  04/04 - 04/11 (6)
    • ►  03/28 - 04/04 (86)
    • ►  03/21 - 03/28 (19)
    • ►  03/14 - 03/21 (2)
    • ►  03/07 - 03/14 (10)
    • ►  02/28 - 03/07 (1)
    • ►  02/21 - 02/28 (29)
    • ►  02/14 - 02/21 (13)
    • ►  02/07 - 02/14 (12)
    • ►  01/31 - 02/07 (6)
    • ►  01/17 - 01/24 (2)
    • ►  01/10 - 01/17 (8)
    • ►  01/03 - 01/10 (14)
  • ▼  2020 (376)
    • ►  12/27 - 01/03 (37)
    • ►  12/20 - 12/27 (92)
    • ►  12/13 - 12/20 (29)
    • ▼  12/06 - 12/13 (37)
      • Multiple where laravel in Codefunda
      • Multiple where laravel in Codefunda
      • Multiple orwhere condition in laravel in Codefunda
      • Multiple orwhere condition in laravel in Codefunda
      • Multiple logical condition in laravel query in Cod...
      • Multiple logical condition in laravel query in Cod...
      • Laravel where condition with if in Codefunda
      • Laravel where condition with if in Codefunda
      • Laravel where condition on relationship in Coderfu...
      • Laravel where condition on relationship in Coderfu...
      • Laravel multiple group by Coderfunda.tk
      • Laravel multiple group by Coderfunda.tk
      • Laravel if else condition in query in Coderfunda
      • Laravel if else condition in query in Coderfunda
      • Laravel eloquent where two conditions in Coderfunda
      • Laravel eloquent where two conditions in Coderfunda
      • How to use multiple where condition in codeigniter...
      • How to use multiple where condition in codeigniter...
      • Count with condition laravel in coderfunda
      • Count with condition laravel in coderfunda
      • Laravel where with multiple conditions query
      • Laravel where with multiple conditions query
      • WordPress Cheat Sheet (For Beginners & Developers)
      • WordPress Cheat Sheet (For Beginners & Developers)
      • How to partition a hard drive in Windows 10, and k...
      • How to partition a hard drive in Windows 10, and k...
      • How can we Create Multiple Where Clause Query Usin...
      • How can we Create Multiple Where Clause Query Usin...
      • Laravel Query Builder & Eloquent ORM
      • Laravel Query Builder & Eloquent ORM
      • Latest 20 Laravel Eloquent Tips and Tricks
      • Latest 20 Laravel Eloquent Tips and Tricks
      • Download Newspaper 10.3.9 Nulled Free – News Maga...
      • Download Newspaper 10.3.9 Nulled Free – News Maga...
      • Laravel Interview Question Answer
      • Laravel Interview Questions And Answers 2020
      • Laravel Interview Questions And Answers 2020
    • ►  11/29 - 12/06 (4)
    • ►  11/15 - 11/22 (14)
    • ►  11/08 - 11/15 (8)
    • ►  11/01 - 11/08 (2)
    • ►  10/18 - 10/25 (14)
    • ►  10/11 - 10/18 (16)
    • ►  10/04 - 10/11 (10)
    • ►  09/20 - 09/27 (10)
    • ►  09/06 - 09/13 (19)
    • ►  08/30 - 09/06 (26)
    • ►  08/23 - 08/30 (4)
    • ►  08/16 - 08/23 (2)
    • ►  07/12 - 07/19 (48)
    • ►  05/17 - 05/24 (2)
    • ►  01/05 - 01/12 (2)
  • ►  2019 (74)
    • ►  07/07 - 07/14 (6)
    • ►  06/16 - 06/23 (6)
    • ►  02/10 - 02/17 (17)
    • ►  01/13 - 01/20 (37)
    • ►  01/06 - 01/13 (8)
  • ►  2018 (376)
    • ►  12/30 - 01/06 (24)
    • ►  12/16 - 12/23 (8)
    • ►  12/09 - 12/16 (98)
    • ►  12/02 - 12/09 (16)
    • ►  11/18 - 11/25 (36)
    • ►  11/04 - 11/11 (18)
    • ►  10/28 - 11/04 (10)
    • ►  10/21 - 10/28 (26)
    • ►  10/14 - 10/21 (52)
    • ►  10/07 - 10/14 (4)
    • ►  09/30 - 10/07 (2)
    • ►  09/23 - 09/30 (68)
    • ►  09/16 - 09/23 (4)
    • ►  09/09 - 09/16 (4)
    • ►  08/26 - 09/02 (6)

Data Publish News

Loading...

Al Jazeera – Breaking News, World News and Video from Al Jazeera

Loading...

Laravel News

Loading...

Copyright © CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda