background preloader

Laravel

Facebook Twitter

Pivot tables and many-to-many relationships - Laravel Daily. 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.

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. Models for Many-to-Many Relationships: BelongsToMany So, option 1:app/Shop.php: Or option 2:app/Product.php: Conclusion. Laravel Collective. Installation Begin by installing this package through Composer. Edit your project's composer.json file to require laravelcollective/html. composer require "laravelcollective/html":"^5.4.0" Next, add your new provider to the providers array of config/app.php: 'providers' => [ Collective\Html\HtmlServiceProvider::class, ], Finally, add two class aliases to the aliases array of config/app.php: 'aliases' => [ 'Form' => Collective\Html\FormFacade::class, 'Html' => Collective\Html\HtmlFacade::class, ], Looking to install this package in Lumen?

Opening A Form Opening A Form By default, a POST method will be assumed; however, you are free to specify another method: echo Form::open(['url' => 'foo/bar', 'method' => 'put']) Note: Since HTML forms only support POST and GET, PUT and DELETE methods will be spoofed by automatically adding a _method hidden field to your form. You may also open forms that point to named routes or controller actions: You may pass in route parameters as well: CSRF Protection Labels Date. Astuce Laravel 5.4 : Comment gérer les requêtes ajax (GET, POST) et les tokens ? | Blog développeuse - Hamon.

Faire un site web aujourd’hui, c’est aussi savoir ne pas recharger la page à chaque action d’utilisateur, pour cela on a JavaScript, bien connu depuis des décennies maintenant. Mais comment l’utiliser avec Laravel ? Pour les besoins de notre page, on va ajouter un système de vote par article. Notre but est de voter pour promouvoir un article sans devoir recharger la page du dit article. Note : Dans cet exemple, je n’utilise que la librairie jQuery mais rien ne nous empêche d’utiliser Angular ou un autre framework JS.

Ici je reste le plus simple possible pour faciliter la compréhension. Dans la vue de notre article, on va ajouter le manager JavaScript de nos posts que l’on aura créé dans le dossier « public/js/posts.js » : /* resources/views/post.blade.php */ @extends('layouts.app') @section('content') /*...*/ <script src="{{ asset('js/posts.js') }}"></script> @endsection Et créé notre manager simple et lisible même si on utilise quasiment du JS pur : Laravel AJAX Pagination with JQuery. Part 3: Fluent Routing - Laravel 5.4 New Features.

Errors