We noticed you're using an ad blocker

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Top 10 question-answer for New Laravel developer

world cup 2022

FootballDesk

Get update standing EPL, UEFA, LaLiga and more

Get Now

The Laravel Framework is the most popular PHP framework in the current PHP community. For its popularity, new PHP developers are getting involved with Laravel Framework. Every new Laravel Developer has lots of how-to questions about Laravel Framework but here I'm going to discuss the top 10 asked question's and it answers. So that It helps you do know those questions answers if you are new in Laravel Framework.

 

Top 10 asked question-answer for new Laravel Developer

 

1. How to run a raw query in Laravel?

Answer: Laravel offers you Eloquent model and query builder to perform database query but sometimes we need to run a raw query because it makes our life easier rather than converting those raw queries into Eloquent query or query builder. To run a raw query in Laravel follow the example given below.

$sql = "select * from users u left join subscriptions s on u.id=s.user_id where s.subs_type='premium'";

$result = \DB::select($sql); 

Note: This example is a simple raw query which can be easily doable with eloquent query or query builder but the actual raw query can be more complex or bigger than it.

 

2. How to use a custom table name in the eloquent model?

Answer: If our eloquent model name is Post then Laravel will find a table in our database posts (which is plural name of our model name) but sometimes we need to change our table name with prefix name or something like that for security reason or our development strategy. That time we can easily set our table name in our eloquent model so that we don't have to change our model name according to our model name. See the given example below.

// model name Post and table name lp_post

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
   protected $table = 'lp_post';
} 

 

3. How to disable created_at, updated_at field?

Answer: Laravel by default look for two columns in every table which created_at, updated_at. If these fields are not present in our table then laravel gives an error during data insertion or update. If we don't need these fields in our certain table we can tell laravel to ignore those fields. Let's check the example given below.

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
   public $timestamps = false;
} 

 

4. How to remove public from URL?

Answer: For removing the public word from URL follow these steps given below.

  1. Move the index.php file from the public directory to root directory.
  2. Now open the index.php file and replace this line  require __DIR__ . '/../bootstrap/autoload.php'; to require __DIR__ . '/bootstrap/autoload.php'; and $app = require_once __DIR__ . '/../bootstrap/app.php'; to $app = require_once __DIR__ . '/bootstrap/app.php';
  3. Save and done! Keep in mind every time when you use any CSS, Js or image you have to add a public word in your asset helper function. Example: asset('public/css/style.css')

 

5. How to disable CSRF token in specific routes?

For security reason Laravel check CSRF ( Cross-Origin Resource Forgery)  token for every POST request. If you need to disable the CSRF token for specific routes then follow the steps given below.

  1. Open app\Http\Middleware\VerifyCsrfToken.php file.
  2. Now add those route path which you want to whitelist for CSRF token checking in the protected $except = [ ]

Example:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{

    protected $addHttpCookie = true;
    protected $except = [
        '/admin/products/autocomplete',
        '/admin/services/*'
    ];
}

 

6. How to insert data by jQuery Ajax?

Answer: To send ajax post request for saving data, updating data or deleting. We need to send the _token field also for Laravel CSRF token checking validation. See the example given below.

In our HTML markup, we have to add a hidden field name with _token

<input type="hidden" name="_token" value="{{csrf_token()}}">

or use only CSRF blade directive

@csrf

In our javascript part

$(function(){

	 $('#form').submit(function(e){
	 	e.preventDefault();

	 	var _url = $(this).attr('action'),
	 		_type = 'POST',
	 		_data = $(this).serialize();

	 	$.ajax({
		    url: _url,
		    type: _type,
		    data: _data,
		    success: function (data) { 
		        // do something after success
		    }
		});

	 })

});

 

7. How to upload image/file in Laravel?

Answer: To upload an image or file follow this example given below. For ajax uploading you can read this post on Laravel Ajax image upload tutorial.

HTML

<input type="file" name="photo">

In controller

$photo = $request->file('photo');
if ($request->hasFile('photo')) {
    $fileName = time() . "." . $photo->getClientOriginalExtension();
    $request->file('photo')->move(public_path('/photos'), $fileName);
    
    // after upload finished you can save the filename in the database
    
}

 

8. How to make a CRUD app in Laravel?

Answer: For new laravel developer, making a CRUD application is good for practice. You can read all post on Laravel CRUD category.

 

9. How to make authentication and disable registration in Laravel?

Answer: To make authentication read the post on Laravel authentication tutorial. If you don't need to the registration system in your application then follow the steps given below to disable registration system in Laravel application.

  1. Open your route file web.php and find the route Auth::routes();
  2. Pass register equal false into auth routes. Auth::routes(['register' => false]); 
  3. Done!

 

10. How to deploy a Laravel project into cPanel shared hosting

Answer:  For reading a step by step tutorial on how to deploy Laravel application in cPanel shared hosting you can read this post on Laravel share hosting application deployment.

 

Hopefully, this post will help you to know the most asked Laravel question's answer for a new Laravel developer. If this post helps you then please share this post with other so that they can be helped.

 

Share on




Related Post - Latest Post


Tinkerpad - A minimal Laravel code editor

What's new in Laravel 9

Make laravel site super fast by page-cache!

Laravel maintenance mode bypass by Secret Route!

Laravel database backup automatically - cPanel shared hosting, VPS

Laravel Datatables - Ajax, Column, Buttons, Customization