how to pass laravel session to vue.js - laravel

i want to pass langguageid session from laravel to my vue component.
how i can do it?
i try like this, but not working.
my laravel session function
public function getLanguageSession($langid)
{
if (Session::has('LangSession')) {
Session::forget('LangSession');
$LangSession = Session::put('LangSession', $langid);
$getLangSession = Session::get('LangSession');
} else {
$LangSession = Session::put('LangSession', $langid);
$getLangSession = Session::get('LangSession');
}
return redirect('/');
}
and then i call it to vue function
public function getLanguageStringArray(Request $request, $langid)
{
$getLangSession = Session::get('LangSession');
dd($getLangSession);
}
this code return me null value.

You should not return json data from a controller using dd($getLangSession);, this will give you a totally different output that cannot be parsed by Vue. You have to return the data as a json result to make sure Vue can read this:
public function getLanguageStringArray(Request $request, $langid)
{
$getLangSession = Session::get('LangSession');
return repsonse()->json($getLangSession);
}

Related

Which design pattern helps me in this case using - Laravel

so I have a controller which returns a view with a DB Collection which I have a repository,
now, in case I have a request I want to return an array - json and not a view
any ideas if this is the correct approach?
public function index(
Request $request,
CampaignPerformanceRepository $campaignPerformanceRepository
) {
$data = $campaignPerformanceRepository->getDataByPeriod($request);
if ($request->all()) {
return $campaignPerformanceRepository->getDataByPeriod($request);
}
return view('reports.campaign-performances', compact('data'));
}
I do something similar in cases where I may want to return a view or just json based on the request. There's an easy helper function request()->wantsJson() which you can use.
$data = $campaignPerformanceRepository->getDataByPeriod($request);
if ($request->wantsJson()) {
return $campaignPerformanceRepository->getDataByPeriod($request);
}
return view('reports.campaign-performances', compact('data'));

Laravel - pass value to registration form

I couldn't find the route to the /register form in laravel. What I wanted to do is to add a variable that is passed..
mysite/register?referral=123456
when you load the page the referral textbox with id="referral" should be prefilled. How can this be done?
you can check the request param
public function register()
{
$referral = request()->get('referral');
if (empty($referral)) {
$referral = '';
}
return view('register', compact('referral'))
}

How to pass data from return()->redirect()->back()?

I want to add data to the database selecting from the dropdown.
After adding the data it redirects me to the same page.
i want select dropdown to select the same data that i selected before inserting data in database.
I didn't allow me by passing value from return redirect back.
Are there any alternatives?
public function district()
{
$data['districts'] = District::all();
$data['locations'] = Location::paginate(6);
return view('Backend.pages.dashboard.district',$data);
}
public function addingdistrict(Request $request)
{
if($request->isMethod('get'))
{
return redirect()->back()->with('danger','Not with URL please');
}
if($request->isMethod('post'))
{
$data['district_id'] = $request->districtname;
$data['location'] = $request->locationname;
if(Location::create($data))
{
return redirect()->back();
}
}
}
i want to pass data from addingdistrict return redirect()->back()
redirect()->back()->with('message','your message')->with(compact('your_varible'));

Laravel cookie in serviceprovider not comparable

I try to pass a variable based on a cookie value in my compose function to all my view to build my menu, with the use of serviceproviders recommmended here:
File: Providers/ViewComposerServiceProvicer.php
public function boot(Request $request) { $this->composeTopBar($request);}
public function composeTopBar(Request $request)
{
$cookieValue = $request->cookie('brand');
// if value not set use default value.
if($cookieValue == null)
{
$cookieValue = 1;
}
$brands = \App\Brand::orderBy('priority', 'asc')->get();
foreach($brands as $brand){
if($brand->id == $cookieValue){
$brand->menuActive = true;
}
else{
// show value to debug
$brand->menuActive = $cookieValue;
}
}
view()->composer('front.layouts.top', function ($view) use ($brands) {
$view->with('brandItems',$brands );
});
}
the cookieValue looks like
yJpdiI6IlNJODBvQ1RNM004OWVleyJpdiI6IlNJODBvQ1RNM004OWVleyJpdiI6IlNJODBvQ1RNM004OWVl
While the value in my controller looks like '2' How can i get the original value 2 in my compose function?
I need to get the original value to compare it in my composeTopBar function so I can pass a variable to be true if it equals the cookie value.
Method to set cookie
$response = response()-> view('front.products.category', compact('products','category'));
$response->withCookie(cookie()->forever('brand',1));
return $response;
I ended up using a class based composer .
The reason why this works is because it's called later in the lifecycle of laravel and the Cookie variables are decrypted. When using Closure based composers the values are encrypted.
Try this: put the view() call as a parameter to response().
$response = response(view('front.products.category', compact('products','category')));
$response->withCookie(cookie()->forever('brand', 1));
return $response;

Simple AJAX / JSON response with CakePHP

I'm new to cakePHP. Needless to say I don't know where to start reading. Read several pages about AJAX and JSON responses and all I could understand is that somehow I need to use Router::parseExtensions() and RequestHandlerComponent, but none had a sample code I could read.
What I need is to call function MyController::listAll() and return a Model::find('all') in JSON format so I can use it with JS.
Do I need a View for this?
In what folder should that view go?
What extension should it have?
Where do I put the Router::parseExtension() and RequestHandlerComponent?
// Controller
public function listAll() {
$myModel = $this->MyModel->find('all');
if($this->request->is('ajax') {
$this->layout=null;
// What else?
}
}
I don't know what you read but I guess it was not the official documentation. The official documentation contains examples how to do it.
class PostsController extends AppController {
public $components = array('RequestHandler');
public function index() {
// some code that created $posts and $comments
$this->set(compact('posts', 'comments'));
$this->set('_serialize', array('posts', 'comments'));
}
}
If the action is called with the .json extension you get json back, if its called with .xml you'll get xml back.
If you want or need to you can still create view files. Its as well explained on that page.
// Controller code
class PostsController extends AppController {
public function index() {
$this->set(compact('posts', 'comments'));
}
}
// View code - app/View/Posts/json/index.ctp
foreach ($posts as &$post) {
unset($post['Post']['generated_html']);
}
echo json_encode(compact('posts', 'comments'));
// Controller
public function listAll() {
$myModel = $this->MyModel->find('all');
if($this->request->is('ajax') {
$this->layout=null;
// What else?
echo json_encode($myModel);
exit;
// What else?
}
}
You must use exit after the echo and you are already using layout null so that is OK.
You do not have to use View for this, and it is your wish to work with components. Well all you can do from controller itself and there is nothing wrong with it!
Iinjoy
In Cakephp 3.5 you can send json response as below:
//in the controller
public function XYZ() {
$this->viewBuilder()->setlayout(null);
$this->autoRender = false;
$taskData = $this->_getTaskData();
$data = $this->XYZ->getAllEventsById( $taskData['tenderId']);
$this->response->type('json');
$this->response->body(json_encode($data));
return $this->response;
}
Try this:
public function listAll(){
$this->autoRender=false;
$output = $this->MyModel->find('all')->toArray();
$this->response = $this->response->withType('json');
$json = json_encode($output);
$this->response = $this->response->withStringBody($json);
}

Resources