Undefined variable of view in laravel 5.4 - php

I define a variable in view. But problem is that it shows undefined. My task is, first i take all the input from user using form helper then i want to post it to the store function & do some calculation and then pass it to the view for showing purpose. But i tried it, unfortunately shows undefined.Undefined variable is "totalamount" Here i attached all the code:
controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\TestSetup;
use Illuminate\Support\Facades\Input;
class TestRequestController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$allTestNames=TestSetup::all()->pluck('test_name','id');
// $testNames=TestSetup::orderBy('test_name')
// ->get();
return view('testrequestentries.createTestRequestEntry')->withAlltestnames($allTestNames);
// return view('testrequestentries.createTestRequestEntry');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// if(Input::get('Add')) {
$total=$request->fee+$total;
return view('testrequestentries.createTestRequestEntry')->withTotalamount($total);
// }else if(Input::get('Save')){
//
// }
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
view
#extends ('main')
#section('title','|Test Request Entry')
#section('stylesheets')
{!! Html:: style('css/parsley.css')!!}
#endsection
#section('content')
{!! Form::open(['route' => 'testrequests.store','data-parsley-validate'=>'']) !!}
{{Form::label('patient_name','Name of the Patient:')}}
{{Form::text('patient_name',null,array('class' => 'form-control','required'=>'', 'data-parsley-required-message' => 'Patient Name is required'))}}
{{Form::label('date_of_birth','Date Of Birth:')}}
{{Form::date('date_of_birth',null,array('class' => 'form-control','data-parsley-required' =>'true')) }}
{{Form::label('mobile_no','Mobile No:')}}
{{Form::text('mobile_no',null,array('class' => 'form-control','type'=> 'number','maxlength'=>'11','minlength' => '11','required'=>'', 'data-parsley-required-message' => 'Mobile No is required'))}}
{{Form::label('test_id','Select Test:')}}
{{Form::select('test_id',$alltestnames,null,['class'=>'form-control','required'=>'','placeholder' => 'Pick a Test...'])}}
{{Form::label('fee','Fee:')}}
{{Form::text('fee',null,array('class' => 'form-control','required'=>'', 'data-parsley-required-message' => 'Fee is required'))}}
{{Form::submit('Add',array('class' => 'btn btn-lg btn-block btn-success','style' => 'margin-top:10px;'))}}
<table class="table table-bordered" style="margin-top: 50px;">
<thead>
<tr>
<th>SL</th>
<th>Test Name</th>
<th>Fee</th>
<th>Type Name</th>
</tr>
</thead>
#php ($i=1)
<tbody>
<tr>
<th scope="row">{{$i}}</th>
<td>{{$totalamount}}</td>
</tr>
</tbody>
#php ($i++)
</table>
{!! Form::close() !!}
#section('scripts')
{!! Html:: script('js/parsley.min.js') !!}
#endsection
#endsection

I bet the view is returned from create() here, so change this:
return view('testrequestentries.createTestRequestEntry')->withAlltestnames($allTestNames);
To:
return view('testrequestentries.createTestRequestEntry', [
'alltestnames' => $allTestNames,
'totalamount' => $total
]);
In the store() method, do not return the view. Redirect back() or to index() or show() methods instead.

first thing first: check the value of $total=$request->fee+$total;
ie: put "dd('$total', $total);" right after the line you set it, before the return.
second: make it simplier return view(...)->with('totalAmount', $total);
test the value of $totalAmount in the view (BLADE file)
ie: {{dump('totalAmount', $totalAmount)}}
Anyway the store() method does NOT store anything ... WHY THAT?

Related

Create view in one controller and store data in other controller laravel

