Laravel - variable and lang system - php

I will show an example, I'm not good at writing English and it's better for the comprehension.
Route::get('/', function () {
return view('home')->withSucces("#lang('home.account_create_confirm')");
});
Here is a route and here is the code from the page:
<div class="container">
<div class="alert alert-success">
{{ $success }}
</div>
</div>
The message "home.account_create_confirm" is "your account has been created", but when I go to the page, instead of this, the page display this:
#lang('home.account_create_confirm')
Screenshot to understand:
https://gyazo.com/623fd5899b95819b6196bbae0197b1d4
I am sorry for this, I know that I'm a beginner and it must be evident for must of you!
Thanks for the help!

#lang is a tag of blade template processor and is not available in your controller. use trans() instead:
Route::get('/', function () {
return view('home')->withSucces(trans('home.account_create_confirm'));
});

Related

How do I assign values to variables and then display it in the view with Laravel?

I'm new to Laravel, I'm trying to follow along with this tutorial
https://laravel.com/docs/7.x/blade#displaying-data
I want to assign a value to a variable and then display it in the view but I don't know where I should do it. I tried to add this snippet
Route::get('greeting', function () {
return view('welcome', ['name' => 'Samantha']);
});
to the web.php file and then display it in the view like this hello, {{name}} but I either get an error or just a plain text:
hello, {{name}}
I'm reading the documentation but I can't figure out where or how to assign values to variables and then display it.
My view named welcome.blade.php has this:
<h1>Example</h1>
Hello, {$name}
In my web.php file I have this:
<?php
use Illuminate\Support\Facades\Route;
Route::get('greeting', function () {
return view('welcome', ['name' => 'Samantha']);
});
It doesn't give me any error. Just shows Hello {$name} instead of Samantha
you are missing the $ sign in your blade.
hello, {{ $name }}

Laravel sessions with sweet alert Session Loops

I'm just confused about my code. But I really thought that my code is correct. I'm trying to use with() method in Laravel 5.1 and then return to a view, then the sweet alert appears if the session that has been set is exists. Please see my code below:
PageController.php
return redirect()->route('list.view')->with('sweetalert', 'List has been created!');
view.blade.php
#extends('layout.master')
#section('container')
#foreach($lists as $list)
<li>{{ $list->name }}</li>
#endforeach
#stop
master.blade.php
<div class="container">
// some markup here...
</div>
#if(Session::has('sweetalert'))
<script>
swal('Success!', '{{ Session::get('sweetalert') }}', 'success');
</script>
#endif
I only want it to appear once, but if I try to click the back button, the message appears again. I have also tried the ff. code but nothings change:
#if(Session::has('sweet'))
<script>
swal('Success!', '{{ Session::get('sweetalert') }}', 'success');
</script>
<?php Session::forget('sweetalert'); ?>
#endif
Little help here?
a flash message has to be trigerred otherwise it will not make sense as you will set it everytime for the view
however you can use this code wherever the trigger is
Please Note :- I am just trigerring it everytime
Route::get('/', function () {
session()->flash('testing', 'I see this'); // Please have this line inside the trigger so the session does not get created everytime the view is called
return view('welcome');
});
Hope this helps

Building an ecommerce site using Laravel: How do I view/route products based on their ID?

I followed a tutorial on Tutsplus about creating an ecommerce website using Laravel. The problem I'm having right now is when trying to route to a subfolder. In the tutorial, the instructor included a feature where you can view products by ID. And this is how he did it:
// StoreController.php
public function getView($id) {
return View::make('store.view')->with('store', Store::find($id));
}
This piece of code seems to be passing an id from the stores table. I think when a product is clicked, that's when the id is passed
// Routes.php
Route::controller('store', 'StoreController');
Also some of the templates:
// store\index.blade.php
<h2>Stores</h2>
<hr>
<div id="stores row">
#foreach($stores as $store)
<div class="stores col-md-3">
<a href="/store/products/view/{{ $store->id }}">
{{ HTML::image($store->image, $store->title, array('class' => 'feature', 'width'=>'240', 'height' => '127')) }}
</a>
<h3>{{ $store->title }}</h3>
<p>{{ $store->description }}</p>
</div>
#endforeach
</div><!-- end product -->
So.. How it goes is when I click on a product, it leads me to domain:8000/store/view/6 where 6 is the id.
This works fine but what I want to know is how do I route through a subfolder? Let's say I want it to be like this: store/view/products/6 considering that I have a folder called products and my view.blade.php is inside that like this: store/products/view.
In my StoreController class, I tried changing this
public function getView($id) {
return View::make('store.view')->with('store', Store::find($id));
}
to this
public function getView($id) {
return View::make('store.product.view')->with('store', Store::find($id));
}
but it does not seem to work giving me nothing but a Controller Method Not Found Error.
First, the view name View::make('store.product.view') has nothing to do with the URL.
You have to change the route:
Route::controller('store/view', 'StoreController');
And then adjust the name of your method in the controller because it should be the same as the segment of the URL after store/view
public function getProducts($id) {
return View::make('store.product.view')->with('store', Store::find($id));
}
I strongly recommend you read the Laravel docs on the topic

