How to view data retrieved in the controller in the view? - laravel

I'm trying to retrieve data in my controller using sqlsrv database connection and I want to view the result in my test.blade.php
public function index()
{
$cout_achat = DB::table('[DWH_SOVAC_PROD_KIT_LIFE_CYCLE]')
->select( DB::raw('SUM([MONTANT_LC]) as cout_achat'))
->get();
return view('test', ['test' => $cout_achat]);
}
and the code in the view
<html lang="{{ config('app.locale') }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Raleway:100,600"
rel="stylesheet" type="text/css">
</head>
<body>
echo {{$cout_achat}};
</body>
</html>
but when I try to access myapp/test I get : ** Use of undefined constant test - assumed 'test' (this will throw an Error in a future version of PHP) (View: C:\wamp64\www\projetSovac\resources\views\test.blade.php) –**

About 404 the route you have provided is completely ok, can you please tell us how you are calling this route.
And please double check any typo in controller name.
Now I would like to talk about some improvement in your code
First of all is that when you return the view like this in your controller
return view('test', ['test' => $cout_achat]);
The $cout_achat will be available with name $test in your blade file. So you should try to use the same naming convention like
return view('test', ['cout_achat' => $cout_achat]);
Or simple you can use compact() laravels helper function like this
return view('test', compact(cout_achat));
It will automatically pass $cout_achat in blade.
Now for echoing the variable in balde you don't need to use echo explicitly like
echo {{$cout_achat}};
You can echo variables like
{{$cout_achat}}
Anything else will be handled automatically by blade compiler.
Hope this will help.

Related

Pass the data to errors custom page on laravel

