Wednesday, December 12, 2018

Laravel : Pivot tables and many-to-many relationships

Pivot tables and many-to-many relationships

Today I want to talk about a feature of Laravel which is really useful but can be potentially difficult to understand at first. Pivot table is an example of intermediate table with relationships between two other “main” tables.

Real-life example of pivot tables

In official documentation they show the example of User-Role relationships, where user potentially can belong to several roles, and vice versa. So to make things clearer – let’s take another real-life example: Shops and Products.
Let’s say a company has a dozen of Shops all over city/country and a variety of products, and they want to store the information about which Product is sold in which Shop. It’s a perfect example of many-to-many relationship: one product can belong to several shops, and one shop can have multiple products.
So here’s a potential database structure:
shops
– id
– name
products
– id
– name
product_shop
– product_id
– shop_id
The final table in the list – product_shop is called a “pivot” table, as mentioned in the topic title. Now, there are several things to mention here.
  • Name of the pivot table should consist of singular names of both tables, separated by undescore symbole and these names should be arranged in alphabetical order, so we have to have product_shop, not shop_product.
  • To create a pivot table we can create a simple migration with artisan make:migration or use Jeffrey Way’s package Laravel 5 Generators Extended where we have a command artisan make:migration:pivot.
  • Pivot table fields: by default, there should be only two fields – foreign key to each of the tables, in our case product_id and shop_id. You can add more fields if you want, then you need to add them to relationship assignment – we will discuss that later.

Models for Many-to-Many Relationships: BelongsToMany

Ok, we have DB tables and migrations, now let’s create models for them. The main part here is to assign a many-to-many relationship – it can be done from either of “main” tables models.
So, option 1:
app/Shop.php:
Or option 2:
app/Product.php:
Actually, you can do both – it depends on how will you actuall use the relationship in other parts of the code: will you need $shop->products or more likely to query $product->shops, or both.
Now, with such declaration of relationships Laravel “assumes” that pivot table name obeys the rules and is product_shop. But, if it’s actually different (for example, it’s plural), you can provide it as a second parameter:



Wednesday, December 5, 2018

Vue: Vue Js starter code

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
<div id="app">
<div>
<input v-model="message">
<p>{{ message }}</p>
</div>

<div>
<span v-bind:title="message">
Hover your mouse over me for a few seconds
to see my dynamically bound title!
</span>
</div>

<div>
<span v-if="seen">Now you see me asdfasdf</span>
</div>


<ol>
<li v-for="todo in todos">
{{ todo.text }}
</li>
</ol>
<ol>
<todo-item
v-for="item in groceryList"
v-bind:todo="item"
v-bind:key="item.id"
></todo-item>
</ol>
</div>


<script>

Vue.component('todo-item', {
props: ['todo'],
template: '<li>{{ todo.text }}</li>'
})

new Vue({
el: '#app',
data: {
message: 'Hello Vue.js!',
message1: 'You loaded this page on ' + new Date().toLocaleString(),
seen: false,
todos: [
{ text: 'Learn JavaScript' },
{ text: 'Learn Vue' },
{ text: 'Build something awesome' }
],
groceryList: [
{ id: 0, text: 'Vegetables' },
{ id: 1, text: 'Cheese' },
{ id: 2, text: 'Whatever else humans are supposed to eat' }
]
}
})

</script>
</body>
</html>

Wednesday, November 28, 2018

finding difference between two date in php and javascript

Javascript code:

<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function sayHello() {
//var date1 = new Date("11/28/2018");
//var date2 = new Date("12/1/2018");

var date1 = new Date("2018-11-28");
var date2 = new Date("2018-12-01");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays);

}
sayHello();
</script>
</head>
<body>
</body>
</html>

PHP code:

$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");

OR

$startDate = strtotime('2018-12-01');
$toDay = strtotime(date('Y-m-d'));
$time = abs($toDay - $loginDate);
$days = floor($time / 86400);


Wednesday, November 21, 2018

Laravel : Laravel cheet sheet

Cakephp : Cakephp Cheet sheet

A CakePHP cheat sheet (CakePHP reference page)

Summary: This is a CakePHP cheat sheet. (Note: This reference was initially created in 2011, and may be slightly out of date.)
As I embark on another CakePHP project after a long hiatus, I'm trying to cram CakePHP back into my head. As part of this effort, I'm creating this large CakePHP cheat sheet (reference page), which I hope will be helpful to the CakePHP community. It is based on both the CakePHP cheat sheet on their Trac website and the CakePHP Cookbook (see the links below).
(Go ahead and say what you will about me, but I happen to like all my CakePHP examples on one big page like this that I can search, rather than spread out across many different pages. That's what a cheat sheet is, right? Also, please note that there are very few ads on this page, besides the standard ads on the side columns.)