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
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 m try to show data of specific user using show method(resource controller)
but getting 404 error.index,create and store functions are working fine.also
getting id in URL but its showing 404 error while calling show function
same error with edit,update and destroy.I try without resource controller and its was working perfectly.
here is my code,
<table class="table table-striped table-inverse table-responsive">
<thead class="thead-inverse">
<tr>
<th>id</th>
<th>name</th>
<th>email</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
#foreach ($data as $item)
<tr>
<td scope="row">
{{$item->id}}</td>
<td>
{{$item->name}}</td>
<td>
{{$item->email}}</td>
<td>edit</td>
</tr>
#endforeach
</tbody>
</table>
code of resource controller,there is no issue with index,create and store function
only getting 404 error when i pass id as a parameter
<?php
namespace App\Http\Controllers;
use App\crud;
use Illuminate\Http\Request;
class CrudController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$data=crud::all();
return view("index",compact("data"));
}
/**
* 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\crud $crud
* #return \Illuminate\Http\Response
*/
public function show(crud $crud)
{
//
return "show";
}
/**
* Show the form for editing the specified resource.
*
* #param \App\crud $crud
* #return \Illuminate\Http\Response
*/
public function edit(crud $crud)
{
//
return "edit";
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\crud $crud
* #return \Illuminate\Http\Response
*/
public function update(Request $request, crud $crud)
{
//
return "update";
}
/**
* Remove the specified resource from storage.
*
* #param \App\crud $crud
* #return \Illuminate\Http\Response
*/
public function destroy(crud $crud)
{
//
return "destroy function is working";
}
}
Route
Route::resource('user/', 'CrudController');
Your route definition is malformed. What you are passing into the first argument is a resource name, which can be a URI, but the last part of it is the resource name.
Route::resource('user/', ...);
This is going to interpret after the / as the resource name which would create a route parameter named {}, blank. Though this would create a route with the name show since there is no resource name to prepend to it.
The correct definition would be:
Route::resource('user', 'CrudController');
This does not create a route named show. It would be user.show. As user is the resource name.
In the Controller the parameter would be user not crud:
public function show(crud $user)
You can override the conventions used if you really want to, but I would suggest sticking within them. I can go into overriding these things if needed. [route names, route parameter name, ...]
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.
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.
I set up a test to get more familiar with Symfony2 and KnpPaginatorBundle. I have a table with pets that references the pet animal type (Dog, Cat, Etc). I can sort by the id, name, but when I try to sort by animal type I get an error message stating:
There is no such field [animalkind] in the given Query component, aliased by [a]
I have tried various field names, but nothing seems to work.
Entity: MyPets.php
<?php
namespace Xyz\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* MyPet
*/
class MyPet
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $name;
/**
* Set id
*
* #param integer $id
* #return MyPet
*/
public function setId($id)
{
$this->id = $id;
return $this;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return MyPet
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #var \Xyz\TestBundle\Entity\AnimalKind
*/
private $AnimalKind;
/**
* Set AnimalKind
*
* #param \Xyz\TestBundle\Entity\AnimalKind $animalKind
* #return MyPet
*/
public function setAnimalKind(\Xyz\TestBundle\Entity\AnimalKind $animalKind)
{
$this->AnimalKind = $animalKind;
return $this;
}
/**
* Get AnimalKind
*
* #return \Xyz\TestBundle\Entity\AnimalKind
*/
public function getAnimalKind()
{
return $this->AnimalKind;
}
}
Entity: AnimalKind.php
<?php
namespace Xyz\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* AnimalKind
*/
class AnimalKind {
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $type;
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set type
*
* #param string $type
* #return AnimalKind
*/
public function setType($type) {
$this->type = $type;
return $this;
}
/**
* Get type
*
* #return string
*/
public function getType() {
return $this->type;
}
/**
* #var \Doctrine\Common\Collections\Collection
*/
private $MyPets;
/**
* Constructor
*/
public function __construct() {
$this->MyPets = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add MyPets
*
* #param \Xyz\TestBundle\Entity\MyPet $myPets
* #return AnimalKind
*/
public function addMyPet(\Xyz\TestBundle\Entity\MyPet $myPets) {
$this->MyPets[] = $myPets;
return $this;
}
/**
* Remove MyPets
*
* #param \Xyz\TestBundle\Entity\MyPet $myPets
*/
public function removeMyPet(\Xyz\TestBundle\Entity\MyPet $myPets) {
$this->MyPets->removeElement($myPets);
}
/**
* Get MyPets
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getMyPets() {
return $this->MyPets;
}
public function __toString() {
return $this->getType();
}
}
Controller: MyPetController.php (IndexAction)
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery("SELECT a FROM XyzTestBundle:MyPet a");
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($query,$this->get('request')->query->get('page',
1)/*page number*/,15/*limit per page*/);
return $this->render('XyzTestBundle:MyPet:index.html.twig', array(
'pagination' => $pagination,
));
}
View: MyPet/index.html.twig (snipped)
<table class="records_list">
<thead>
<tr>
{# sorting of properties based on query components #}
<th>{{ knp_pagination_sortable(pagination, 'Id', 'a.id') }}</th>
<th>{{ knp_pagination_sortable(pagination, 'Name', 'a.name') }}</th>
<th>{{ knp_pagination_sortable(pagination, 'kind', 'a.animalkind') }}</th>
</tr>
</thead>
<tbody>
{% for mypet in pagination %}
<tr>
<td>{{ mypet.id }}</td>
<td>{{ mypet.name }}</td>
<td>{{ mypet.animalkind }}</td>
</tr>
{% endfor %}
</tbody>
</table>
Can anyone give me any insight as to the problem?
The first issue is the missing ORM relationship declaration; basically in your DB YourPet cannot be associated to an AnimalKind.
You can find documentation about Entity relationships and associations on the Symfony's website.
From YourPet entity's point of view, you probably want to use a ManyToOne relationship.
The second issue, once you fix the problem above, is that you are not joining the AnimalKind entity in your query.
The third issue is that you cannot order a query result by an entity.
First issue potential solution:
// In MyPet class
/**
* #var \Xyz\TestBundle\Entity\AnimalKind
* #ORM\OneToMany(targetEntity="AnimalKind", inversedBy="MyPets")
*/
private $AnimalKind;
// In AnimalKind class
/**
* #var \Doctrine\Common\Collections\Collection
* #ORM\ManyToOne(targetEntity="MyPet", inversedBy="AnimalKind")
*/
private $MyPets;
Second issue potential solution:
$query = $em->createQuery("
SELECT a, k
FROM XyzTestBundle:MyPet a
JOIN a.animalKind k
");
Third issue potential solution:
<th>{{ knp_pagination_sortable(pagination, 'kind', 'k.type') }}</th>
To display animals with no animalKind you'll have to use a LEFT JOIN in your query
$query = $em->createQuery("
SELECT a, k
FROM XyzTestBundle:MyPet a
LEFT JOIN a.animalKind k
");
For those using query builder and DQL and still looking for answer:
$qb = $this->createQueryBuilder('a');
$qb->select('a', 'k')
->leftJoin(
'a.animalKind',
'k'
)
;
Assumption -> you have correctly created relation in your entity.