Tag Archives: laravel tutorials for beginners

Laravel Redirect All Requests To HTTPS


Laravel Redirect All Requests To HTTPS : We sometimes need to redirect all requests to https in laravel. Here in this tutorial we are going to explain how you can redirect all the request to https in Laravel.


Laravel Redirect All Requests To HTTPS

You can use the app/filters.php file and it’s block App::before() check each request if it is not secure redirect it to the https as below –

Laravel Redirect All Requests To HTTPS:

App::before(function($request)
{
    if( ! Request::secure())
    {
        return Redirect::secure(Request::path());
    }
});

The above example will redirect all request to secure https request.

Laravel redirect back


Laravel redirect back : Redirect::back(); is used to redirect back the users from the current url to the back url. Here in this tutorial we are going to explain how you can redirect from current url to back url with errors in laravel.


Laravel redirect back

You can redirect back to the previous url in laravel as below –

Laravel redirect back:

Session::flash('message', "Data saved successfully.");
Redirect::back();

The above example will redirect back to previous url with the given message.

Now to display the message add the following flash message to display it-

Laravel redirect back with message:

@if (Session::has('message'))
   {{ Session::get('message') }}
@endif

The above example will display the message added in the redirect back url.

Laravel select last row from table


Laravel select last row from table : You can get the last row from table by sorting the data in descending order and then get the first row which will give the last row from the table. Here in this example we are going to explain how to get the last row from table.


Laravel select last row from table

You can select the last row from table as below-

Laravel select last row from table:

DB::table('MyTable')->orderBy('ID', 'desc')->first();

The above example will give you the last row from the table Mytable.

If you are using the Eloquent you can select the last record as below-

Laravel select last row from table:

User::orderBy('ID', 'desc')->first();

The above example will fist sort by column ID(auto increment) in descending order and then it will select the first result which will give you the last record from table.

Download files in Laravel


Download files in Laravel : Downloading files from url is very easy in laravel. Response::download($filePath)is used to download files in laravel. This generates a response which forces users browser to download the file from the specified path. Here in this tutorial we are going to explain the download files.


Download files in Laravel

Here in syntax for downloading files in laravel.

Syntax

Syntax for Downloading files in Laravel 5 from url:

return response()->download($pathToFile, $name, $headers);
  • $pathToFile : Full Path of file to be downloaded.
  • $name : Name is optional. This is name which user will see after downloading the file.
  • $headers : Headers are also optional. You can pass an array of HTTP headers.

Example

Here is a simple function which contains the code to download files in laravel using the Response::download() .-

Download files in Laravel 5 from url Example

public function fileDownload(){
        //Suppose profile.docx file is stored under project/public/download/profile.docx
        $file= public_path(). "/download/profile.docx";
        $headers = array(
              'Content-Type: application/octet-stream',
            );
        return Response::download($file, 'my-filename.docx', $headers);
}

The above example will download the file with name my-filename.docx on user’s machine. Both second and third parameters are optional.

Get current route name in laravel 5


Get current route name in laravel 5 : You can get the current route name in laravel 5 simply using the Route::getCurrentRoute()->getPath();. Here in this tutorial we are going to explain the methods to get the current route.


Get current route name in laravel 5

You can use the below method to get the current route in the laravel5 –

Get current route name in laravel 5:

Route::getCurrentRoute()->getPath();
or 
\Request::route()->getName();

The above example will give you the current route in laravel 5.1

Get Route Action Name

You can use the below syntax to get the current route name –

How to get current route name in laravel 5:

Route::getCurrentRoute()->getActionName();

The above example will give you the current route action name in laravel 5.1

Laravel Redirect to Url


Laravel Redirect to Url : Laravel Redirects are basically instance of the Illuminate\Http\RedirectResponse. This contains the headers which are responsible to redirect the user from on url to another url. There are many ways to redirect the user from one url to another url. The simple method is use helper method of global helper helper. In this tutorial we are going to explain how to use redirect method in laravel to redirect the users from one url to another url.


Laravel Redirect to Url

Here are the redirection methods which are used in Laravel-

Simple Redirect-

Laravel Redirect to Url:

Route::get('users', function () {
    return redirect('home/users');
});

Which Will redirect user to home/users location.

More About Laravel Redirect to Url

Let us have more example on laravel redirect-

Laravel Redirect Back-

Laravel Redirect Back to url-

Route::get('users/login', function () {
   return back()->withInput();
});

Which Will redirect user to back to the previous location.

Laravel Redirect to Named Routes-

Laravel Redirect to Named Routes-

return redirect()->route('users');

Which Will redirect user to the route users.

Laravel Redirect to Named Routes With Parameters-

Laravel Redirect to Named Routes With Parameters-

return redirect()->route('users',['id' => 10]);

Which Will redirect user to the route users with parameter id and it’s value 10.

Laravel Redirect to Controller Action-

You can redirect to controller Action in laravel as below-

Laravel Redirect to Controller Action-

return redirect()->action('UsersController@index');

Which Will redirect user to the route index Action of UsersController Controller.

Laravel Redirect to Controller Action With Parameters-

You can redirect to controller Action with parameters in laravel as below-

Laravel Redirect to Controller Action With Parameters-

return redirect()->action('UsersController@index',['id' => 10]);

Which Will redirect user to the route index Action with parameter id and it’s value 10 of UsersController Controller.

Laravel Redirect with Flashed Session Data-

You can redirect flashed session data in laravel as below-

Laravel Redirect with Flashed Session Data-

Route::post('user/profile', function () {
    // Update the user's profile information...
    return redirect('home')->with('messgae', 'User's Profile updated Successfully!');
});

Which Will redirect user to the home with flashed session message ‘User’s Profile updated Successfully!’.

Note :Flashed Messages are shown only once and cleared after that.

Laravel delete multiple rows


Laravel delete multiple rows : You can delete multiple rows in laravel by passing array of ids which you want to delete. You can pass as much as ids you want to delete from the table. Here we are going to explain this with example.


Laravel delete multiple rows

Deleting multiple records by passing array of ids as below-

Mass Delete in Laravel Example 1

Laravel delete multiple rows

$ids = array(10, 20, 30);
DB::table('table_name')->whereIn('id', $ids)->delete(); 

Mass Delete in Laravel Example 2

Using model you can also perform mass deletion as below –

Laravel delete multiple rows

$deletedRows = Users::where('id', '>', 500)->delete();

Laravel Load multiple views in same controller


Laravel Load multiple views in same controller : You can load multiple views in single controller function using the nest method. Here is simple example of loading multiple views from single controller method.


Laravel Load multiple views in same controller

You can load multiple views in laravel controller as below –

Laravel Load multiple views in same controller:

return View::make('header_template', $data)
->nest('left_content_view', $data2)
->nest('main_content_view', $data3)
->nest('right_content_view', $data4)
->nest('footer_template', $data5);

Thus you can load multiple views using the nest.

Laravel get current url


Laravel get current url : If you are working with laravel you often need to get current url and its parameters. You can use helper function URL::current(); to get the current url in laravel4, laravel5. Here we are going to demonstrate the method to get current url and how to get its parameter.


Laravel get current url Syntax

Here is simple syntax to get current url in laravel.

Laravel get current url Syntax

Route::get('/current/url', function()
{
return URL::current();
});

Laravel get url parameters

Here is simple syntax to get url parameters in laravel. Suppose you have following url www.xyz.com/home/user?page=2. Suppose you need to get the page value that is 2.

Laravel get current url Syntax

$segment = Request::segment(3);

Which will give the page string’s value from url.