Pages

Showing posts with label laravel. Show all posts
Showing posts with label laravel. Show all posts

How to pass a variable in route using javascript and ajax in laravel

Get the value of the using the id, define the route of the laravel with id, looking for passing to route to controller as per following.

 

 var $val = $("#pid").val();
 var ur = '{{ route("ajx.subproduct",["id" =>  ":val"]) }}';
 var url =ur.replace(':val', $val);

complete tutorial

 html>
   <head>
      <title>Laravel Ajax Example</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
	<script>
	    $(document).ready(function () {
		$("#mdivsubproduct").hide();

		$("#pid").change(function (e) {
		    e.preventDefault();
		    var $val = $("#pid").val();
		    var ur = '{{ route("ajx.subproduct",["id" =>  ":val"]) }}';
		    var url =ur.replace(':val', $val);
		      var data = 'pid=' + $val + '&feature=pin';
		        $.ajax({
		            url: url,
		            data: data,
		            type: 'get',
		            success: function (output) {
		                 $("#mdivsubproduct").show();
		                $("#divsubproduct").html(output);
		                
		            }
		        })
		   
		});
	    });   
	</script>      

   </head>
   
   <body>
	Product :
	 <select name="pid" id="pid" class="form-control">
	    <option value="">Select Product</option>
	     @foreach($products as  $product)
		 <option value="{{ $product->pid }}">{{ $product->name }}</option>
		  @endforeach
	</select>

	Sub Product :
	<div class="col-md-6 col-sm-6" id="divsubproduct">
		                   
	</div>

   </body>

</html>                        
web.php

Route::get('/associate/ajxsubproducct/{id}', 'ProductController@Ajaxsubproduct')->name('ajx.subproduct');


In Controller add this function

ProductController.php

/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function Ajaxsubproduct(Request $request, $id)
    {

        $pid = $id;
        $Subproducts = DB::table('sub_products')->where('pid', '=', $pid)->get();
        
        $data ='<select name="sub_pid" id="sub_pid" class="form-control">
        <option value="">Select Product</option>';
            foreach($Subproducts as  $product){
                $data.='<option value="'. $product->pid.'">'. $product->name .'
                </option>';
            }
         $data.='</select>';
        
        
         return $data;
      
    }

LARAVEL Running Raw SQL Queries

In Laravel, Once you have configured your database connection, you may run queries using the  DB facade. The DB facade provides methods for each type of query: select, update,  insert, delete, and statement.

laravel : Accessing The Request from URL using get method


To obtain an instance of the current HTTP request via dependency injection,you should type-hint the Illuminate\Http\Request class on your controller method.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index(Request $request)
    {
  echo "==>".   $name = $request->input('name');
    }
}

web.php

Route::get('/category', 'CategoryController@index')->name('category');

http://127.0.0.1:8000/category?name=test

Laravel : How to call controller index function using routes

In Laravel  Let's go with the example using controller name is Product

1. Create controller in Controllers case sensitive name so, create like a example given example : ProductController.php

//controller path
<project_name>/app/Http/Controllers/<controller_name.php>
Example product controller

//controller path
<project_name>/app/Http/Controllers/ProductController.php

Laravel : setup laravel in localhost with linux

Laravel setup in localhost before we need basic requirement to fulfill laravel works, you will need to make sure your server meets the following requirements:
PHP >= 7.1.3
OpenSSL PHP Extension
PDO PHP Extension
Mbstring PHP Extension
Tokenizer PHP Extension
XML PHP Extension
Ctype PHP Extension
JSON PHP Extension
BCMath PHP Extension

Installing Laravel

Laravel utilizes Composer to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.

Laravel run/access without php artisan

Laravel is Framework of php language,  its access through command php artisan server but when we run the project using live/domain name then..

its

http://localhost/<project_name>/public/



Popular Posts