I'm trying to save my Pivot but nothing happened, it doesnt prompt any error I dunno where to debug cause there is no prompt, any idea ? attached here is my code.
Controller note: Email and Name is updating properly.
public function update(Request $request, User $User)
{
$user=auth()->user();
$User->name = $request->get('name');
$User->email = $request->get('email');
$User->warehouse_id = $request->get('warehouse_id');
$User->role_id = $request->get('role_id');
$User->save();
$User->roles()->updateExistingPivot($request->get('role_id'), ['role_id' => $request->get('role_id')]);
$User->warehouse()->updateExistingPivot($request->get('warehouse_id'), ['warehouse_id' => $request->get('warehouse_id')]);
return redirect()->route('user.index');
}
Model for relationship
Warehouse
class Warehouse extends Model
{
protected $fillable = ['warehouse','warehouse_description','created_by'];
public $primaryKey='id';
public function users()
{
return $this->belongsToMany(User::class);
}
}
User
public function roles()
{
return $this->belongsToMany(Role::class);
}
public function warehouse()
{
return $this->belongsToMany(Warehouse::class);
}
Role
class Role extends Model
{
protected $fillable = ['role'];
public $primaryKey='id';
public function users()
{
return $this->belongsToMany(User::class);
}
}
What I did was putting isnerting the previews id before yusing updateExistingPivot
Here is my Controller
public function update(Request $request, User $User)
{
$User->name = $request->get('name');
$User->email = $request->get('email');
$User->warehouse_id = $request->get('warehouse_id');
$User->role_id = $request->get('role_id');
$User->roles()->updateExistingPivot($User->role_id, ['role_id' => $request->get('role_id')]);
$User->warehouse()->updateExistingPivot($User->warehouse_id, ['warehouse_id' => $request->get('warehouse_id')]);
$User->save();
return redirect()->route('user.index');
}
Related
Here is the source code of model and controller.
My Role.php
class Role extends Model
{
public function permissions()
{
return $this->belongsToMany('App\Models\Permission');
}
}
My Permission.php
class Permission extends Model
{
public function roles()
{
return $this->belongsToMany('App\Models\Role');
}
}
My Controller
class RoleController extends Controller
{
public function update(Request $request, $id) {
$role = Role::findOrfail($id);
$roleid = $role->id;
$permissions = $request->permissions;
foreach ($role as $roles) {
$roles->permissions()->updateExistingPivot($roleid, $permissions);
}
}
}
i will appreciate your help.Thanks
Assuming you want to 'sync' the permissions for the role, make this role only have the permissions that have been passed via the Request input:
public function update(Request $request, $id)
{
// validate the permissions array
...
$role = Role::findOrFail($id);
$role->permissions()->sync($request->input('permissions', []));
...
}
Laravel 6.x Docs - Eloquent - Relationships - Updating Many to Many Relationship - Syncing Associations sync
you can use laravel builtin functions
Add = attach
Delete = detach
Update = sync
more info
https://laravel.com/docs/5.8/eloquent-relationships#updating-many-to-many-relationships
Pass Array data while update data
class RoleController extends Controller
{
public function update(Request $request, $id) {
$input = $request->all();
$role = Role::findOrfail($id);
$roleid = $role->id;
$permissions = $input['permissions'];
foreach ($role as $roles) {
$roles->permissions()->updateExistingPivot($roleid, $permissions);
}
}
}
Create a controller with resource and assign the controller to web.php
Your form's action should be as follows and use
action="{{route('GustPosts.update', $DataEdit->id)}}"
#csrf
#method('put')
Form as follows
<form action="{{route('GustPosts.update', $DataEdit->id)}}" method="post">
#csrf
#method('put')
</form>
On Your controllers
Edit Function
public function edit($id)
{
$data = Post::find($id);
return view ('gustUpdateForm',['DataEdit'=> $data]);
}
Update Function
public function update(Request $request, $id)
{
$objProductsUpdate = Post::find($id);
$objProductsUpdate -> ProductName = $request -> get('PNameTxt');
$objProductsUpdate -> ProductModel = $request -> get('PModelTxt');
$objProductsUpdate -> ProductPrice = $request -> get('PPriceTxt');
$objProductsUpdate -> OriginalFileName = $file->getClientOriginalName();
$objProductsUpdate -> FileName = $file->getFilename().'.'.$extension;
$objProductsUpdate ->save();
return redirect()->route('products.index');
}
I going to do massage notification, I already make the notifications steps,
but gives me this error
when I do dd($notifiable); I found all data
Undefined index: user_id OR
Undefined index: name
public function store(Request $request)
{
$chating=new chats();
$chating->chat = $request->input('chat');
$chating->user_id = Auth::id();
$chating->employee_id = $request->input('employeeid');
$chating->save();
$user_id=$request->input('employeeid');
auth()->user()->notify(new SendMassages($user_id));
return redirect()->back();
}
database notifications table coulmn data {"user_id":5,"name":"Ibrahim"}
Model
protected $user_id;
protected $name;
public function __construct($user_id)
{
$this->user_id = $user_id;
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
return [
'user_id' => $this->user_id,
'user'=>$notifiable
];
}
View:
{{ $notification->data['name'] }}
Model:
class SendMassages extends Notification
{
use Queueable;
public $user;
public $user_id;
public function __construct($user_id)
{
$this->user_id = $user_id;
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
// dd($notifiable);
return [
'user_id' => $this->user_id,
'user'=>$notifiable
];
}
}
It seems that you have problem with encapsulation in your code. First you have to make the property of your model into public if you want the quick and not-safe way but if you want the OOP way, You must write setters to do that logic for you.
First and not-safe approach :
class YourModel {
public $user_id;
.
.
.
}
The OOP way:
Class {
public function setUserId(int $userId)
{
$this->user_id = $userId;
}
.
.
.
}
if you are willing to get the exact answer please copy your controllerclass and model in here.
I have 3 models
User - Role- Permission
User
class User extends Model
{
protected $fillable = [
'name', 'email', 'password',
];
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
Role
class Role extends Model
{
protected $fillable = ['name' , 'label'];
public function users()
{
return $this->belongsToMany(User::class);
}
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
}
Permission
class Permission extends Model
{
protected $fillable = ['name' , 'label'];
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
I want get List of users whose permissions were updated on a specific date
I know I sould use something like below but I dont know exatly how to use Where
$users = User::with('roles.permissions')->orderBy('name', 'asc')->paginate(25);
thanks alot
Use whereHas():
$users = User::whereHas('roles.permissions', function($query) use($date) {
$query->whereDate('permission_role.updated_at', $date);
})->orderBy('name', 'asc')->paginate(25);
Now I try to connect an Ionic3 test app with laravel 5.4 API data.
Ionic code is click here
So, my goal is to show list page in Ionic3 app.
But there are nothing to show.
Could you some advice where I modify code?
Thank you in advence.
Laravel 5.4 API
namespace App\Http\Controllers;
use App\Customer;
use Illuminate\Http\Request;
class CustomerController extends Controller
{
public function index()
{
$customer = Customer::all();
return response()->json($customer);
}
public function store(Request $request)
{
$this->validate($request, [
'name'=>'required',
'email' =>'required',
'tel' =>'required',
]);
$customer = Customer::create($request->all());
return response()->json($customer);
}
public function show($id)
{
$customer = Customer::find($id);
return response()->json($customer);
}
public function update(Request $request, $id)
{
$customer = new Customer;
$customer->name = $request->input('name');
$customer->email = $request->input('email');
$customer->tel = $request->input('tel');
$customer->address = $request->input('address');
$customer->note = $request->input('note');
$customer->save();
return response()->json($customer);
}
public function destroy($id)
{
$customer = Customer::find($id);
$customer->delete();
return response()->json($customer);
}
}
I have 2 tables: customers and address one form to save data into this tables. For this, I made the relation like:
Address model
public function customer()
{
return $this->belongsTo(Customer::class);
}
customer model
public function address()
{
return $this->hasMany(Address::class);
}
How can i save the address in table? I already done with customer i can save, update and delete. I read the DOC of Eloquent of laravel, but i don't really get it.
Here is my controller
class CustomerController extends Controller
{
public function index()
{
// $customer = Customer::all();
$customer = Customer::with('address')->get();;
return view('customer.index', compact('customer'));
}
public function create(Address $address)
{
$address = $address->all();
return view('customer.create');
}
public function store(CustomerRequest $request , Customer $customer)
{
$customer = Customer::create($request->all());
return redirect()->route('customer.index',compact('customer'));
}
public function show(Customer $customer)
{
return view('customer.show',compact('customer'));
}
public function edit(Customer $customer)
{
return view('customer.edit', compact('customer'));
}
public function update(CustomerRequest $request, $id)
{
$customer = Customer::find($id)->update($request->all());
return redirect()->route('customer.index',compact('customer'));
}
public function destroy($id)
{
Customer::destroy($id);
return redirect()->route('customer.index');
}
}
Any suggestions would be appreciated!!