Display name of current blade file - php

I would like to have an h1 tag that displays the current file name.
For example, in the blade file "index.blade.php", I would like to have an h1 tag that displays "index.blade.php".
I have tried doing using {{__FILE__}} but that prints out the cached blade file and not the actual name
// index.blade.php
<h1>{{__FILE__}}</h1>
Result:
project/storage/framework/views/eweijo29398hr23.php
Desired Result: /index.blade.php

Here is still working solution:
How can I get the current view name inside a master layour in Laravel 4?
View::composer('*', function($view){
View::share('view_name', $view->getName());
});

#php
app('events')->listen('composing:*', function ($view, $data = []) {
echo last(explode('/', $data[0]->getEngine()->getCompiler()->getPath()));
});
#endphp
It's slightly messy but works!

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 }}

On what basics we give url and name in laravel route file

You may find me stupid but i am unable to understand on what basics we give url and name in our route file
Example:
Route::get('/order/getOrders', 'OrderController#getOrders')-
>name('order.getOrders')->middleware('auth');
can anyone please tell me.
and if we take url on the basics of where our file in view folder like( order->getorder blade file)
Then what if my path is layouts.site.topbar
In view instead of pages, my file is in layouts.
EDIT:
blade file
<a href="{{ route('sync.index') }}">
#if(isset($syncs))
#foreach ($syncs as $sync)
#endforeach
{{ $sync->session_date }}
#endif
</a>
controller file
class TopbarController extends Controller
{
public function index()
{ die('o');
$syncNames = Sync::select('session_date','session_time')->where('user_id',$user_id)->get();
return view('layouts.site.topbar', array(
'syncs' =>$syncNames
));
}
public function sync_finish_session() {
die('s');
$user_id = Auth::id();
$sync_date = date('M d ',strtotime("now"));
$sync_time = date('M d, Y H:i:s',strtotime("now"));
$sync = Sync::where('user_id',$user_id)->get();
if(count( $sync) > 0) {
Sync::where('user_id',$user_id)->update(['session_date'=>$sync_date,'session_time'=>$sync_time,'user_id'=>$user_id]);
}
else {
$dates = new Sync();
$dates->session_date = $sync_date;
$dates->session_time = $sync_time;
$dates->user_id = $user_id;
$dates->save();
}
return $sync;
}
}
web file
Route::post('/sync_finish_session', 'TopbarController#sync_finish_session')->name('sync_finish_session')->middleware('auth');
Route::get('/sync/index', 'TopbarController#index')->name('sync.index')->middleware('auth');
Now whats the problem its giving nothing even i put die but its not going in controller file.
I think this is more a personal preference thing than that there are rules.
The convention I use is name(<model>.<action>)
This way i can create routes like
Route::get('/users/{id}/view', 'UserController#view')->name('users.specific.view')->middleware('auth');
You just name route as you do want. There is no strict rules how to name route. You can change name('order.getOrders') to name("anyName") and use new name in templates.
As the Laravel documentaton about rounting says:
Named routes allow the convenient generation of URLs or redirects for specific routes.
So, you can use this name to generate URLs or redirects. For example:
You could put this in your web.php file:
Route::get('/image/index', 'API\SettingsController#index')->name('image.index');
And call that route like this in your view:
Le met see that index!
Where the {{ route('image.index') }} references the name you gave to it.
You can name your route(s) anything you want. If you wanted, you could call your above route "mySuperCoolRouteName":
Route::get('/order/getOrders', 'OrderController#getOrders')-
>name('mySuperCoolRouteName')->middleware('auth');
and later in a view file you can use this name as a "shorthand" to get/print the URL of that route:
To My Cool Route
will be rendered to
To My Cool Route

Load data in view in Laravel

