i'm trying to add users with specified roles into database , an error indicates
"ErrorException in 748460238d8f66b0fe4acb25c19f24f04b548bd9.php line 25:
Trying to get property of non-object (View: ..\views\admin\users\index.blade.php)".
below are the UserModel, index.blade.php and the controller.
What's wrong ?
Great thanks
<?php
namespace App\Http\Controllers;
use App\Role;
use App\User;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\UsersRequest;
class AdminUsersController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
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::lists('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)
{
//
User::create($request->all());
//return $request->all();
return redirect('/admin/users');
}
/**
* 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)
{
//
}
}
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token'
];
public function role(){
return $this->belongsTo('App\Role');
}
}
#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>
</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->is_active == 1 ? 'Active' : 'No active' }}</td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
</tr>
#endforeach
#endif
</tbody>
</table>
#stop
Migrations schema:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('roles');
}
}
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('role_id')->index()->unsigned()->nullabe();
$table->integer('is_active')->default(0);
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
In your blade file, try this:
#foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
#if($user->role)
{{ $user->role->name }}
#else
No role defined
#endif
</td>
<td>{{ $user->is_active == 1 ? 'Active' : 'No active' }}</td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
</tr>
#endforeach
Update function role() in User model like below:
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'password'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token'
];
public function role(){
return $this->belongsTo('App\Role','role_id');
}
}
Add role_id and is_active in $fillable inside User model to save those record in users table.
protected $fillable = [
'name', 'email', 'password','role_id','is_active',
];
Why don't you check what is on line 25 of 748460238d8f66b0fe4acb25c19f24f04b548bd9.php?. Find the file and check it.
My first assumption is that you're not using User::with('role')->get() instead of User::all(). You can't access $user->role->name without the relation loaded.
Related
i am trying to edit my categories via crud edit,
I think I did everything right both in the controller and in the routes but I keep getting error 404 page not found, as soon as I click on the button to change the category
this is my controller
<?php
namespace App\Http\Controllers;
use App\Models\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$category=Category::all();
return view('categorie.categorie', compact('category'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('categorie.crea');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $req)
{
$category = Category::create([
'nome'=>$req->nome
]);
return redirect()->back()->with('message', 'Categoria inserita correttamente');
}
/**
* Display the specified resource.
*
* #param \App\Models\Category $category
* #return \Illuminate\Http\Response
*/
public function show(Category $category)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Models\Category $category
* #return \Illuminate\Http\Response
*/
public function edit(Category $category,$id)
{
$category=Category::findOrFail($id);
dd($category);
return view('categorie.editcat', compact('category'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Category $category
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Category $category)
{
$category=Category::updated([
'nome'=>$request->nome,
]);
return redirect()->back();
}
/**
* Remove the specified resource from storage.
*
* #param \App\Models\Category $category
* #return \Illuminate\Http\Response
*/
public function destroy(Category $category)
{
//
}
}
this is route
Route::get('/categorie',[CategoryController::class,'index'])->name('categorie.categorie');
Route::get('/categorie/create',[CategoryController::class,'create'])->name('categorie.crea');
Route::post('/categorie/store',[CategoryController::class,'store'])->name('categorie.store');
Route::get('/categorie/store/{$id}',[CategoryController::class,'edit'])->name('categorie.edit');
Route::post('/categorie/update/{$id}',[CategoryController::class,'update'])->name('categorie.update');
and this is blade with form for edit
<x-layout>
<div class="container text-center">
<div class="row justify-content-center">
<div class="col-12 col-md-12 justify-content-center">
<h1>Categorie</h1>
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">NOME</th>
<th scope="col">STATO</th>
<th scope="col">AZIONI</th>
</tr>
</thead>
<tbody>
#foreach ($category as $cat)
<tr>
<th scope="row">{{$cat->id}}</th>
<th>{{$cat->nome}}</th>
<th>--</th>
<th><i class="fas fa-plus-circle"></i>
<form action="{{route('categorie.edit',$cat->id)}}" method="GET">
<button type="submit"><i class="fas fa-plus-circle"></i></button>
</form></th>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</x-layout>
any solution?
you have to change this uri '/categorie/store/{$id}' to '/categorie/store/{id}'
and do that in update route
I am having issue with livewire template rendering with Relationship-Eloquent with paginate method.
Livewire component:
<?php
namespace App\Http\Livewire;
use App\Models\User;
use Livewire\Component;
use App\Http\Controllers\AllUsersController;
use Livewire\WithPagination;
class DatatableComponent extends Component
{
use WithPagination;
/**
* #var parent object
* #property AllUsersController has to be replace using laravel Stubs
*/
protected $parent = AllUsersController::class;
/**
* #var int
*/
public $perPage = 1;
/**
* #var cities
*/
public $cities;
/**
* #var states
*/
public $states;
/**
* #var string
*/
public $page_title = 'Users';
/**
* #var string
*/
public $page_description = '';
/**
* #var array
*/
public $users;
/**
* This used to set the initial value of the parent model.
*
* #return default
*/
public function mount()
{
/** #todo initialize data using read method */
$this->read();
}
/**
* This used to initialize the data .
* #return initial object
* #todo This has to be replaced by the other parent controller objects
*/
protected function read()
{
/** #todo - set all public properties */
$this->parent = new $this->parent;
$this->states = $this->parent->getStates();
$this->cities = $this->parent->getCities();
$this->users = User::with(['logs' => function($query){
$query->with('distinctMeta');
}],'logsMeta','activity')->paginate($this->perPage);
}
/**
* This used to return component.
*
* #param void public property
* #return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function render()
{
return view('livewire.datatable-component');
}
}
Steps which i followed.
when i checked the $this->users its returning the user object. but while accessing public $users property on template its returning error.
Error :
Undefined property: Livewire\CompilerEngineForIgnition::$files
Template:
<div class="card card-custom">
<div class="card-body">
{{-- moving form to partials --}}
#include('livewire._partials.search')
{{-- end move --}}
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>{{ __('Email') }}</th>
</tr>
</thead>
<thead>
<tr>
#foreach($users as $index => $user)
<td>{{ $user->tualog->email }}</td>
#endforeach
</tr>
</thead>
</table>
{{ $users->links() }}
</div>
</div>
Main issue - is unable to use paginate without toArray() method. but when i used toArray() on template {{ $users->links() }} method not working.
if anyone solved this issue please help with that.
Thanks in Advance
as Remul stated, you can't assign objects to public properties in a livewire component. Remove the $users public property and instead assign it in the render() method:
public function render()
{
$users = User::with([
'logs' => function($query) {
$query->with('distinctMeta');
},
'logsMeta',
'activity'
])
->paginate($this->perPage);
return view('livewire.datatable-component', [
'users' => $users
]);
}
I have solved it by using local property for collection like this.
protected $collection
and assigning
$this->collection = $users = User::with([
'logs' => function($query) {
$query->with('distinctMeta');
},
'logsMeta',
'activity'
])
->paginate($this->perPage);
I have changed render method with
public function render()
{
return view('livewire.datatable-component',['users' =>$this->collection]);
}
I have a CRM Project to make, the problem is, when the other user created a project,
the project will display in project forms of all the users. How can i get the project of Authenticated Users.
THIS IS MY CONTROLLER
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Leads;
class LeadsController 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()
{
$arr['leads'] = Leads::all();
return view('leads.index')->with($arr);
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('leads.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request, Leads $leads)
{
$leads->Name = $request->Name;
$leads->Email = $request->Email;
$leads->Address = $request->Address;
$leads->contactnumber1 = $request->contactnumber1;
$leads->contactnumber2 = $request->contactnumber2;
$leads->contactnumber3 = $request->contactnumber3;
$leads->sourcelink = $request->sourcelink;
$leads->prevpublisher = $request->prevpublisher;
$leads->leadminer = $request->leadminer;
$leads->remarks = $request->remarks;
$leads->verifier = $request->verifier;
$leads->consultant = $request->consultant;
$leads->Save();
return redirect('leads');
}
/**
* 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)
{
//
}
}
THIS IS MY VIEW.
<table class="table table-bordered table-striped">
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
<th>ContactNo.</th>
<th>ContactNo.</th>
<th>ContactNo.</th>
<th>SourceLink</th>
<th>PrevPublisher</th>
<th>Leadminer</th>
<th>Remarks</th>
<th>Verifier</th>
<th>Consultant</th>
</tr>
#foreach($leads as $l)
<tr>
<td>{{ $l->Name }}</td>
<td>{{ $l->Email}}</td>
<td>{{ $l->Address }}</td>
<td>{{ $l->contactnumber1}}</td>
<td>{{ $l->contactnumber2 }}</td>
<td>{{ $l->contactnumber3 }}</td>
<td>{{ $l->sourcelink }}</td>
<td>{{ $l->prevpublisher }}</td>
<td>{{ $l->leadminer }}</td>
<td>{{ $l->verifier }}</td>
<td>{{ $l->consultant }}</td>
<td>Edit Delete</td>
</tr>
#endforeach
</table>
You need to restrict your $leads to only the ones from your authenticated user:
$arr['leads'] = Leads::where('user_id', '=', Auth::id())->get();
Assuming your have a user_id column
so here is my question, i have done everything and my senior request me to replace the parent_id now into description as the coder know what is the integer number represent but the users doesn't know. Here is the picture
My current view looks like !
As you can see inside the red column, there are two id :
1.( 999162, Testing3, Test3, 999161, active ) and
2.( 999163, testing4, test, 999162, active )
My desired output is the 1.( 999161 calls the 999161 description instead of id ).
Lets take 999163 as example : the desired output should be like 999163, testing4, test, test3, active.
I don't know how to call the description to replace the parent_id,can someone help ?
<div class="row">
<div class="col-md-12">
<br />
<h3 align="center">Category Data</h3>
<br />
#if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{$message}}</p>
</div>
#endif
<div align="right">
Add
<br />
<br />
</div>
<table class="table table-bordered table-striped">
<tr>
<th>Id</th>
<th>Code</th>
<th>Description</th>
<th>Parent</th>
<th>Status</th>
<th>Action</th>
<th>Action</th>
</tr>
#foreach($category as $row)
<tr>
<td>{{$row['id']}}</td>
<td>{{$row['code']}}</td>
<td>{{$row['description']}}</td>
<td>{{$row['parent_id']}}</td>
<td>{{$row['status']}}</td>
<td>Edit</td>
<td>
<form method="post" class="delete_form" action="{{action('categoryController#destroy',$row['id'])}}">
{{ csrf_field() }}
{{ method_field('DELETE')}}
<input type="hidden" name="_method" value="DELETE" />
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
#endforeach
</table>
</div>
</div>
Here is my categoryController.php coding
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
class categoryController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$category =Category::all()->toArray();
return view('category.index',compact('category'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$parents = Category::all();//DB::table("bit_app_policy_category")->lists("name","id");
//Category::all();
return view('category.create',compact('parents'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'code' => 'required',
'description' => 'required',
'parent_id' => 'required',
'status' => 'required',
]);
$category = new Category([
'id' => $request->get('id'),
'code' => $request->get('code'),
'description' => $request->get('description'),
'parent_id' => $request->get('parent_id'),
'status' => $request->get('status'),
]);
$category->save();
return redirect()->route('category.create')->with('success', 'Data Added');
}
/**
* 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)
{
$category = Category::find($id);
return view('category.edit', compact('category','id'));
$parents = Category::all();//DB::table("bit_app_policy_category")->lists("name","id");
//Category::all();
return view('category.create',compact('parents'));
$parents = Category::all();//DB::table("bit_app_policy_category")->lists("name","id");
//Category::all
return view('category.edit',compact('parents'));
}
public function subRequest()
{
return view('subRequest');
}
public function subRequestPost()
{
$input = request()->all();
return response()->json(['success'=>'Got Submit Request.']);
}
/**
* 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,[
'code' =>'required',
'description' =>'required',
'parent_id'=>'required',
'status'=>'required'
]);
$category = Category::find($id);
$category->code =$request->get('code');
$category->description =$request->get('description');
$category->parent_id =$request->get('parent_id');
$category->status =$request->get('status');
$category->save();
return redirect()->route('category.index')->with('success','Data Updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$category = Category::find($id);
$category->delete();
return redirect()->route('category.index')->with('success','Data Deleted');
}
}
Category.php picture
Category.php picture
Suppose you have a Category model, add blew code to your Category model class.
public function parent()
{
return $this->belongsTo(Category::class, 'parent_id', 'id');
}
Then, replace {{$row['parent_id']}} with {{$row->parent->description}} in your code.
This is the answer
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$tmp =Category::all()->toArray();
$category = array();
foreach ($tmp as $key => $row) {
$policy = Category::find($row['parent_id']);
$tmpResult = new Category();
$tmpResult-> id =$row['id'];
$tmpResult-> code =$row['code'];
$tmpResult-> description =$row['description'];
$tmpResult-> parent_id =$policy['description'];
$tmpResult-> status =$row['status'];
array_push($category, $tmpResult);
}
return view('category.index',compact('category'));
}
What's the problem? I have records in DB.
The ressource route is Route::resource('dylan_dog', 'DylanDogController');
I looped each $dylan_dog record, provided by the controller(further below), in index.blade.php like this:
#foreach ($dylan_dog as $ddog)
<tr>
<td>{{ $ddog -> id }}</td>
<td>{{ $ddog -> name }}</td>
<td>{{ $ddog -> title }}</td>
<td>{{ $ddog -> biography }}</td>
<td>Edit</td>
<td>
<form action="{{ route('dylan_dog.destroy', $dylan_dog->id) }}" method="post">
#csrf
#method('DELETE')
<button class="btn btn-danger" type="submit" name="delete">Delete</button>
</form>
</td>
</tr>
#endforeach
What is the catch?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Dylan_dog;
class DylanDogController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$dylan_dog = Dylan_dog::all();
return view('dylan_dog.index', compact('dylan_dog'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('dylan_dog.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name'=>'required',
'title'=> 'required',
'biography' => 'required'
]);
$dylan_dog = new Dylan_dog([
'name' => $request->get('name'),
'title'=> $request->get('title'),
'biography'=> $request->get('biography')
]);
$dylan_dog->save();
return redirect('/dylan_dog')->with('success', 'Thanks for your contribution');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//return view('dylan_dog.index', ['name' => Dylan_dog::findOrFail($id)]);
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$dylan_dog = Dylan_dog::find($id);
return view('dylan_dog.edit', compact('dylan_dog'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$request->validate([
'name'=>'required',
'title'=> 'required',
'biography' => 'required'
]);
$dylan_dog = Share::find($id);
$dylan_dog->name = $request->get('name');
$dylan_dog->title = $request->get('title');
$dylan_dog->biography = $request->get('biography');
$dylan_dog->save();
return redirect('/dylan_dog')->with('success', 'Character has been updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$dylan_dog = Share::find($id);
$dylan_dog->delete();
return redirect('/dylan_dog')->with('success', 'Character has been deleted successfuly');
}
}
Error message "ErrorException (E_ERROR) Property [id] does not exist
on this collection instance. (View:
C:\xampp\htdocs\miljan\resources\views\dylan_dog\index.blade.php)"
Just noticed you are using $dylan_dog->id. $dylan_dog is a Collection and doesn't have id as attribute.Change it to $ddog->id:
<td>Edit</td>
<td>
<form action="{{ route('dylan_dog.destroy', $ddog->id) }}" method="post">
#csrf
#method('DELETE')
<button class="btn btn-danger" type="submit" name="delete">Delete</button>
</form>
</td>
You should change the route paramters aswell to $dogg.