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.
Leave a Reply