show module value in template laravel - 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

Related

Laravel: Error when trying to use Str::limit in views

I am getting this error when trying to use the Str::limit in views
ErrorException
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$body (View: C:\Users\USER\Desktop\laravels\qna\resources\views\questions\index.blade.php)
here is the code
<div class="media-body">
<h3 class="mt-0">{{ $question->title }}</h3>
{{ Str::limit($questions->body, 250) }}
</div>
Here is the controller
namespace App\Http\Controllers;
use App\Models\Question;
use Illuminate\Http\Request;
// use Illuminate\Support\Str;
class QuestionsController extends Controller
{
// use Str;
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$questions = Question::latest()->paginate(5);
return view('questions.index', compact('questions'));
}
...
}
I get his error when "Str" is un-commented
Symfony\Component\ErrorHandler\Error\FatalError
App\Http\Controllers\QuestionsController cannot use Illuminate\Support\Str - it is not a trait
What is the proper method to use Str:: in a view
Write $question->body instead of $questions->body in your view in order to use the object of question not the paginator.
Actually you don't have to use Illuminate\Support\Str in your controller at all , because you use Str class only in your view and it's one of the aliases in laravel , take a look at config/app.php.
By the way … The (use statement) above class only shorten the namespace you must use in your code like so :
use Illuminate\Support\Str;
class QuestionsController extends Controller
{
public function index()
{
Str::limit("Some String");
}
}
But if you don't put this use , your code would be :
class QuestionsController extends Controller
{
public function index()
{
\Illuminate\Support\Str::limit("Some String");
}
}
whereas when we put use statement inside class , it means we are trying to use trait in our class
https://www.php.net/manual/en/language.oop5.traits.php
I don't think you need to add it in your controller, so just add
use Illuminate\Support\Str;
to your model and that should allow you to use it anywhere. And then in your blade you can use
\Illuminate\Support\Str::limit($questions->body, 250)
This is a Laravel solution but I do recommend looking at this thread for a pure PHP answer Limit String Length
string limit with the end of three dots
\Illuminate\Support\Str::limit($clientName,16,'...');

Laravel Page Route Not Using The Correct Parameters Passed In

Hey all you smart people,
Im having a issue I normally work with API routes not really used Web Routes before and finding this rather complicated for some reason :D
Ive made this route
Route::get('/test/{page?}', \App\Http\Livewire\Test::class);
sand this is my logic in the render() in the controller
public function render(Request $request, $page = 1)
{
dd($page);
}
however when I'm on the browser and type
http://url.com/test/2
The Die Dump keeps giving me page 1 all the time am i missing something here ??
Thanks for the help if anyone can help...
Update
Im not sure if its because I'm using a livewire component and not an actual controller....
Livewire Component
<?php
namespace App\Http\Livewire;
use Illuminate\Http\Request;
use Livewire\Component;
class Test extends Component
{
public function render(Request $request, $page = 1)
{
dd($page);
return view('livewire.test');
}
}
Route Parameters in livewire works like this
web.php
Route::get('/test/{page?}', \App\Http\Livewire\Test::class);
component
public function mount($page = 1)
{
dd($page);
}
ref link https://laravel-livewire.com/docs/2.x/rendering-components#route-params

Define a variable not explained

