1595906940
Laravel eloquent join 2, 3, or multiple tables example. Here, you will learn how to use laravel eloquent join 2 or multiple tables for fetching data from database tables.
And As well as how to use laravel eloquent join() with multiple where conditions.
If you want to join two or multiple tables in laravel then you can use laravel eloquent join(), left join(), right join(), cross join(). And another option to join two or multiple table, you can use laravel eloquent relationships instead of laravel join.
Note that, you can also join two or multiple tables using laravel left, rigth and cross join.
Laravel JOIN eloquent returns all rows from the both table, if there are matches in the both table. Otherwise, the result is NULL.
Now, demonstrates laravel eloquent join with the following examples. You can see the following example of laravel eloquent join() method:
Here, fetch data using laravel eloquent join(), you can see the following example:
$users = User::join('posts', 'users.id', '=', 'posts.user_id')
->get(['users.*', 'posts.descrption']);
When you dump the above-given laravel eloquent join query, you will get the following SQL query:
select `users`.*, `posts`.`descrption` from `users`
inner join `posts` on `users`.`id` = `posts`.`user_id`
In this example, get data using laravel eloquent join 3 table, you can see the following example:
$users = User::join('posts', 'posts.user_id', '=', 'users.id')
->join('comments', 'comments.post_id', '=', 'posts.id')
->get(['users.*', 'posts.descrption']);
When you dump the above-given laravel eloquent join 3 table query, you will get the following SQL query:
select `users`.*, `posts`.`descrption` from `users`
inner join `posts` on `posts`.`user_id` = `users`.`id`
inner join `comments` on `comments`.`post_id` = `posts`.`id`
In this example, get data using laravel eloquent join with multiple where conditions, you can see the following example:
$users = User::join('posts', 'posts.user_id', '=', 'users.id')
->where('users.status', 'active')
->where('posts.status','active')
->get(['users.*', 'posts.descrption']);
When you dump the above-given laravel eloquent join with multiple conditions query, you will get the following SQL query:
select `users`.*, `posts`.`descrption` from `users`
inner join `posts` on `posts`.`user_id` = `users`.`id`
where `users`.`status` = active and `posts`.`status` = active
**In another option instead of join laravel, **you can use laravel relationship, once you created laravel relationship it will work like join.
#laravel
1627450200
Hello Guys,
Today I will show you how to create laravel AJAX CRUD example tutorial. In this tutorial we are implements ajax crud operation in laravel. Also perform insert, update, delete operation using ajax in laravel 6 and also you can use this ajax crud operation in laravel 6, laravel 7. In ajax crud operation we display records in datatable.
#laravel ajax crud example tutorial #ajax crud example in laravel #laravel crud example #laravel crud example with ajax #laravel #php
1595209620
As a developer, I have experienced changes in app when it is in production and the records have grown up to millions. In this specific case if you want to alter a column using simple migrations that will not work because of the following reasons:
It is not so easy if your production servers are under heavy load and the database tables have 100 million rows. Because such a migration will run for some seconds or even minutes and the database table can be locked for this time period – a no-go on a zero-downtime environment.
In this specific case you can use MySQL’s algorithms: Online DDL operations. That’s how you can do it in Laravel.
First of all create migration. For example I want to modify a column’s name the traditional migration will be:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'first_name');
});
Run the following command php artisan migrate –pretend this command will not run the migration rather it will print out it’s raw sql:
ALTER TABLE users CHANGE name first_name VARCHAR(191) NOT NULL
Copy that raw sql, remove following code:
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('name', 'first_name');
});
Replace it with following in migrations up method:
\DB::statement('ALTER TABLE users CHANGE name first_name VARCHAR(191) NOT NULL');
Add desired algorithm, in my case query will look like this:
\DB::statement('ALTER TABLE users CHANGE name first_name VARCHAR(191) NOT NULL, ALGORITHM=INPLACE, LOCK=NONE;');
#laravel #mysql #php #alter heavy tables in production laravel #alter table in production laravel #alter tables with million of records in laravel #how to alter heavy table in production laravel #how to alter table in production larave #mysql online ddl operations
1594769515
Data validation and sanitization is a very important thing from security point of view for a web application. We can not rely on user’s input. In this article i will let you know how to validate mobile phone number in laravel with some examples.
if we take some user’s information in our application, so usually we take phone number too. And if validation on the mobile number field is not done, a user can put anything in the mobile number field and without genuine phone number, this data would be useless.
Since we know that mobile number can not be an alpha numeric or any alphabates aand also it should be 10 digit number. So here in this examples we will add 10 digit number validation in laravel application.
We will aalso see the uses of regex in the validation of mobile number. So let’s do it with two different way in two examples.
In this first example we will write phone number validation in HomeController where we will processs user’s data.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('createUser');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'phone' => 'required|digits:10',
'email' => 'required|email|unique:users'
]);
$input = $request->all();
$user = User::create($input);
return back()->with('success', 'User created successfully.');
}
}
In this second example, we will use regex for user’s mobile phone number validation before storing user data in our database. Here, we will write the validation in Homecontroller like below.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Validator;
class HomeController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('createUser');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:10',
'email' => 'required|email|unique:users'
]);
$input = $request->all();
$user = User::create($input);
return back()->with('success', 'User created successfully.');
}
}
#laravel #laravel phone number validation #laravel phone validation #laravel validation example #mobile phone validation in laravel #phone validation with regex #validate mobile in laravel
1591339307
https://www.youtube.com/watch?v=HoPol7IBo3g
#laravel #laravel eloquent relationships #laravel eloquent
1595201363
First thing, we will need a table and i am creating products table for this example. So run the following query to create table.
CREATE TABLE `products` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
Next, we will need to insert some dummy records in this table that will be deleted.
INSERT INTO `products` (`name`, `description`) VALUES
('Test product 1', 'Product description example1'),
('Test product 2', 'Product description example2'),
('Test product 3', 'Product description example3'),
('Test product 4', 'Product description example4'),
('Test product 5', 'Product description example5');
Now we are redy to create a model corresponding to this products table. Here we will create Product model. So let’s create a model file Product.php file under app directory and put the code below.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $fillable = [
'name','description'
];
}
Now, in this second step we will create some routes to handle the request for this example. So opeen routes/web.php file and copy the routes as given below.
routes/web.php
Route::get('product', 'ProductController@index');
Route::delete('product/{id}', ['as'=>'product.destroy','uses'=>'ProductController@destroy']);
Route::delete('delete-multiple-product', ['as'=>'product.multiple-delete','uses'=>'ProductController@deleteMultiple']);
#laravel #delete multiple rows in laravel using ajax #laravel ajax delete #laravel ajax multiple checkbox delete #laravel delete multiple rows #laravel delete records using ajax #laravel multiple checkbox delete rows #laravel multiple delete