Undefined variable
означает, что переменная не существует, и причина для вашего случая заключается в том, что вы не передали ее в представлении.
Обычно для получения записей customers
из базы данных в представленияхВы можете сделать это несколькими способами:
- Запросите его перед загрузкой своего представления, а затем передайте его своим представлениям:
//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.
Запрос прямо на ваш взгляд (Не очень рекомендуется, но иногда вам просто нужно заставить его работать)
В вашем customer.blade.php
@php
$customers = \App\Customer::get();
@endphp
<ul>
@foreach($customers as $customer)
<li>{{$customer->name}}</li>
@endforeach
</ul>
Мой советпопробуйте посмотреть несколько основных видеороликов Laravel, чтобы вы поняли последовательность запросов и ответов.