Laravel blade variable not working - laravel

Heres the controller:
class HomeController extends BaseController {
public function index() {
return View::make('home', array('username' => 'Vaughan'));
}
}
Here the home blade view:
{{ $username }}
Yet the view is outputting exactly this:
{{ $username }}
The variable is not being rendered. Could this be something to do with Apache or PHP?

You need to name your view with blade extension:
home.blade.php
That's all.

Related

Livewire - Passing Data To View & Component from URL

I need to pass my data from the url and it works in the console log, but something happened to the view, it does not render the variable and the view loads just the footer, can anyone help me out please?
web routes:
Route::view('/aplicacion', 'application.visa-americana');
Route::get('/aplicacion/{id}', VisaUsaComponent::class);
layout:
#extends('layouts.app')
#section('content')
{{-- livewire interactions --}}
#livewire('applications.visa-usa-component')
#stop
Component:
use App\Models\User;
use Livewire\Component;
class VisaUsaComponent extends Component
{
public $post;
public function mount($id)
{
$this->post = User::findOrFail($id);
}
public function render()
{
return view('livewire.applications.visa-usa-component');
}
}
View:
<div>
{{ $post->name }}
</div>
try this:
public function render()
{
return view('livewire.applications.visa-usa-component')->layout('your layout address')->section('content');
}
Livewire use {{$slot}} for render and app.blade.php for layout, if you want change them, you must pass to render function
don't forget use #livewireScripts and #livewireStyles in app.layout.php

Eloquent model object not recognized_ laravel course excerice

Following an online course on Laravel I got stuck, and I do not see what I am doing wrong.
Can anyone see what I am doing wrong?
I have the following code in my project:
web.php
Route::get('/', function () {
return view('welcome');
});
Route::get('/pizzas', 'PizzaController#index');
Route::get('/pizzas/{id}','PizzaController#show');
Pizza.php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Pizza extends Model
{
use HasFactory;
// protected $tabel = 'pizzas';
}
PizzaController.php:
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Pizza;
class PizzaController extends Controller
{
//
public function index(){
$pizzas = Pizza::all();
return view('pizzas', [
'pizzas'=>$pizzas,
]);
}
public function show($id){
return view('details', ['id'=> $id]);
}
Pizza.blade.php:
<h1> Pizza List </h1>
#foreach($pizzas as $pizza)
<div>
{{ $pizza->$name }} // HERE IS WHERE THE ERROR OCCURS
</div>
#endforeach
Error message:
ErrorException
Trying to get property '' of non-object (View: D:\xxxxxxxxxxx\laravel\pizzahouse1\resources\views\pizzas.blade.php)
http://localhost:8000/pizzas
error in line 21
<h1> Pizza List </h1>
#foreach($pizzas as $pizza)
<div>
{{ $pizza->$name }} // HERE IS WHERE THE ERROR OCCURS
</div>
#endforeach
Why you are using "$name"?
Error says ,
You are calling for a property that doesn't exists in $pizza.
it should be something like following:
{{ $pizza->name }}
If problem still exists just :
dd($pizza);
Then see the results and call the property.
Hope it would help.

show module value in template laravel

I am new to Laravel I have install laravel 5.2 and running laraadmin.
I have added a 'header' module with field phone and want to show it in my blade template on my site.
Please help me.
my controller name headercontroller have the code
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
/**
* Class HomeController
* #package App\Http\Controllers
*/
class headerController extends Controller
{
public function index()
{
$header = headers::all();
return view('layout.aap')->with('headers', $header);
}
}
my view file name is "app.blade.php" inside a folder name "layout" have the following code
{{ $header -> phone }}
I cannot show the phone number on site please help me.
I didn't worked on laraadmin. But you can try this -
In your controller -
public function example(){
$data = Phone::all(); //Phone is model name. You make change according to your's
return view('xyz', compact('data'));
}
Now in your view you can access variable data.
#foreach($data as $d)
<span>$d->phone</span>
#endforeach

How to use index method to display a listing of the resource in Laravel

I created a customerController with Laravel resource. Now I want to show the list of the customers from the database using the index() method and it doesn't work when I visit the customer/index route. However if use the show() method instead and visit the customer/show route, it works perfectly.
Why does this behave this way and how do I get the index() method to do this?
class CustomerController extends Controller
{
public function index()
{
$customers = Customer::all();
return view('customer')->with('customers' , $customers);
}
public function show($id)
{
// adding the code in the index() method here makes the code run
// as expected
}
}
customer.blade.php
<ul>
#foreach($customers as $customer)
<li>{{$customer->name}}</li>
#endforeach
</ul>
routes/web.php
Route::resource('customer' , 'CustomerController');
I expect the output to be:
.sucre
.hameed
.micheal
I just learned how to use resource on Laravel 8.
Which version are you using?
Run php artisan route:list on your console to see every route available and their names.
To see all customers through your index function do the following:
public function index()
{
$customers = Customer::all();
return view('customer', [
'customers' => $customers
]);
}
Now just access your customer.blade.php file on browser.
PS: Don't forget of #foreach
#foreach ($customers as $customer)
{{ $customer->attribute }}
#endforeach
Hope this could be useful.

How to use a View Composer variable in the controller

I am trying to use the View Composer $view_name variable in same controller but it is giving me error.
It is only available to all views, but not for controllers
I want to use that variable in the same controller.
My controller:
public function boot()
{
view()->composer('*', function($view){
$view_name = str_replace('.', '-', $view->getName());
view()->share('view_name', $view_name);
});
$page_name=substr(strrchr(url()->current(),"/"),1);
if($page_name==request()->server('HTTP_HOST')) {
$keywords=keyword::where('page_name','index')->where('active','1')->first();
} else {
$keywords=keyword::where('page_name',$page_name)->where('active','1')->first();
}
}
You can create a Middleware to populate a Configuration variable, configuration is always available from all the framework.
public function handle()
{
config(['myCustomConfig.email' => 'me#example.com']);
}
On your controller
$data = config('myCustomConfig.email');
On your view file
<div>{{ config('myCustomConfig.email') }} </div>

Resources