Form Model Binding with relationship - laravel

I already looked at this post but I can't seem to make it right:
Laravel form model binding
I get this error:
https://gyazo.com/2ea7b7bb6a19d588829447ee1a92053e I use laravel 5.2
for this.
some screenshots:
https://gyazo.com/1b2c35e660dfe1aae69a02703733d083
https://gyazo.com/3d6f294473f6e54650a4a4403dc2777e
https://gyazo.com/a59aebc7362f51f9ac27852ea032f962

To expand on my comment:
Your problem is here:
$user = User::where('id', $id) // Here more specifically, Laravel does not know if you mean id of users table or details table
->leftJoin('user_details', user_details.user_id', '=', 'users.id')
->first();
Rewrite your where statement like this:
$user = User::where('users.id', $id)....
And it should work. Basically since you're joining 2 tables and they both got id you need to specify which id you want to query by.

Related

Laravel eloquent without() in nested relationship

I am trying to exclude a few relations from my API call.
Right now I'm returning the records as following:
return response()->json(Ticket::where('status_id', '!=', 5)->get());
In the ticket model I have a belongsTo relation with customer.
In the customer model I have a hasOne relation with Address.
Now, I have included the address in the customer model, and the customer in the ticket model usin $with.
I want to get the customer with the ticket, but not the address. I tried using the without function as followed: Ticket::where('status_id', '!=', 5)->without(['customer.address'])->get().
This is not working, however the following does work: Ticket::where('status_id', '!=', 5)->without(['customer'])->get() I do get the ticket without any relations.
I assumed the first function would work, considering this also works in the with() function, but it does not.
To remove the relation address while keeping the address relation in the $with attribute of the customer class, use this:
Ticket::where('status_id', '!=', 5)->with(['customer' => function($customer){
$cutomer->without(['address']);
}])->get()
But I suggest you remove the address from the $with attribute or you will end up with more without(['address']) than with('address') if you remove it.
It should work provided there's no typo in the relation name (like address instead of addresses).
Another approach - try the following
return response()->json(
Ticket::with('customer', fn($query) => $query->without('address'))
->where('status_id', '<>' 5)
->get()
);
I think even this should work (without typos in relation name)
return response()->json(Ticket::without('customer.address')->where('status_id', '<>' 5)->get());
This should remove/unset address relationship from customer

Laravel eloquet check non existing many to many relation

I'm having trouble to write query in laravel eloquent ORM.
I have a proyect table, where you can assign users, in a many to many relationship
In the view to asign users, I have a selector, but I want to show only the users not already assigned to the proyect and checking also that the user belongs to the company that created the proyect (user.company_id=proyect_id)
In a normal query should me something like this, having $company_id and $proyect_id from the controller.
select * from users u left join proyect_user pu on u.id=pu.user_id and
pu.proyect_id = $proyect_id where u.company_id=$company_i and
proyect_id is null;
The query works, but I would like to use Eloquent. ¿Any idea how to do it?
It depends on how you declared the relationship in the User model. But I would do something like this:
$users = User::whereHas('company', function ($query) use ($companyId) {
$query->where('id', $companyId)
})->whereDoesntHave('proyects', function ($query) use ($proyectId) {
$query->where('id', $proyectId);
})->get();

Laravel 7 Query with() and using Where()

Hey guys I have a query that looks like this
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table));
I need to filter the transaction based on the iso_id that belons to the current user logged in.
$query = Transaction::with(['customer', 'merchant', 'batch'])
->select(sprintf('%s.*', (new Transaction)->table))
->where('merchant.iso_id', '=', auth()->user()->isIso());
The iso_id I need to compare to, is inside the merchant table
auth()->user()->isIso() returns the correct iso_id if true or sends false if not
So my first try at this was to use where('merchant.iso_id', '=', auth()->user()->isIso())
But that returns that the column does not exist because for some reason, it's not switching from the transaction model to the merchant one.
I am not sure how to use the stuff inside with() as a selector for my where()
Any help would be appreciated!
Try using whereHas to add the constraint:
$query = Transaction::with(['customer', 'batch'])
->whereHas('merchant', function ($q) {
$q->where('iso_id', auth()->user()->isIso());
})
->select(sprintf('%s.*', (new Transaction)->table))
->get();

Empty field in view while it has been retrieved with dd Function

I have joined two tables in Clint table controller and Appointment table as below image and dd function showing the data already.
Here is my controller:
and here is result of dd():
but in the view page it's an empty field:
and here is available I am using in the view:
I have seen your controller image and in join statement mistake.
When you join your appointment table to the clients table then you should use foreign keys.
public function show(Client $client) {
abort_if(Gate::denies('client_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$client = DB::table('clients') ->join('appoinments', 'clients.id', '=', 'appoinments.clint_id') ->select('clients.*', 'appoinments.start_time', 'appoinments.finish_time') ->get();
return view('admin.clients.show', compact('client'));
}
I assume in the appointment table you have clint_id.
The variable $client that you are passing to your view is from your Route Model Binding. $clients is the result of your query with the join to appointments which provides the fields start_time and finish_time. You are showing us the output of a dd on $clients which includes those fields, but passing $client to your view which most likely does not have such fields.
You could adjust what you are passing to your view to fix this:
return view('admin.clients.show', [
'client' => $clients,
]);
I am not sure what your purpose is with this method though as the route parameter doesn't end up being used. You probably want to be using that route parameter to filter your query. Though this could be a good place to try using relationships instead of directly joining with query builder.
Also, please do not put up pictures of code. Please edit your question and include the code into the question if you can. Thanks.
Laravel 6.x - Docs - Routing - Route Model Binding

Laravel Eloquent get result where relation data is null

I have two Models User and Owner with many to many relationship
I want to fetch only those users who don't have owner
how can I get using eloquent
i tried
$query = User::whereHas('userOwners', function ( $subquery ){
$subquery->whereNull('owner_id');
})->get();
but not working.
Eloquent has a way to query an absent relationships, it should work like this in your case:
$query = User::doesntHave('userOwners')->get();
User::with('userOwners')
->whereHas('userOwners', function ($query) {
$query->wherehas('owner_id');
})
->where('user_status', 1)->get();
use second where if you want to filter on user
use first where if you want to filter on Owner
I think you should just change your query like:
$query = User::whereHas('userOwners')->get();
Hope this work for you!!!

Resources