I'm new to Laravel and I'm trying to store a form. I created the view with the House controller but now I want to store the data in the view with the Booking controller. But when I click the button nothing happens.
My question is if it is possible to make a view with one controller and store it with another controller or maybe there is an other solution.
I also want to use the id of the house to store. How do I get that in the other controller as well?
Web Route
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', [\App\Http\Controllers\HouseController::class, 'index']);
Route::get('house/{house}', [\App\Http\Controllers\HouseController::class, 'show']);
Route::post('house/{house}', [\App\Http\Controllers\BookingController::class, 'store']);
Route::get('rental', [\App\Http\Controllers\HouseController::class, 'getUserHouses']);
Route::get('rental/new', [\App\Http\Controllers\HouseController::class, 'create']);
Route::post('rental/new', [\App\Http\Controllers\HouseController::class, 'store']);
Route::get('rental/edit/{house}', [\App\Http\Controllers\HouseController::class, 'edit']);
Route::put('rental/edit/{house}', [\App\Http\Controllers\HouseController::class, 'update']);
Auth::routes();
Booking controller
<?php
namespace App\Http\Controllers;
use App\Models\Booking;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class BookingController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$newBooking = Booking::create([
'user_id' => Auth::id(),
'house_id' => $request->id,
'begin' => $request->begin,
'end' => $request->end,
'status' => 0
]);
return redirect('/');
}
/**
* Display the specified resource.
*
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function show(Booking $booking)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function edit(Booking $booking)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Booking $booking)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Booking $booking
* #return \Illuminate\Http\Response
*/
public function destroy(Booking $booking)
{
//
}
}
House controller
<?php
namespace App\Http\Controllers;
use App\Models\house;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Helper\Imageable;
use DB;
class HouseController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$houses = House::all();
return view('/home', [
'houses' => $houses
]);
}
/**
* Display a listing of the houses the owner has
*
* #return \Illuminate\Http\Response
*/
public function getUserHouses()
{
$houses = DB::table('houses')
->where('user_id', '=', Auth::id())
->get();
return view('/rental/rental', [
'houses' => $houses
]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('rental/new');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$path = Imageable::storeMedia($request);
$request->online === 'on' ? $online = 1 : $online = 0;
$newHouse = House::create([
'title' => $request->title,
'price_per_night' => $request->price,
'summary' => $request->summary,
'place' => $request->place,
'country' => $request->country,
'user_id' => Auth::id(),
'online' => $online,
'image' => $path,
]);
return redirect('rental');
}
/**
* Display the specified resource.
*
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function show(house $house)
{
return view(
'/house',
[
'house' => $house
]
);
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function edit(house $house)
{
return view(
'rental/edit',
[
'house' => $house
]
);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function update(Request $request, house $house)
{
$path = Imageable::storeMedia($request);
$request->online === 'on' ? $online = 1 : $online = 0;
$house->update([
'title' => $request->title,
'price_per_night' => $request->price,
'summary' => $request->summary,
'place' => $request->place,
'country' => $request->country,
'online' => $online,
'image' => $path,
]);
return redirect('rental/edit/' . $house->id);
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\house $house
* #return \Illuminate\Http\Response
*/
public function destroy(house $house)
{
//
}
}
View
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-12">
<h1 class="display-one ">{{ $house->title }}</h1>
<p class=".text-light">{{ $house->place }}, {{ $house->country }}</p>
</div>
</div>
<div class="row mt-5">
<div class="col-sm-6">
<img src="{{ asset("img/houses/$house->image") }}" alt="{{ $house->title }}" class="img-fluid" />
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="exampleFormControlSelect1">Kies een datum en reserveer direct</label>
<form method="POST" action="">
#csrf
<input type="date" name="begin">
<input type="date" name="end">
<div class="col-md-12 bg-light mt-3">
<button type="button" class="btn btn-warning ml-2">Vraag aan</button>
</div>
</form>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-sm-6">
<p class="display-one ">{{ $house->summary }}</p>
</div>
<div class="col-sm-6">
<h2 class="display-one ">Aangeboden door</h2>
<p>Prijs per nacht €{{ $house->price_per_night }}</p>
</div>
</div>
</div>
#endsection
First of all, nothing happens when you click the form submit button because it is currently type="button" and in order this button to play role of submission button it must be type="submit". You can do whatever you want with Laravel. If you want your form to hit another controller method you can simply specify that in your form tag. Like so:
Imagine this is a form inside a view that is rendered by HouseController
<form method="POST" action="{{ url('/save/from/booking/controller') }}">
// ....
</form>
And now on form submission inside a view that is rendered by HouseController, you will actually hit a route that is BookingController responsive for. And here is your route that is being hit by the form
Route::post('/save/from/booking/controller', [BookingController::class, 'store']);

Problem displaying the data from my 'artists' table with Laravel

