Laravel getting ids of a collection without foreach - laravel

My code currently looks like this:
foreach ($things as $thing) {
$ids[] = $thing->id;
}
dd(Other::whereIn('thing_id', $ids)->get());
Thing model has many Other
public function others()
{
return $this->hasMany(Other::class);
}
It's working, but can I achieve this functionality without using the foreach? It doesn't seems to be clean for me. I've tried to give whole collection to where in like this:
dd(Other::whereIn('thing_id', $things)->get());
but this only returned where id was 1.
I'm looking for help to clean up this code, any help appreciated.

There is a function called "pluck"
You can apply it on a collection as following
$collection->pluck('id');
More can be seen on docs
https://laravel.com/docs/7.x/collections#method-pluck

I've already found a way to clean it up a bit, instead of foreach I can simply use :
$ids=$things->pluck('id');
If there's a cleaner way pls show me :)

Related

Laravel 5.5 - Deep Relations calling by -->

I now spent hours googling and experimenting on trying to get an relation with two intermediate tables working.
My database looks like this:
(apt_id is apartment_id in real, was shorter to write)
I have every relation one away setup correctly with belongsTo and and hasMany:
EXAMPLE FROM House.php
public function user()
{
return $this->belongsTo('App\User');
}
public function apartments()
{
return $this->hasMany('App\Apartment');
}
Isn't there a way to access these relations like:
$house->apartments->tenants->entries
in Blade:
#foreach ( $house->apartments->tenants->entries as $entry )
, since I want to display all house entries on house.show (Blade View)
The only way it's working is by using a bunch of foreach inside each others... :/ and they define the order...
Using my wanted relation calling produces:
Property [tenants] does not exist on this collection instance.
displayed on the page.
Greetings,
Pat
I don't think you can achieve what you want using the code you posted, because when calling, for example, $house->apartments it returns a Collection object. So, it is not dealing with database anymore, that's why you would need to use a bunch of #foreachs.
I don't know if this is the best way to solve this, or if it will help you in your actual problem, but you could think this problem backwards and try something like this:
$entries = \App\Entry::whereHas('tenants', function($q) use ($house) {
$q->whereHas('apartments', function($q1) use ($house) {
$q1->where('apartments.house_id', $house->id);
});
})->get();
And in the view:
#foreach ($entries as $entry)
{{ $entry->tenant->apartment->house->name }}
#endforeach

Can't get Another field from session

I got problem about session data, in my web, i want to make a different view based on previledge user, here my code in controller :
$previledge = $this->session->userdata['previledge'];
$data['previledge']=$previledge;
if($data['previledge'] == 'admin'){
$this->load->view("app_admin/global/header",$d);
$this->load->view("app_admin/materi/home");
$this->load->view("app_admin/global/footer");
}
else {
$this->load->view("app_admin/global/header2",$d);
$this->load->view("app_admin/materi/home");
$this->load->view("app_admin/global/footer");
}
}
if i run my code above, previledge with admin can't go to the right view.
if there any suggestion, please inform,
thanks
Its something like syntax error.
$previledge = $this->session->userdata('previledge');
This will help you.
Below code must have to work
$this->session->userdata('previledge')=='admin'

findOrFail() in Query\Builder

DB::table('amounts')->findOrFail($id);
return
Call to undefined method Illuminate\Database\Query\Builder::findOrFail()
I don't want to use models here.
What's the best way to achieve this ? (I just want to return a 404 when the id is not in the table.)
In the meantime I do:
$amount = DB::table('amounts')->find($request->input('amount'));
if(!$amount) abort(404);
As you can see in this API documentation
https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Builder.html#method_find
https://laravel.com/api/5.4/Illuminate/Database/Eloquent/Builder.html#method_findOrFail
find($id) is Database Builder method but findOrFail($id) is not so you cannot use it directly that way.
I didn't really get why you don't want to use Eloquent Model but thats the good way to do or stick with what you are doing in meantime.
You can try with below code to get record if exits into table or not:
$result = DB::table('amounts')->find($id);
if($result == NULL) //check if no record found
{
//do your stuff here
}
else //if record found from table
{
//do your stuff here
}
Hope this helps.
If you don't want to use models then that will do the job. If you use models you can try and catch the
\Illuminate\Database\Eloquent\ModelNotFoundException
and abort 404 at that point.

Laravel Debugger showing duplicated queries

Does any one has an idea about this. I don't know why it is showing duplicated queries. I searched a lot, found one answer on stackoverflow, but didn't get proper answer. If anyone faced the same error then please let me know. Thank you
protected $_param;
public function __construct(Utility $utility)
{
$league = $utility->checkDomainController();
view()->share('league', $league);
$this->league = $league;
}
This is the code in controller. which shares league to all the views. But there is only one query in $league = $utility->checkDomainController();
Here is the checkDomainController
if(\Request::server('HTTP_HOST') == env('MAIN_DOMAIN'))
{
$leagueSlug = Route::current()->getParameter('league');
$league = League::where('url', $leagueSlug)->first();
}
else
{
$league = League::where('custom_domain', \Request::server('HTTP_HOST'))->first();
}
if(!$league)
{
//error page
}
return $league;
Anytime you call a property which relies on a related model in blade, Laravel executes a new query, unless you eager load and pass from the controller to the view the preloaded data.
This example you posted seems to me as a loop somewhere within your blade code. Maybe if you can share a bit more of the code related to the front-end, I can help you figure it out.

update record with codeigniter active record

In my controller, I am calling a model function with the following:
$this->sales_model->softdelete_order($this->input->post('ordernumber'));
what I want to do, in my model is
update Customer_Order_Summary set Deleted='1' where CustomerOrderID='123'
where 123 is $this->input->post('ordernumber')
My Model syntax is:
function softdelete_order($q){
$this->db->set('Deleted','1');
$this->db->where('CustomerOrderID', $q);
$query = $this->db->update('Customer_Order_Summary');
}
This is not working or outputting any errors.
The model is preloaded and the post information is posting and echoing correctly so purely a model syntax issue I think.
Help appreciated as always.
Thanks,
After
$query = $this->db->update('Customer_Order_Summary');
add
$this->db->last_query();
to see your query and you can repair it from there.
Try using
$this->db->set('Deleted',1);
If not working post your exact error

Resources