Call to a member function where() an integer in laravel - php

I get this error when running this code
public function update(StoreAccountsRequest $request, $id)
{
$user = User::findOrFail($id);
$user->Accounts()->update($request->except('_method', '_token'))->where('accounts.user_id', '$user');
return redirect()->route('accounts.index')->with(['message' => 'Accounts updated successfully']);
}
please I need help understanding how to update a single user account in the accounts Table

Code must e like this
$user->Accounts()->update($request->except('_method', '_token'));

Related

actingAs doesn't work as expected for feature testing. laravel

I actually am not able to understand why I am getting the following error.
App\Models\User::team must return a relationship instance, but "null" was returned. Was the "return" keyword used?
I am basically creating test cases for simple orders for ecommerce.
User Modal
public function team(): BelongsTo|null
{
if (!empty($this->team_id)) {
return $this->belongsTo(Team::class);
}
return null;
}
Test case
public function test_order_status_update()
{
$order = $this->create_order($this->books->id, $this->appUser->id, $this->address->id);
$response = $this->actingAs($this->user)->put('orders/' . $order->json('order.id'), [
'order_status' => 'ordered',
]);
$response->assertRedirect('orders/' . $order->json('order.id'))->assertSessionHas('success');
}
In addition, I have another feature in my application called pages access control, which controls page access for multiple users (admin, developer, and users).
I have implemented this feature manually using middleware.
Middlware.php
public function handle(Request $request, Closure $next)
{
//teams 1-Developer 2-Admin 3-Management 4-Marketing 5-Audit 6-Sales 7-Bookstores 8-Delivery 9-User
$team = $request->user()->team;
if ($team->id == 1 || $team->id == 2) {
return $next($request);
}
$pages = auth()->user()->pages->merge(auth()->user()->team->pages);
$currentRouteName = $request->route()->getName();
$pages->contains('route_name', $currentRouteName) ?: abort(403);
return $next($request);
}
Based on the error above, I believe the actingAs function is unable to obtain authenticated user information, which is why my test failed.
How can I fix this?
Simply don't check your team_id:
public function team(): BelongsTo
{
return $this->belongsTo(Team::class);
}
Laravel tries to be smart. If team_id isn't set, it will just return null. However, if you don't return the BelongsTo, the magic code of Laravel will trip when you try to access user->team

How to relate multiple forms from different controllers?

I have Certificate and Student models, with one to many relation.
When the user is done making the certificate he is redirected to another page with student form, I dont know how to get the certificate_id or how to relate the student to the last certificate that it was created. help please.
Certificate Controller
public function store(StoreCertificateRequest $request)
{
$data = $request->validated();
$data['user_id'] = auth()->id();
Certificate::create($data);
return redirect()->route('students.create')->with(['success' => 'Certificate has been Saved']);
}
Student Controller
public function store(StoreStudentRequest $request)
{
$data = $request->validated();
Student::create($data);
return redirect()->route('users.index')->with('success', 'Students were added');
}
So the certificate is already created, you can get that id by assigning a variable to the create, and then return it to the view as $variable->id

Laravel 8: Checking user's data with database table does not work properly

I have created a two factor authentication system, and it redirects user to token.blade.php where he must enters the token that is going to be sent to his phone number and also stored at active_codes table.
Now I want to check if the user has entered the same token code that was stored at active_codes table which looks like this:
And then at the Controller, I tried this:
public function submit(Request $request)
{
$data = $request->validate([
'token' => 'required|min:6'
]);
if($data['token'] === auth()->user()->activeCode()->code){
dd('same');
}
}
But when I enter the same token, I get this error:
ErrorException Undefined property:
Illuminate\Database\Eloquent\Relations\HasMany::$code
so my question is how to check if the requested token code of user, is as same as the token code which is stored on the DB ?
Note: The relationship between User and ActiveCode Models is OneToMany.
User.php:
public function activeCode()
{
return $this->hasMany(ActiveCode::class);
}
ActiveCode.php:
public function user()
{
return $this->belongsTo(User::class);
}
Your solution is pretty easy, you are not doing ->first() or ->get(), so you are trying to access a model property on a HasMany class.
This code should be similar to:
auth()->user()->activeCode()->first()->code
But if you have more than 1 code, then you should do something like:
public function submit(Request $request)
{
$data = $request->validate([
'token' => 'required|min:6'
]);
if(auth()->user()->activeCode()->where('code', $data['token'])->exists()){
dd('same');
}
}

Laravel Blade Multiple Values in DB Table

Little question: Currently I've a route
Route::get('update/{id?}', 'SessionController#onClick');
That loads with this function:
public function onClick($id, Request $request)
{
$data = $request->all();
$user = User::where('userRooms', $id)->first();
return view('sessions.cards')->with('user', $user);
}
I want that my blade view displays all users that have access to this room, currently I hard coded everything. My question is now, what do I have to write down in my code so that users have access to multiple rooms and not just one(userRooms row in my db for User::Class) & how I can display all users that have this room?
I hope I understood your question correctly. Try this way:
public function onClick($id, Request $request)
{
$data = $request->all();
$users = User::where('userRooms', $id)->get();
return view('sessions.cards', [
'users' => $users,
]);
}

Laravel how to insert my auth user id to my database upon submission?

im using laravel 5.4
upon submitting the form, the user_id of the submitter will be inserted in the database. and im not sure if its the right way so if you have any idea please help me thanks
can anyone help me please this my controller as of now.
public function index()
{
$user = Auth::user();
$rfq_table = $user->rfq_table;
return view('admin.sales.rfq.index',compact('rfq_table'));
}
public function create()
{
return view('admin.sales.rfq.create',compact('rfq_table'));
}
public function store(Request $request)
{
$user=Auth::user();
$rfq= $user->rfq_table()->create($request->all());
return redirect()->route('admin.sales.rfq.index');
}
I'm not sure how your DB looks, but you can create a var $user_id = Auth::user()->id; and save that in your DB
If you are using a form, insert the session user id like this in your insert controller:
Create your variable out of the user()'s id:
$user_id = Auth::user()->id;
Then use the variable here:
$insert->usr_id = $user_id;
$insert->postcode = request('postcode');
Your Other Inserts from your submitted form go here.

Categories