I did create a custom 404 page on my Laravel project by creating a folder called errors and inside the folder I did create a file called 404.blade.php
However when I use a component and I try to pass the data with that component I get an error
<x-main-header-component></x-main-header-component>
</head>
<body>
<!-- Main Header -->
<x-main-navbar-component></x-main-navbar-component>
<!-- ... end User Menu Popup -->
error message here
<x-main-footer-component :message="$SocialMediaLinks"></x-main-footer-component>
</body>
</html>
the error that I'm getting is:
ErrorException
Undefined variable $SocialMediaLinks (View:
What can I do to pass the data to the component main-footer on errors/404.blade.php?
There are more than one ways you can achieve this. Since we have no access to 404Controller or anything like so, I usually come up with several ideas:
Directly import the targetted data source (model for example). This will guarantee result, but the view may look ugly and not that logical.
Caching: Redis, Memcache or anything will help. If your error views are using the same data source as your landing pages (navigation bar, header for example). The idea is that you will use Cache::remember() to store and get data whereever you want. Cache
View composer: (Docs). Cleanest way in my opinion. You just share data to all views (error views included) using global * regular expression.
Use helper: Since helpers are globally available, you can use it to store and retrieve data as you want.
For JavaScript framework component availability, just include it (the .js file) in the error views under resources/views/errors.
Example for the 3rd method:
In your AppServiceProvider#boot:
view()->composer('*', function ($view) {
$view->with([
'somethingToShare' => "Hello world",
]);
});
In your error views:
{{dd($somethingToShare)}}
Now when you open your targetted error view, it should show "Hello World"
I'm assuming you are calling $SocialMediaLinks in your app or footer.
if you are, then use an helper function. i'll add my example below
#if (isset($seo))
<title>{{ $seo->title }}</title>
<meta name="description" content="{{ $seo->meta_description }}">
<meta name="keywords" content="{{ $seo->meta_keywords }}">
#else
#php
$seo = getSEO(); //helper function
#endphp
<title>{{ $seo->title }}</title>
<meta name="description" content="{{ $seo->meta_description }}">
<meta name="keywords" content="{{ $seo->meta_keywords }}">
#endif

i am new with sweetalert, could someone help me on how to use this package in laravel?

I found this link describing how to use sweet-alert in laravel.
Step 1
require the package using composer - I successfully downloaded the package
step 2
Usage - Imported the UxWeb\SweetAlert\SweetAlert in my controller.
public function index()
{
$departments = Department::all();
SweetAlert::message('Robots are working!');
return view('department.index')->with('departments', $departments);
}
The problem is when i reload department.index view, sweet-alert does not work!
Could someone help me with this? Either to solve this issue or give a suggestion to any other valid way to use sweet-alert?
Did you add sweet-alert to front-end?
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Include this in your blade layout -->
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
</head>
<body>
#include('sweet::alert')
</body>
</html>

Laravel Request cookie doesn't show JS created Cookies

I'm using JS-Cookie for setting a Cookie via the front-end.
Why can't I access it in my Laravel 5.8 application with:
dd(request()->cookies)? The Cookie is visible with the name, but the value is null. But when I do try to get the Value of the Cookie via the "normal" way, I do get the value: dd($_COOKIE['the-cookie-value-i-want']);.
How can I access the value of the Cookie via the "Laravel-way"? Which is more secure.
I know this is an old question, but you can disable encryption for specific cookies instead of disabling it for everything (which would break secure sessions).
Edit the \App\Http\Middleware\EncryptCookies class to include the following:
/**
* The names of the cookies that should not be encrypted.
*
* #var array
*/
protected $except = [
'my_frontend_cookie_name'
];
You can now access the cookie value via request()->cookie() or the Cookie::get() facade.
Only cookie created by laravel can handle by laravel.
Now here is the way you can achieve this
Go to : app > Http > Kernal.php : under $middlewareGroups array comment this
'web' => [
//\App\Http\Middleware\EncryptCookies::class,
And then try
Here is the demo
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
</head>
<body>
{{ Cookie::get('cookie1') }}
{{ request()->cookie('cookie1') }}
<script type="text/javascript">
document.cookie = 'cookie1=test; path=/'
</script>
</body>
</html>

Why can I not use a body class selector with Laravel Dusk?

I am using Laravel 5.5 and Dusk 2.0. I have the following html.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body class="my-body-class" id="my-body-div">
<div class="my-content-class" id="my-content-div">
Content goes here.
</div>
</body>
</html>
Here's my Dusk test.
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit('/test/admin-fixed-layout');
$this->assertNotNull($browser->element('.my-content-class'));
$this->assertNotNull($browser->element('#my-content-div'));
// $this->assertNotNull($browser->element('.my-body-class'));
$this->assertNotNull($browser->element('#my-body-div'));
});
}
If I un-comment the assertion that uses the body class selector, the test fails. Why?
This is because by default prefix is set to body:
public function __construct($driver, $prefix = 'body')
{
$this->driver = $driver;
$this->prefix = trim($prefix);
}
in Laravel\Dusk\ElementResolver class.
If you really need to change this (but probably there is no point), you can add the following method into Tests/DuskTestCase class:
protected function newBrowser($driver)
{
return new \Laravel\Dusk\Browser($driver, new \Laravel\Dusk\ElementResolver($driver, ''));
}
This will override default browser and pass empty prefix instead of default body prefix

An error when passing a parameter in a route laravel 5.2

this is my code
route.php
Route::get('test/{id}','testController#test');
testController.php
public function test($id){ return('test'); }
test.blade.php
<!DOCTYPE html>
<html>
<head>
<title>a</title>
</head>
<body>
<img src="myPath/myImage.jpg">
</body>
</html>
The result: the image cant be load
note: when i deleted the 'id' parameter it has worked perfectly
What is the error message you are getting?
You will also probably want to change the return value of testController#test to something along the lines of this so you can actually pass the variable to the view.
return view('test', [
'id' => $id,
])
You may also want to have env('APP_DEBUG',true) turned on to get verbose output
the solution : i have changed the value of the src attribute
<img src="{{URL::to('myPath/myImage.jpg')}}">
You need to use:
href="{{ URL::to('yourfile')}}". Also you should place your images in laravel public folder. If you will create folder img in your public folder then you need to something like this: href="{{ URL::to('img\imgname.png')}}".

Resources