I have a simple controller function that fetch all records from db. but when i am trying to show all these records it show nothing. In fact it shows me hard coded foreach loop like this.
#foreach ($compactData as $value) {{ $value->Name }} #endforeach
this is my contoller function.
public function showallProducts()
{
$productstock = Product::all()->stocks;
$productoldprice = Product::all()->OldPrices;
$productcurrentprice = Product::all()->CurrentPrice;
$compactData=array('productstock', 'productoldprice', 'productcurrentprice');
return view('welcome', compact($compactData));
}
this is my view
<!doctype html>
<html lang="{{ app()->getLocale() }}">
<head>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<div class="title m-b-md">
Laravel
</div>
<div class="title m-b-md">
All products
</div>
<table>
<tbody>
#foreach ($compactData as $value)
{{ $value->Name }}
#endforeach
</tbody>
</table>
</div>
</div>
</body>
why it is behaving like this. any solution?? I am using phpstorm version 17. Is their any setting issue to run project because what ever project I ran it gives me the only page which i ran with only html?
My route is.
Route::get('/', function () {
$action = 'showallProducts';
return App::make('ProductController')->$action();
});
Have you checked your $compactData variable? Please dd($compactData) to see what it contains.
Problem 1
You are accessing a relational property as a property of Eloquent collection, like this:
Product::all()->stocks
which is not correct. Because the Collection object doesn't have the property stocks but yes the Product object might have a stocks property. Please read the Laravel documentation about Collection.
Problem 2
$compactData = array('productstock', 'productoldprice', 'productcurrentprice');
This line creating an array of 4 string, plain string not variable. So, your $compactData is containing an array of 4 string. If you want to have a variable with associative array then you need to do the following:
$compactData = compact('productstock', 'productoldprice', 'productcurrentprice');
Problem 3
return view('welcome', compact($compactData));
Here you are trying to pass the $compactDate to the welcome view but unfortunately compact() function doesn't accept variable but the string name of that variable as I have written in Problem 2. So, it should be:
return view('welcome', compact('compactData'));
Problem 4
Finally, in the blade you are accessing each element of the $compactData data variable and print them as string which might be an object.
You most likely have a problem with your web server.
Try to use Laravel Valet as development environnement.
Edit : I found this : Valet for Windows
I think you didn't mention the blade in the name of the view file by which it is saved. So change the name of the file by which it is save to something like:
filename.blade.php
and try again.
Explanation:
#foreach ($compactData as $value) this is the syntax of blade template engine, and to parse and excute it, you have to mention the blade extension in the name.

Using Laravel View Composer to format page titles

I am trying to use a View composer to automatically modify page titles based on a defined section.
#section('title', 'Page')
And use it like this:
<title>{{ $title }}</title>
I've written the following code as the composer, but it doesn't work correctly. It just displays the title as Website - Website when it should say Page - Website or, in the case of not including a title, Website.
View::composer('*', function($view){
$title = $view->title;
$view->with('title', !empty($title) ? $title . " - Website" : "Website");
});
This only needs to work on one template, master, but when I replaced '*' with 'master' it didn't modify the behaviour at all.
What do I need to change to make this correctly modify the title section?
You can use
view()->composer('*', function ($view) {
$view->with('title', 'Your title');
});
In service provider.
Or you can use share
view()->share('key', 'value');
Then in view use
{{ $key }}
May be try to use this:
view()->composer('*', function ($view) {
$data = $view->getData();
//here you can get your data which was sent to view
//exmp. $old_title = $data['title'];
//where 'title' - key of variable which sent via ->with('title','data')
$view->with('title', 'new title');
});

empty anchor tag inspite of loading value in blade syntax

In my store cotroller i have the following function defined :
public function getProductspg() {
return View::make('store.products')->with('product' , Product::all());
}
now in my main.blade.php , which is the index page, i have the following link:
<li>products</li>
But however when i refresh my index page, I.E. main.blade.php , which is the page which has the above link , i still see the following:
<li>products</li>
Why ?
try using:
<li>products</li>
and in the routes file create a route to your function:
Route::get('products', 'StoreController#getProductspg');

Categories