I do not manage to display the data from my database (I'm working with Laragon).
I already tried to change some names (variables etc.) here and there but I can't find where come from the problem.
I was able to display nothing first so I needed to change:
resources\views\artist\index.php -> resources\views\artists\index.blade.php
I probalby need to change more things in the code but I don't know where.
VIEW (views\artists\index.blade.php)
#extends('layouts.app')
#section('title', 'Liste des artistes')
#section('content')
<h1>Liste des {{ $resource }}</h1>
<table>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>
</thead>
<tbody>
#foreach($artists as $artist)
<tr>
<td>{{ $artist->firstname }}</td>
<td>{{ $artist->lastname }}</td>
</tr>
#endforeach
</tbody>
</table>
#endsection
ROUTES (routes\web.php)
<?php
Route::get('/', function () {
return view('welcome');
});
Route::get('artists', 'ArtistController#index');
CONTROLLER (Controllers\ArtistController.php)
<?php
namespace App\Http\Controllers;
use App\Artist;
use Illuminate\Http\Request;
class ArtistController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
/*
Take the artist from the db
and send it to a specific template
*/
$artists = Artist::all();
return view('artists.index',
[
'artists' => $artists,
'resource' => 'artistes',
]);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* #param \App\Artist $artist
* #return \Illuminate\Http\Response
*/
public function show(Artist $artist)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Artist $artist
* #return \Illuminate\Http\Response
*/
public function edit(Artist $artist)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Artist $artist
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Artist $artist)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param \App\Artist $artist
* #return \Illuminate\Http\Response
*/
public function destroy(Artist $artist)
{
//
}
}
DATABASE (database\ArtistsTableSeeder.php)
<?php
use Illuminate\Database\Seeder;
class ArtistsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$artists = [
['firstname'=>'Bob','lastname'=>'Sull'],
['firstname'=>'Marc','lastname'=>'Flynn'],
['firstname'=>'Fred','lastname'=>'Durand'],
];
foreach ($artists as $a) {
DB::table('artists')->insert([
'firstname' => $a['firstname'],
'lastname' => $a['lastname'],
]);
}
}
}
The only thing who get displayed on my browser when I go to 127.0.0.1:8000/artists is:
**Liste des Artistes**
Firstname Lastname
But nothing from the content of my Artistes table is displayed.
Thank you very much guys, the problems was that I needed to change the names of my columns...
FirstName LastName to Firstname Lastname
The data get displayed properly now.

Form action not correctly triggered in PHP Laravel 5.5

I am using the Forms provided by Laravel Collective and it is correctly installed.
What I'm trying to do is to validate the Form in PictureController.php
Just like this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Picture;
use DB;
class PictureController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$pictures = Picture::all();
return view('welcome')->with('pictures', $pictures);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('pictures.publicize');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'hashtag' => 'required',
]);
return 123;
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
And this is the code snippet of publicize.blade.php which contains this form.
{!! Form::open(array(
'action' => 'PictureController#store',
'method' => 'POST',
'files' => true
))
!!}
<div class="form-group">
{{ Form::label('hashtag', 'HashTag') }}
{{ Form::text('hashtag', '', ['placeholder' => 'Eg. #Happiness', 'class' => 'form-control', 'name' => 'hashtag' /*'required'*/]) }}
</div>
{{ Form::submit('Publicize', ['class' => 'btn btn-primary']) }}
{!! Form::close() !!}
And in the routes/web.php, this is the code written...
Route::resource('/', 'PictureController');
I have also submitted whole code on GitHub. Just look at this commit to take a look at all the files.
The problem is..
When I look into the Source Code in browser, I see that action attribute in the form equals the home url, i.e /public/. It looks like the form is not connected to the controller and method I specified in publicize.blade.php. How can I make that form work properly with validations?
Thanks for help in advance.
You have to give a name in route,
For example :
Route::resource('picture', 'PictureController');
And set on your blade like this,
{!! Form::open(array(
'action' => 'picture',
'method' => 'POST',
'files' => true
))
!!}
update route, web.php ( you should try this way )
Route::resource('/', 'PictureController');
update action attribute also,
{!! Form::open(array(
'action' => '/',
'method' => 'POST',
'files' => true
))
!!}
Source : here

Laravel: Trouble with update db after click button

