Laravel PHP Web Development Framework

Source: Internet
Author: User
Tags php web development
<span id="Label3"></p><p><p>Laravel is a simple, elegant PHP Web development Framework (php Web framework). It frees you from the messy code of noodles, and it helps you build a perfect web app, and every line of code can be concise and Expressive.</p></p><p><p>This article explains the Laravel framework of database curd operation, coherent operation, the use of chain operation, including a large number of database operations commonly used methods, interested students reference.</p></p><p><p>Laravel is a simple, elegant PHP Web development Framework (php Web framework). It frees you from the messy code of noodles, and it helps you build a perfect web app, and every line of code can be concise and Expressive.</p></p><p><p><strong>first, Selects</strong></p></p><p><p>Retrieving all rows in a table</p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->get ();<br clear="none">foreach ($users as $user)<br clear="none">{<br clear="none">Var_dump ($user->name);<br clear="none">}<p><p></p></p><p><p><strong>Retrieving a single row from a table</strong></p></p>The code is as Follows:<br clear="none">$user = db::table (' users ')->where (' name ', ' John ')->first ();<br clear="none">Var_dump ($user->name);<p><p><br clear="none"><strong>Retrieving rows for a single column</strong></p></p><p><p></p></p>The code is as Follows:<p><p></p></p><br clear="none">$name = db::table (' users ')->where (' name ', ' John ')->pluck (' name ');<p><p><br clear="none"><strong>Retrieving a list of column values</strong></p></p><p><p></p></p>The code is as Follows:<p><p></p></p><br clear="none">$roles = db::table (' roles ')->lists (' title ');<p><p><br clear="none"><strong>The method returns the function of an array header. You can also specify a custom key column to return an array of</strong></p></p><p><p></p></p>The code is as Follows:<p><p></p></p><br clear="none">$roles = db::table (' roles ')->lists (' title ', ' name ');<p><p><br clear="none"><strong>Specify a SELECT clause</strong></p></p><p><p></p></p>The code is as Follows:<p><p></p></p><br clear="none">$users = db::table (' users ')->select (' name ', ' email ')->get ();<br clear="none">$users = db::table (' users ')->distinct ()->get ();<br clear="none">$users = db::table (' users ')->select (' name as user_name ')->get ();<p><p></p></p><p><p><strong>The SELECT clause is added to an existing query $query = db::table (' users ')->select (' name ');</strong></p></p>The code is as Follows:<br clear="none">$users = $query->addselect (' Age ')->get ();<p><p></p></p><p><p><strong>where</strong></p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->where (' votes ', ' > ', +)->get ();<p><p></p></p><p><p><strong>OR</strong></p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->where (' votes ', ' > ', ')->orwhere (' name ', ' John ')->get ();<p><p></p></p><p><p><strong>Where between</strong></p></p><p><p></p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->wherebetween (' votes ', Array (1))->get ();<p><p></p></p><p><p><strong>Where not between</strong></p></p><p><p></p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->wherenotbetween (' votes ', Array (1))->get ();<p><p></p></p><p><p><strong>Where in with an Array</strong></p></p><p><p></p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->wherein (' ID ', array (1, 2, 3))->get ();<br clear="none">$users = db::table (' users ')->wherenotin (' ID ', array (1, 2, 3))->get ();<p><p></p></p><p><p><strong>Using Where Null to Find Records with Unset Values</strong></p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->wherenull (' updated_at ')->get ();<p><p></p></p><p><p><strong>Order by, Group by, and have</strong></p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->orderby (' name ', ' desc ')->groupby (' count ')->having (' count ', ' > ', Get ();<p><p></p></p><p><p><strong>Offset & Limit</strong></p></p><p><p></p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->skip (ten)->take (5)->get ();<p><p></p></p><p><p><strong>second, the connection</strong></p></p><p><p><strong>Joins</strong></p></p><p><p>The query Builder can also be used to write connection Statements. Take a look at the following example:</p></p><p><p><strong>Basic Join Statement</strong></p></p><p><p></p></p>The code is as Follows:<br clear="none">Db::table (' Users ')<br clear="none">->join (' contacts ', ' users.id ', ' = ', ' contacts.user_id ')<br clear="none">->join (' orders ', ' users.id ', ' = ', ' orders.user_id ')<br clear="none">->select (' users.id ', ' contacts.phone ', ' Orders.price ')<br clear="none">->get ();<p><p></p></p><p><p><strong>Left join-up statements</strong></p></p><p><p></p></p>The code is as Follows:<br clear="none">Db::table (' Users ')<br clear="none">->leftjoin (' posts ', ' users.id ', ' = ', ' posts.user_id ')<br clear="none">->get ();<br clear="none">Db::table (' Users ')<br clear="none">->join (' contacts ', function ($join)<br clear="none">{<br clear="none">$join->on (' users.id ', ' = ', ' contacts.user_id ')->oron (...);<br clear="none">})<br clear="none">->get ();<br clear="none">Db::table (' Users ')<br clear="none">->join (' contacts ', function ($join)<br clear="none">{<br clear="none">$join->on (' users.id ', ' = ', ' contacts.user_id ')<br clear="none">->where (' contacts.user_id ', ' > ', 5);<br clear="none">})<br clear="none">->get ();<p><p></p></p><p><p><strong>third, Group</strong></p></p><p><p>sometimes, you may want to create more advanced where clauses, such as "presence" or nested parameter groupings. Laravel Query Builder can handle these:</p></p><p><p></p></p>The code is as Follows:<br clear="none">Db::table (' Users ')<br clear="none">->where (' name ', ' = ', ' John ')<br clear="none">->orwhere (function ($query)<br clear="none">{<br clear="none">$query->where (' votes ', ' > ', 100)<br clear="none">->where (' title ', ' <> ', ' Admin ');<br clear="none">})<br clear="none">->get ();<p><p><br clear="none">The above query will produce the following sql:</p></p><p><p></p></p>The code is as Follows:<p><p></p></p><br clear="none">SELECT * from users where name = ' John ' or (votes > and title<br clear="none"><> ' Admin ')<br clear="none">Exists statements<br clear="none">Db::table (' Users ')<br clear="none">->whereexists (function ($query)<br clear="none">{<br clear="none">$query->select (db::raw (1))<br clear="none">->from (' orders ')<br clear="none">->whereraw (' orders.user_id = users.id ');<br clear="none">})<br clear="none">->get ();<p><p></p></p><p><p>The above query will produce the following sql:</p></p>The code is as Follows:<br clear="none">SELECT * FROM Userswhere exists (<br clear="none">Select 1 from orders where orders.user_id = Users.id<br clear="none">)<p><p></p></p><p><p><strong>Iv. Aggregation</strong></p></p><p><p>The Query Builder also provides a variety of aggregation methods, such as statistics, max, min,avg, and Sum.</p></p><p><p>Using Aggregate Methods</p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->count ();<br clear="none">$price = db::table (' orders ')->max (' price ');<br clear="none">$price = db::table (' orders ')->min (' price ');<br clear="none">$price = db::table (' orders ')->avg (' price ');<br clear="none">$total = db::table (' users ')->sum (' votes ');<p><p></p></p><p><p><strong>Raw Expressions</strong></p></p><p><p>Sometimes you may want to use a query with a raw Expression. These expressions will inject the query string, so be careful not to create any SQL injection points! to create a primitive expression, you can use Db:rawmethod:</p></p><p><p><strong>Using A Raw Expression</strong></p></p><p><p></p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')<br clear="none">->select (db::raw (' Count (*) as user_count, status '))<br clear="none">->where (' status ', ' <> ', 1)<br clear="none">->groupby (' Status ')<br clear="none">->get ();<p><p></p></p><p><p><strong>Increment or decrement the value of a column</strong></p></p>The code is as Follows:<br clear="none">Db::table (' Users ')->increment (' votes ');<br clear="none">Db::table (' Users ')->increment (' votes ', 5);<br clear="none">Db::table (' Users ')->decrement (' votes ');<br clear="none">Db::table (' Users ')->decrement (' votes ', 5);<p><p></p></p><p><p><strong>You can also specify additional column updates:</strong></p></p>The code is as Follows:<br clear="none">Db::table (' Users ')->increment (' votes ', 1, array (' name ' = ' John ') ');<p><p></p></p><p><p>Inserts</p></p><p><p><strong>Inserting records into a table</strong></p></p><p><p></p></p>The code is as Follows:<br clear="none"><strong><strong>db::table (' users ')->insert (</strong></strong><br clear="none"><strong><strong>array (' Email ' = ' [email protected] ', ' votes ' + 0)</strong></strong><br clear="none"><strong><strong>);</strong></strong><p><p></p></p><p><p><strong>Automatically increment the ID of the record into the table</strong></p></p><p><p>If the table has an auto-incrementing ID field, use Insertgetid to insert a record and retrieve the Id:</p></p>The code is as Follows:<br clear="none"><strong><strong>$id = db::table (' users ')->insertgetid (</strong></strong><br clear="none"><strong><strong>array (' Email ' = ' [email protected] ', ' votes ' + 0)</strong></strong><br clear="none"><strong><strong>);</strong></strong><p><p></p></p><p><p>Note: when using the PostgreSQL Insertgetid method, It is expected that the Self-increment column is named "id".</p></p><p><p><strong>Inserting multiple records into a table</strong></p></p>The code is as Follows:<br clear="none">Db::table (' users ')->insert (array (<br clear="none">Array (' Email ' = ' [email protected] ', ' votes ' + 0),<br clear="none">Array (' Email ' = ' [email protected] ', ' votes ' + 0),<br clear="none">));<p><p></p></p><p><p><strong>Iv. Updates</strong></p></p><p><p>To update a record in a table</p></p>The code is as Follows:<br clear="none"><strong><strong>db::table (' users ')</strong></strong><br clear="none"><strong><strong>->where (' ID ', 1)</strong></strong><br clear="none"><strong><strong>->update (array (' votes ' = 1));</strong></strong><p><p></p></p><p><p><strong>wu, Deletes</strong></p></p><p><p><strong>Delete a record in a table</strong></p></p>The code is as Follows:<br clear="none"><strong><strong>db::table (' Users ')->where (' votes ', ' < ', +)->delete ();</strong></strong><p><p></p></p><p><p><strong>Delete all records in a table</strong></p></p>The code is as Follows:<br clear="none">Db::table (' users ')->delete ();<p><p><br clear="none"><strong>Delete a table</strong></p></p><p><p></p></p>The code is as Follows:<p><p></p></p><br clear="none">Db::table (' users ')->truncate ();<p><p></p></p><p><p><strong>liu, Unions</strong></p></p><p><p>The Query Builder also provides a quick way to "federate" two of queries:</p></p><p><p></p></p>The code is as Follows:<br clear="none">$first = db::table (' users ')->wherenull (' first_name ');<br clear="none">$users =<br clear="none">Db::table (' users ')->wherenull (' last_name ')->union ($first)->get ();<p><p></p></p><p><p>The UnionAll method can also have the same method Signature.</p></p><p><p>Pessimistic Locking</p></p><p><p>The query Builder includes some "pessimistic locking" features to help you make your select Statement. Run the SELECT statement "shared lock", which you can query using the Sharedlock method:</p></p>The code is as Follows:<br clear="none">Db::table (' Users ')->where (' votes ', ' > ',<br clear="none">()->sharedlock ()->get ();<p><p><br clear="none">Update "lock" in a select statement, you can use the Lockforupdate method to Query:</p></p><p><p></p></p>The code is as Follows:<p><p></p></p><br clear="none">Db::table (' Users ')->where (' votes ', ' > ', ')->lockforupdate ()->get ();<p><p></p></p><p><p><strong>seven, Cache Query</strong></p></p><p><p>You can easily cache the results of a query using the mnemonic method:</p></p>The code is as Follows:<br clear="none">$users = db::table (' users ')->remember (ten)->get ();<p><p><br clear="none">In this case, the result of the query will be <strong>cached for 10 minutes</strong> .  When you query the results cache, the database is not run, and the results are assigned to your application from the default cache load Driver. If you are using a driver that supports caching, you can also add a tag to cache:</p></p><p><p></p></p>The code is as Follows:<p><p></p></p><br clear="none">$users = db::table (' users ')->cachetags (array (' people ', ' authors '))->remember ()->get ();<p><p>Laravel PHP Web Development Framework</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.