Sorry for the dumb question but can anyone please tell me how to define a variable in very simple terms? I have struggled for several months with "undefined variable" errors. Are variables stored in config? Or maybe in routes?
I have a database with a customers table. When I put this on my view home page {{$customers->name}} I get Undefined variable: customers.
Fine. So how and where do I define a variable. I would have thought it WAS defined given that the database table is literally called customers. Ugh!
My model file Customer.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $fillable = ['name', 'phone'];
public function address()
{
return $this->hasOne(CustomerAddress::class);
}
public function purchases()
{
return $this->hasMany(CustomerPurchase::class);
}
}
Undefined variable means the variable does not exist and the reasons for your case is, you did not pass it in the view.
Usually, to get the customers records from the database to your views, you can do it in several ways:
Query it prior to loading your view then pass it to your views:
//doing it in the controller
//create a controller: php artisan make:controller CustomerController
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller as BaseController;
use App\Customer; //Dont forget to import your Customer model
class CustomerController extends BaseController
{
public function index()
{
$customers = Customer::get(); //this will fetch the customer using your mdoel
return view('customer', ['customers' => $customers]); //this will pass the records to the view
}
}
//then in your routes/web.php:
Route::get('/customers', 'CustomerController#index'); //when you go to your application/customers in the browser, it will go to the controller and return the view with the records.
//OR you can skip the controllers and do it in the routes/web.php directly as what #jitesh jose mentioned.
Query straight into your view (Not really recommended, but sometimes you just need to make it work)
In your customer.blade.php
#php
$customers = \App\Customer::get();
#endphp
<ul>
#foreach($customers as $customer)
<li>{{$customer->name}}</li>
#endforeach
</ul>
My advice, try to watch a few basic Laravel videos so that you will understand the flow of the request and response.
If your model name is Customer,laravel automatically pick the table name as customers.Otherwise you have to use your desired table name in Model as follows.
protected $table = 'customers_table';
In your web.php
Route::get('/home',function () {
$customers = DB::table('customers_table')->get();
OR
$customers = Customer::get();
return view('welcome')->with('customers',$customers);
});
You can use$customers in welcome.blade.php as
#foreach($customers as $customer)
{{$customer->name}}
#endforeach

Laravel: a simple MVC example

I'm new to Laravel and the documentation's basic task list returns Views from the Route(web.php) but I want to use a Controller to return an image file.
So I have for my route:
Route::get('/products', 'ProductController#index');
Then my ProductController action (please ignore comments as I'm using index to simplify things):
<?php
namespace App\Http\Controllers;
use App\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
#return \Illuminate\Http\Response
Fetch and return all product records.
*/
public function index()
{
//
//return response()->json(Product::all(), 200);
return view('/pages/product', compact('product'));
}
And my product.blade.php (nested in views/pages/product):
<img src="/images/product/Frozen_Ophelia_800x.png">
I keep getting a ReflectionException Class App\Product does not exist.
I got this working when I just returned a view from the route. I'm getting a ReflectionException
Class App\Product does not exist so I think it's something at the top, ie. use App\Product; that is wrong.
Edit (below is my App\Product nested in app/Providers):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Product extends Model
{
//
use SoftDeletes
protected $fillable = [
'name', 'price', 'units', 'description', 'image'
];
public function orders(){
return $this->hasMany(Order::class);
}
}
Assuming App\Product model exists, correct code should be:
public function index() {
$product = Product::all();
return view('pages.product', compact('product'));
}
Check the docs.
PS did you call a $ composer dumpautoload? ReflectionException Class error is often related to new class autoloading (eg. new classes in a packages)
view function should have any view template not any url or route. Of you have file views/pages/product.blade.php then use
view('pages.product',compact('product'));

Laravel(500- Internal server error): Unable to get data from model to controller

I have been trying to list a dropdown in the index page with data from database. I created a model and made some changes in controller to display it in my view page but making any change in the controller gives a blank page with 500 Internal server error in the console. Please help me out to sort this problem.
Table name: walker_type
Routes:
Route::get('/', 'WebController#index');
Model: ProviderType.php :
<?php
class ProviderType extends Eloquent {
protected $table = 'walker_type';
}
Controller: WebController.php
public function index() {
$walkerTypeList = ProviderType::all();
return view('website.index')->with(['walkerTypeList' => $walkerTypeList]);
}
View:index.php
#foreach ($walkerTypeList as $car)
<option data-icon="glyphicon-road" value="{{ $car->name }}"> {{ $car->name }} </option>
#endforeach
Had u declare your model below your namespace?
eg. use App\WalkerType;
also you forgot to declare a namespace to your Model.
it should have namespace App;
or if you have a folder for your model to make it more conventional.
you should have a namespace on each of your model.
eg. App\Model
and then use that in every controllers by declarin in between your namespace and class
eg.
namespace App\Controllers;
use App\Model\WalkerType;
class SomeController extends Controller{
protected $data; //this is a class variable that can call anywhere to your class by calling it this way $this->data
public function some_method(){
$this->data['variable_a'] = "some_value"; //this can call in you view later by $variable_a
$this->data['sum'] = 1+4; //result can be display in your view by calling the variable $sum
return view('someview',$this->data);
}
}
I hope this can help you for your project efficiently, cause we had experienced that we forgot to include some of the data that has been processed on the controller and needed to display in your view file but forgot to include.

Resources