I have problem: I build an app and I have 2 tables in my db: habits and users. The User has a field called points and after click a button 'Add point' I want to increment that value. I added a method in HabitsController called addPoints and button in a view, but I get an error message: MethodNotAllowedHttpException No message. There is my code.
HabitsController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Habit;
use App\User;
use DB;
use App\Quotation;
class HabitsController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
//store in $habits all posts of current user
$habits = DB::table('habits')->where('user_id', auth()->id())->get();
return view('habits.index')->with('habits', $habits);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('habits.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'name'=>'required',
'description'=>'required',
'difficulty'=>'required'
]);
$habit = new Habit;
$habit->name = $request->input('name');
$habit->description = $request->input('description');
$habit->difficulty = $request->input('difficulty');
$habit->NumberOfCompleted = 0;
$habit->user_id = auth()->user()->id;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
$single = Habit::find($id);
return view('habits.show')->with('single', $single);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$single = Habit::find($id);
return view('habits.edit')->with('single', $single);
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'name'=>'required',
'description'=>'required',
'difficulty'=>'required'
]);
$habit = Habit::find($id);
$habit->name = $request->input('name');
$habit->description = $request->input('description');
$habit->difficulty = $request->input('difficulty');
$habit->NumberOfCompleted = 0;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$single = Habit::find($id);
$single->delete();
return redirect('/habits')->with('success', 'Habit deleted');
}
public function addPoint(Request $request, $id)
{
$habit = User::find($id);
$habit->points += 1;
$habit->save();
return redirect('/habits')->with('success', 'Habit created');
}
}
show.blade.php
#extends('layouts/app')
#section('content')
<div class="row single">
<div class="col-sm-8 col-sm-offset-2 showSingleHabit">
<h2>{{$single->name}}</h2>
<p>{{$single->description}}</p>
<p>{{$single->difficulty}}</p>
<p>{{$single->NumberOfCompleted}}</p>
Edit
{!!Form::open(['action' => ['HabitsController#destroy', $single->id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
{!!Form::open(['action' => ['HabitsController#update', $single->id], 'method' => 'POST'])!!}
{{Form::hidden('_method', 'POST')}}
{{Form::submit('Add point', ['class' => 'btn btn-primary'])}}
{!!Form::close()!!}
</div>
</div>
#endsection
routes/web.php
Route::get('/', 'PagesController#index');
Route::get('/about', 'PagesController#about');
Route::resource('habits', 'HabitsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
I will be grateful for any help. :)
Change
{{Form::hidden('_method', 'POST')}}
To
{{Form::hidden('_method', 'PUT')}}

Trying to get property of non-object laravel 5.3, index.blade

I am stuck with this error in Laravel:
Trying to get property of non-object (View: C:...\resources\views\admin\users\index.blade.php)
Is anything wrong with my code?
This My index
#extends('layouts.admin')
#section('content')
<h1>Users</h1>
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Role</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
<th>Photo</th>
</tr>
</thead>
<tbody>
#if($users)
#foreach($users as $user)
<tr>
<td>{{$user->id}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->role->name}}</td>
<td>{{$user->active == 1 ? 'Active' : 'Not Active'}}</td>
<td>{{$user->created_at->diffForHumans()}}</td>
<td>{{$user->updated_at->diffForHumans()}}</td>
<td>{{$user->photo_id}}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
#stop
This my Controller
public function index()
{
//
$users = User::all();
return view('admin.users.index', compact('users'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
$roles = Role::pluck('name','id')->all();
return view('admin.users.create', compact('roles'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(UsersRequest $request)
{
//Buat bikin create dengan method ini
User::create($request->all());
return redirect('/admin/users');
// return $request->all();
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
return view('admin.users.show');
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
return view('admin.users.edit');
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
This my Request
class UsersRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
//
'name'=>'required',
'password'=>'required',
'email'=>'required',
'active'=>'required',
'role_id'=>'required',
'photo_id'=>'required|image|mimes:jpeg,png,jpg,gif,svg|max:7098',
];
}
}
This means (inside the controller)
$users = User::all();
returns empty object. There are no users in the db. The error probably comes from (admin/users/index.blade.php)
{{$user->id}}
and the other lines follow as well.
Before looping over the users, check if the data is empty. Correct this line
#if($users)
to look like this
#if($users->toArray())
It will return an empty array if there are no records for users and never enter in loop.
This will correct your error.
PS: Put an else statement for the #if statement with proper message so you can know it worked.
Please change this line
<td>{{$user->role->name}}</td>
to
<td>{{ $user->role ? $user->role->name : '-' }}</td>
Should work now... :)

Categories