Can you add a class/id to a session in laravel?

I'm creating a website with laravel and when a user edits their details it will access the controller update them and redirect to the 'edit' page using this
return Redirect::to('/member/editprofile')->with('message', 'Information Changed');
this part works fine, it redirects and sends the message which is printed out on the page with this
{{ Session::get('message') }}
I was wondering if there was a way to add a class to the session? I'm probably missing something completely obvious here and this is what I tried...
{{ Session::get('message', array("class" => "success")) }}
//added the class as you would with a HTML::link
any help is appreciated, thanks in advance!
You want this
<div class="success">{{ Session::get('message') }}</div>
In Laravel-4, the Session::get accepts two arguments :
$value = Session::get('key', 'default');
$value = Session::get('key', function() { return 'default'; });

Laravel 4: Using flexible layouts within routes

I have an application without controllers and read about controller layouts in laravel 4 documentation and this other article too, but I don't know where to start for implement it within routes (version 4), how can I do that?
Error received: InvalidArgumentException, View [master] not found.
app/routes.php
<?php
View::name('layouts.master', 'layout');
$layout = View::of('layout');
Route::get('users/create', array('as' => 'users.create', function() use($layout) {
//#TODO: load view using 'layouts.master',
// desirable: append 'users.create' and 'users.menu' views to sidebar and content sections.
//return View::make('users.create');
return $layout->nest('content', 'master');
}));
?>
app/views/layouts/master.blade.php
<html>
<body>
#section('sidebar')
This is the master sidebar.
#show
<div class="container">
#yield('content')
</div>
</body>
</html>
app/views/users/create.blade.php
{{ Form::open() }}
{{ Form::text('name') }}
{{ Form::submit('submit') }}
{{ Form::close() }}
app/views/users/menu.blade.php
<!-- This is appended to the master sidebar -->
<p>Create user</p>
Update: I modified example code to clarify what I want to do. Check app/routes.php and its comments
The code in your routes file is trying to nest the master layout within itself, which isn't really what you want. You're getting the error because 'master' would look for app/views/master.blade.php. That's easily fixed by changing it to 'layouts.master', but I wouldn't like to think what might happen...
The root cause of the issue you're having is the difference between "yielding" views from a Blade template, and nesting them from a route. When you nest a route, you need to echo it rather than using the #yield tag.
// File: app/routes.php
View::name('layouts.master', 'layout');
$layout = View::of('layout');
Route::get('users/create', array('as' => 'users.create', function() use ($layout)
{
return $layout
->nest('content', 'users.create')
->nest('sidebar', 'users.menu');
}));
/*
|--------------------------------------------------------------------------
| View Composer
|--------------------------------------------------------------------------
|
| Code in this method will be applied to all views that use the master
| layout. We use that to our advantage by injecting an "empty" sidebar
| when none is set when returning the view. It will error otherwise.
|
*/
View::composer('layouts.master', function($view)
{
if (!array_key_exists('sidebar', $view->getData()))
{
$view->with('sidebar', '');
}
});
// File: app/views/layouts/master.blade.php
<html>
<body>
#section('sidebar')
This is the master sidebar
{{ $sidebar }}
#show
<div class="container">
{{ $content }}
</div>
</body>
</html>
Laravel's View composers are a powerful tool. If you have any data (eg logged-in user info) used by all views that share the same template(s), you can use the composers to save injecting the data every time you load the view.
You could also use the #parent tag to append content, assuming you;re using blade for templating. E.g. (in the view)
#section('sidebar')
#parent
<p>This is appended to the master sidebar.</p>
#stop
You don't need to use nesting views if you're using blade.
app/views/users/create.blade.php
You need to extend the master.blade
#extends('layouts.master')
#section('content')
// form stuff here
#stop
Now, all you need to do is call create.blade
return View::make('users.create')
Just throwing this out there as a possible solution using controller routing (whereas you can set the template from within the controller).
app/routes.php
Route::controller('something', 'SomethingController');
app/controllers/SomethingController.php
class SomethingController extends BaseController {
protected $layout = "templates.main"; // denotes views/templates/main.blade.php
public function getIndex() { // the "landing" page for "/something" or "/something/index"
$this->layout->content = View::make('something.index')->with("myVar", "Hello, world!"); // load in views/something/index.blade.php INTO main.blade.php
}
public function getTest() { // for "/something/test"
$this->layout->content = View::make('something.index')->nest("widget", "something.widget", array("myVar" => "Hello, World!"));
}
}
app/views/templates/main.blade.php
#include('templates.partials.header')
#yield('something')
#yield('content')
#include('templates.partials.footer')
app/views/something/widget.blade.php
I'm a widget. {{ $myVar }}
app/views/something/index.blade.php
#section('something')
I will go in the 'something' yield in main.blade.php
#stop
#section('content')
I will go in the 'content' yield in main.blade.php.
{{ $myVar }}
{{ $widget }}
#stop
?>
Now you can test http://myserver/something and http://myserver/something/test to see the differences. Note: not tested but as a rough example.

Categories