hulapoua nusaka hulapoua nusaka - 6 months ago 145
Ajax Question

Ajax, laravel - call function from controller in blade template

How to call a function


storeSessionData( $name , $value) @sessionController


from


partList.blade


with ajax?

I am quite confused, what route should i use to call ajax function if i have mutiple urls to 1 template. Maybe anyone would be so kind and write me an example how to do it?

SessionController

public function storeSessionData( $name , $value){




switch ($name) {
case 'cpu':
\Session::put($name,$value);
break;
case 'cpu_cooler':
\Session::put($name,$value);
break;

case 'case':
\Session::put($name,$value);
break;

case 'motherboard':
\Session::put($name,$value);
break;

case 'psu':
\Session::put($name,$value);
break;

case 'memory':
\Session::push($name,$value);
break;

case 'sound_card':
\Session::push($name,$value);
break;

case 'wireless_card':
\Session::push($name,$value);
break;

case 'wired_card':
\Session::push($name,$value);
break;


case 'storage':
\Session::push($name,$value);
break;

case 'optical_drive':
\Session::push($name,$value);
break;

case 'gpu':
\Session::push($name,$value);
break;

case 'case_fan':
\Session::push($name,$value);
break;

}

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

}


Partlist.Blade

@foreach ($parts as $part=>$value)
@foreach ($value as $desc => $val)


<th><a href="?sort={{$desc}}&order=asc">{{trans('specifications.' .$desc)}}</a></th>
@endforeach
@break
@endforeach
</thead>
<tbody>

@foreach ($parts as $part => $value)
<tr>
@foreach ($value as $desc => $val)


@if($val == reset($value))
<td class="part-link" ><a href="{{$slug}}{{$value->slug}}">{{$val}} </a></td>
@elseif ($val == end($value))

<td><a class="button button-primary" href="" // Call ajax function on click {{$session_name}} {{$value->slug}} >Pievienot </a></td>

@else

<td data-title="{{trans('specifications.' .$desc)}}:">{{$val}}</td>
@endif
@endforeach
</tr>

@endforeach


partlist controller

class partListController extends Controller
{
public function showAllCpu(Request $request){

// cpu query
$parts = // part query
$parts = $parts->paginate(30);

$data = [
'title' => '',
'parts' => $parts,
'slug' => 'procesors-',
'bgImg' => 'cpubg',
'headTitle' => 'Intel un AMD Procesori',
'headParagraph' => 'Izvēlies jaunākos un izcilākos procesorus',
'session_name' => 'cpu',


];

if ($parts->currentPage() <= $parts->lastpage()) {
return view('partlist', $data);
} else{
abort(404, 'Lapa nēeksistē');
}
}




public function showAllMemory(Request $request){


// part query
$parts = // part query
$parts = $parts->paginate(30);

$data = [
'title' => 'Izvēlies operatīvo atmiņu',
'parts' => $parts,
'slug' => 'operativa-atmina-',
'bgImg' => 'memorybg',
'headTitle' => 'Operatīvā atmiņa',
'headParagraph' => 'Paātrini sava datora ātrdarbību',
'session_name' => 'memory',
];

if ($parts->currentPage() <= $parts->lastpage()) {
return view('partlist', $data);
} else{
abort(404, 'Lapa nēeksistē');
}
}

Answer

Here is a simple example :

Routes.php

Route::post('sessions/{name}/{value}', ['as' => 'session.store', 'uses' => 'SessionController@storeSessionData']);

SessionController.php

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

class SessionController extends Controller
{
    public function storeSessionData($name, $value) {
        // Do something with $name and $value

        return ['name' => $name, 'value' => $value];
    }
}

partlist.blade.php

<html>
  <head>
    ...
    <meta name="csrf-token" content="{{ csrf_token() }}" />
    ...
  </head>
  ...
  <td><a class="button button-primary save-session" data-url="{{ route('session.store', ['name' => $session_name, 'value' => $value->slug]) }}">Pievienot</a></td>
  ...
</html>

Javascript

$('.save-session').on('click', function(){
    var token = $('meta[name="csrf-token"]').attr('content'),
      $this = $(this),
      url = $this.data('url');

    $.post(url, { _token: token }).done(function(msg) {
      alert(msg);
    });
});