Micheal  Emard

Micheal Emard

1644329520

How to Work with Laravel one-to-one Relationship

In this video, we are going to work with laravel one-to-one relationship.

Laravel Relationship
Laravel relations
Laravel one to one relationship

#laravel 

What is GEEK

Buddha Community

How to Work with Laravel one-to-one Relationship

Laravel Eloquent Relationships

In this example we will see Laravel Eloquent relationships, laravel provides many relationship like laravel hasmany, laravel belongsto, wherehas laravel, many to many relationship laravel, eloquent relationships, one to many relationship laravel, one to one relationship laravel, here we will see all relationship with example.

There are 3 types of relationships in laravel:

  • One to one
  • One to many
  • Many to many

Read More : Laravel Eloquent Relationships

https://websolutionstuff.com/post/laravel-eloquent-relationships


You May Also Like :

  1. https://websolutionstuff.com/post/how-to-get-current-url-in-laravel
  2. https://websolutionstuff.com/post/laravel-8-google-pie-chart-example
  3. https://websolutionstuff.com/post/laravel-8-autocomplete-search-from-database

#laravel eloquent relationships #laravel relationship #one to one relationship #laravel hasmany #many to many relationship laravel #one to many relationship laravel

Seamus  Quitzon

Seamus Quitzon

1595201363

Php how to delete multiple rows through checkbox using ajax in laravel

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'
    ];
}

Step 2: Create Route

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

Seamus  Quitzon

Seamus Quitzon

1595216280

Some of the most frequent how tos in Laravel

How to get relationship from relationship using With() in Laravel

Some times there are cases where you want to get relationship from relationship in Laravel, that can be achieved via following:

Account::with(['profiles','profiles.products'])->get();

How to create multiple where clauses in single where clause in Laravel

Sometimes you want to apply multiple where clauses in a single query like that:

$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();

This can be achieved using single where clause by following code:

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

How to get last inserted id in Laravel

The are situations, during bulk insertion or single row insertion you may want to retrieve last inserted id, can be achieved by following:

$post = Input::All();

    $data = new Company;
    $data->nombre = $post['name'];
    $data->direccion = $post['address'];
    $data->telefono = $post['phone'];
    $data->email = $post['email'];
    $data->giro = $post['type'];
    $data->fecha_registro = date("Y-m-d H:i:s");
    $data->fecha_modificacion = date("Y-m-d H:i:s");
   $data->save();
   //getting last inserted id
   $data->id;

How to add a custom attribute in Laravel during Model load

Sometimes you want to add a custom attribute in model during load, this can be achieved by following code:

class Book extends Eloquent {

    protected $table = 'books';

    public function toArray()
    {
        $array = parent::toArray();
        $array['upper'] = $this->upper;
        return $array;
    }

    public function getUpperAttribute()
    {
        return strtoupper($this->title);    
    }

}

#laravel #php #how to add a custom attribute in laravel during model load #how to create multiple where clauses in single where clause in laravel #how to get a specific column using with() in laravel #how to get last inserted id in laravel #how to get relationship from relationship laravel #laravelfaq

Seamus  Quitzon

Seamus Quitzon

1595212560

5 Laravel’s Hidden Gems

1. Stop on first validation error

By default, Laravel will check for all validation rules and return a list of errors. But if you want to stop this process after first validation failure, that’s how you can achieve it

$request->validate([
    'title' => 'bail|required|unique:posts|max:255',
    'body' => 'required',
]);

2. Find many

Most of us developers have used Eloquent’s method find to search for one specific id, but do you know we can pass multiple ids to find which will return a collection.

// Will return Eloquent Model
$user = User::find(1);
// Will return Eloquent Collection
$users = User::find([1,2,3]);

3. Relationship but with condition

Some times we need to apply where on relationships, instead of calling relationship and then chaining where we can achieve it by following:

// app/Post.php model
public function comments()
{
    return $this->hasMany(Comment::class);
}
public function approved_comments()
{
    return $this->hasMany(Comment::class)->where('approved', 1);
}

4. Rename pivotal table

In pivot table, if you want to rename pivot to some thing else, this can be done by following code:

public function podcasts() {
return $this->belongsToMany('App\Podcast')
->as('subscription')
->withTimestamps();
}
// Then somewhere in Controller...
$podcasts = $user->podcasts();
foreach ($podcasts as $podcast) {
// instead of $podcast->pivot->created_at ...
echo $podcast->subscription->created_at;
}

#laravel #php #find many laravel #prepare for validation laravel #relationship but with condition laravel #rename pivotal table laravel #stop on first validation error laravel