Can not seem to successfully pass through a variable through to a laravel Route - laravel

I am a complete newb trying to teach myself the laravel framework. I have a little PHP experience from a past life and am just trying to learn a new skill set. I am def not a developer by trade.
I am trying to complete a tutorial on routes.
In my web.php file I have this
Route::get('/test/{post}', function($post) {
$posts = [
'my-first-post' => 'Hello, this is my first blog post!',
'my-second-post' => 'Now I am getting the hang of this blogging thing',
'test' => 'test'
];
if (! array_key_exists($post, $posts)) {
abort(404, 'Sorry, that post was not found');
}
return view('post', [
'post' => $posts[$post]
]);
});
In my blade file I have this
<!DOCTYPE HTML>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
This is a test
</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
this is a test
{{$post}}
</div>
</div>
</div>
</body>
</html>
When I refresh the page with the following URL http://127.0.0.1:8000/test/test
I get the following error
InvalidArgumentException
View [post] not found.
Am i doing something silly?

Through the exception it seems that the view 'post' does not exist.
Please make sure that post.blade.php exists in project_root/resources/views/post.blade.php
For further details please refer to this link:
https://laravel.com/docs/7.x/views#creating-views

Related

How to use laravel-vue-i18n to change translations in laravel blades also?

Can anyone help me make translations working in laravel blades too?
I need to change/translate the contents of the title and description meta depending on the language being switched.
This is how the language changes in Vue:
<template>
<ul class='language-switcher'>
<li :class="{'active': language === 'de'}">
De
</li>
<li :class="{'active': language === 'en'}">
En
</li>
</ul>
</template>
<script>
import {loadLanguageAsync} from "laravel-vue-i18n";
export default {
name: "LanguageSwitcherComponent",
data() {
return {
language: 'en',
};
},
methods: {
switchLanguageTo(lang) {
// ...
loadLanguageAsync(lang);
// ...
},
},
}
</script>
In Vue, the translations work fine, but I need to translate title and description meta in index.balde.php as well.
<!DOCTYPE html>
<html lang="{{str_replace('_', '-', app()->getLocale())}}">
<head>
<title>{{ __("Site Title") }}</title>
<meta name="description" content="{{ __("Site Info") }}">
<meta charset="utf-8">
<meta name="csrf-token" content="{{ csrf_token() }}"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="{{mix('css/app.css')}}">
</head>
<body>
<div id="app">
<index-component/>
</div>
<script src="{{mix('js/app.js')}}"></script>
</body>
</html>
Also, this is how configured i18n in app.js:
import {i18nVue} from 'laravel-vue-i18n';
// ...
app.use(i18nVue, {
lang: 'en',
resolve: lang => import(`../../lang/${lang}.json`),
});
Thanks!
Save site language in cookies and in web.php set locale from cookies.
Route::get('/{any}', function (Request $request) {
// Set locale
App::setLocale($request->cookie('site_language') ?: 'en');
return view('index');
})->where('any', '.*');
Note: Maybe will need to add cookie name in app/Http/Middleware/EncryptCookies.php in $except list to get cookie from the $request successfully.

How to use Stripe Element Into Laravel Blade

I have a Laravel project. I want to add Stripe payment system in my Laravel app. In stripe there is js Element but unfortunately I cannot use it in blade files. Can anyody help me suggest other way to do this?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Stripe Payment</title>
</head>
<body>
<!-- Mount the instance within a <label> -->
<label>Card
<div id="card-element"></div>
</label>
<!--
Or create a <label> with a 'for' attribute,
referencing the ID of your container.
-->
<label for="card-element">Card</label>
<div id="card-element"></div>
<script src="https://js.stripe.com/v3/"></script>
<script>
var stripe = Stripe('pk_test_....');
const elements = stripe.elements();
const cardElement = elements.create("payment", {"clientSecret": "sk_test_...."});
cardElement.mount('#card-element');
</script>
</body>
</html>

i have used laravel blade template make layout but doent work

Template:
<!DOCTYPE html>
<html>
<head>
<title>App Name #yield('title')</title>
</head>
<body>
#section('content')
</div>
</body>
</html>
View:
#section('content')
#if(session('message'))
<p>{{session('message')}}</p>
#endif
#foreach ($posts as $post)
<h2> {{$post->title}}</h2>
<p>{{$post->body}}</p>
Edit
Delete
#endforeach
#endsection
I have used this code and get white screen how to solve this problem
make layout folder and layout file get only white screen no any error and output
MASTE PAGE:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- Tell the browser to be responsive to screen width -->
<meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
<meta name="csrf-token" content="{{csrf_token()}}">
#yield('metas')
#yield('head')
#yield('style')
</head>
<title>#yield('title')</title>
<body class="skin-blue sidebar-mini">
#yield('body')
</body>
<footer>
#yield('footer')
#yield('script_whole')
</footer>
</html>
CHILD PAGES:
#extends('path.to.master_page')
#section('head')
//head elemetns here
#endsection
#section('style')
//style here
#endsection
#section('title', 'my_title')
#section('content')
//content elemetns here
#endsection
#section('footer')
//footer elemetns here
#endsection
#section('script_whole')
//scripts here
#endsection
and now every page that extends from MASTER_page, so should use #section
Instead of using
#section('content');
in main blade file use
#yield('content');

ng-admin doesn't initialize in another ui-view

I have faced a problem that ng-admin is not rendering inside simple ui-view.
Here is the code:
$stateProvider
.state('home', {
url: '/',
template: homeTemplate
})
.state('login', {
url: "/login",
template: loginTemplate,
controller: 'LoginController',
controllerAs: 'vm'
})
.state('register', {
url: "/register",
template: "<div>Register is under maintenance</div>",
})
;
And here is Html:
<html ng-app="myApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Admin-tool</title>
<meta name="google" value="notranslate">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--<link rel="shortcut icon" href="favicon.ico" />-->
</head>
<body ng-strict-di class="main-content">
<div id="content-body" class="content">
<div ui-view=""></div>
</div>
<pg-footer></pg-footer>
</body>
Also there is an info message
WARNING: Tried to load angular more than once.
Maybe this is the case? Could you please point me out why it doesn't even render ng-admin simple page?
In browser it just looks like:
<!-- uiView: -->
<div ui-view="" class="ng-scope"></div>
Well, ng-admin has it's own routing so it was needed to make login rout and default on which ng-admin initialize. If it's needed to make custom pages inside ng-admin you just need to specify that in your state like this: parent: 'ng-admin'

Blade Template with comment on top not working

file: app/route.php
Route::get('/', function()
{
return View::make('home');
});
file: app/views/home.blade.php
{{-- Blade comment. --}}
#extends('layouts.base')
#section('head')
<link rel="stylesheet" href="second.css" />
#stop
#section('body')
<h1>Heading</h1>
<p>Hello Home!</p>
#stop
file: app/views/layouts/base.blade.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
#section('head')
<link rel="stylesheet" href="style.css" />
#show
</head>
<body>
#yield('body')
</body>
</html>
When I access to laravel.localhost/
It only output
#extends('layouts.base')
but however, if I remove the
{{-- Blade comment. --}}
then it works perfectly.
May I know what is the issue?
The first line in your extended blade view must be the #extends directive.
Yes it is a convention by the devs.
Look at BladeCompiler.php on line 119.
protected function compileExtends($value)
{
// By convention, Blade views using template inheritance must begin with the
// #extends expression, otherwise they will not be compiled with template
// inheritance. So, if they do not start with that we will just return.
if (strpos($value, '#extends') !== 0)
{
return $value;
}

Resources