• PHP
  • MySQL
  • Demos
  • HTML
  • CSS
  • jQuery
  • Framework
  • Social
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • PHP Jobs
  • Advertisement
PHP Lift
  • PHP
  • MySQL
  • Demos
  • HTML
  • CSS
  • jQuery
  • Framework
  • Social
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Youtube
    • RSS
Useful Laravel functions and methods, Part-1
Home
Framework

Useful Laravel functions and methods, Part-1

February 28th, 2017 Ivan Dimov Framework, PHP 0 comments

Facebook Twitter Google+ LinkedIn Pinterest

Laravel is one of the best PHP Framework so we are going to show you some cool functions and methods in this article I hope you like it.

dd($var) – This will print out beautifully its input.

request()->input(“parameterName”) – this method works with both GET and POST parameters and allows you to get the value of the given parameter.

view(“viewPath”, parametersArray) – the view function can be used as a return value within a controller to display a view (Blade/HTML/PHP template).

Session::flash(“name”, “msg”) – sets a session message on the next page render that disappears afterward.

request()->all() – returns an array of all user inputs

redirect()->back() – redirects back to the previous page that the user visited

redirect()->back()->withInput() – redirects back to the previous page that the user visited and passes back the inputs/parameters from this page.

redirect()->back()->with(“msgSuccess”, “The chosen articles were deleted.”); – you can pass arbitrary variables to the redirection; they might be stored in Session.

redirect(“root/relative/path”) – redirects to the given path

Session::get(‘sessionProperty’) – returns the value of the given session property

Database utilities

DB::table(‘tableName’) – you can use the DB class to interact with any database table within your established database connection. Once you select a table, you can chain methods to refine your query.

->select(‘blog.*, MAX(blog.id)’) – this would select some specific columns from the chosen table

->leftJoin(‘authors’, ‘blog.author_id’, ‘=’, ‘authors.id’) – this would perform a left join of another table based on the given condition

->whereIn(‘authors.id’, array(1,2,3,6,7) – this would make a wherein query to select all authors whose id is one of the values in the given array.

->orderBy(‘authors.age’) – this would order the authors by their age.

->orderBy(‘authors.age’, “ASC”) – this would order the authors in ascending order, by their age. Two values are possible – asc for ascending order and desc for descending.

->get() – this would finalize the actual database query and return the matching results

->first() – if you are looking for just one result, you can use first() to get the first matching row, you can also use it instead of get() in that case.

->max(“id”) – this will return the highest value of the field.
->delete() – chaining this to a query will delete all matching rows from the selection that precedes it.
->where(“id”, “=”, 3) – this will select only the item that has an id column with the value of 3.

$validator = Validator::make($data, [
        'name' => 'required|unique:authors,name,' . $id
    ], [
        'required' => “The :attribute field is required”,
        'unique' => trans('admin::validation.unique')
    ]);

Validator::make will make a validator, you can later check if some inputs pass the validation or not by using $validator->fails() which will return a Boolean with a truthy value if the validation fails. The first argument is the array with fields (you can just pass it request()->all()), the second are the rules according to which the data needs to be validated and the third are the messages that will be returned by the validator in case a specific validation fails. In the messages, you can use :attribute to refer to the field name, the message will then convert it to the proper field’s name.

I hope you like this article please do subscribe to get more in future.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

Facebook Twitter Google+ LinkedIn Pinterest
Next article 10 Programming Habits that every Developer Should Adopt
Previous article Working with dates in PHP and JavaScript

Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: http://www.dimoff.biz. facebook, twitter

Related Posts

Stripe Payment Gateway Charge Credit Card with PHP API
February 15th, 2021

Stripe Payment Gateway Charge Credit Card with PHP

6 Reasons Laravel is the Top PHP Framework in the Web Development Industry Framework
February 1st, 2021

6 Reasons Laravel is the Top PHP Framework in the Web Development Industry

12 Reasons to Choose PHP for Developing Website Guide
November 17th, 2020

12 Reasons to Choose PHP for Developing Website

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Advertisement
Recent Posts
  • Stripe Payment Gateway Charge Credit Card with PHP
  • How to building responsive WordPress theme using Bootstrap
  • 6 Reasons Laravel is the Top PHP Framework in the Web Development Industry
  • 9 Best Programming languages you should learn in 2021
  • 12 Reasons to Choose PHP for Developing Website
Categories
  • API
  • Bootstrap
  • CSS
  • CSS 3
  • Designing
  • Framework
  • Guide
  • HTML
  • HTML 5
  • JavaScript
  • jQuery
  • MySQL
  • Node.js
  • oAuth
  • Payment
  • PHP
  • Python
  • Social
  • Tips
  • WordPress
Weekly Tags
  • PHP
  • api
  • How to
  • PHP Basics
  • Programming Habits
  • HTML5
  • PHP framework
  • Download
  • laravel
  • css
  • About
  • Privacy Policy
  • Back to top
© PHPLift 2020. All rights reserved.