I have SQL functions stored on my database.
However, I can not call them.
$nb = DB::select('SELECT nb_seances_archivees()');
The result is :
array:1 [▼
0 => {#186 ▼
+"nb_seances_archivees": 0
}
]
But the desired result is just 0.
Thank's for help !
By default DB::select return an array of objects, you can use collections to get the first result:
$nb = collect(DB::select('SELECT nb_seances_archivees() AS nb'))->first()->nb;
Or directly access the first object in the array:
$nb = DB::select('SELECT nb_seances_archivees() AS nb')[0]->nb;
If you want to pass parameters then you should do:
DB::select('SELECT nb_seances_archivees(?) AS nb', [$parameter]);
Related
In controller index function
I'm picking up which news ids are matching with a pack id:
$content_pack_news_array = DB::table('content_pack_news')->where('content_pack_id' , $content_pack_id)->get();
Using dd I get this result which I need to access news_id of all elements in it
Illuminate\Support\Collection {#3017 ▼
#items: array:2 [▼
0 => {#2376 ▼
+"news_id": 2
+"content_pack_id": 2
}
1 => {#3010 ▼
+"news_id": 4
+"content_pack_id": 2
}
]
}
How to return data that matched with ids:
"news_id": 2
"news_id": 4
Inside:
$news = News::with(['media', 'assigned_content_packs'])->where('id' , $news_id)->get();
If I use
$content_pack_news = DB::table('content_pack_news')->where('content_pack_id' , $content_pack_id)->first();
It works but it gets only first matching item to display.
Any help appreciated.
You can use pluck, to get the ids out.
$newsIds = DB::table('content_pack_news')
->where('content_pack_id' , $content_pack_id)
->pluck('news_id');
Pluck works great in combination with whereIn(), that checks a column against an array.
$news = News::with(['media', 'assigned_content_packs'])
->whereIn('id' , $newsIds)
->get();
You can do it in single query using sub query as:
$news = News::with(['media', 'assigned_content_packs'])
->whereIn('id', function($query) use($content_pack_id){
$query->select('news_id')
->from('content_pack_news')
->where('content_pack_id', $content_pack_id);
})
->get();
if i correct you only asking the first.
$content_pack_news = DB::table('content_pack_news')->where('content_pack_id' , $content_pack_id)->first();
change it to get(); then you get all records.
$content_pack_news = DB::table('content_pack_news')->where('content_pack_id' , $content_pack_id)->get();
Trying to get matching id's from a table and inserting them again in the same table under differnet relationship.
$contentPack = ContentPack::find($id);
$cloned_pack_goals = DB::table('content_pack_goal')->where('content_pack_id' , $contentPack->id)->get();
$cloned_pack_goal_ids = $cloned_pack_goals->goal_id;
Produces Exception
Exception
Property [goal_id] does not exist on this collection instance.
dd($cloned_pack_goals); outputs:
Illuminate\Support\Collection {#2466 ▼
#items: array:2 [▼
0 => {#3129 ▼
+"goal_id": 4
+"content_pack_id": 2
}
1 => {#2467 ▼
+"goal_id": 9
+"content_pack_id": 2
}
]
}
How to get goal_ids from the output to insert them into the same table again but with a different relation?
$newPack = $contentPack->replicate();
DB::table('content_pack_goal')->insert(['content_pack_id' => $newPack->id,'goal_id' => $cloned_pack_goal_ids]);
Am doing something wrong when getting the ID's and when inserting them. tried using ->first(); it works but only one id gets inserted
$cloned_pack_goals is a collection, so you need to exclude goal_ids from all collection records separately.
This snippet may help you:
$cloned_pack_goal_ids = DB::table('content_pack_goal')->where('content_pack_id' , $contentPack->id)->pluck('goal_id')->toArray();
foreach($cloned_pack_goal_ids as $key => $goal_id) {
DB::table('content_pack_goal')->insert(['content_pack_id' => $newPack->id,'goal_id' => $goal_id]);
}
To get an array of only the Ids, use pluck() and toArray()
$cloned_pack_goal_ids = DB::table('content_pack_goal')
->where('content_pack_id' , $contentPack->id)
->pluck('goal_id') // returns a collection of only Ids.
->toArray(); // returns an array from the collection.
Write your query in this format this will give you the require output:
$cloned_pack_goals = DB::table('content_pack_goal')->where('content_pack_id' , $contentPack->id)->get()->toArray();
$cloned_pack_goal_ids = $cloned_pack_goals[0]->goal_id;
What i have:
Illuminate\Support\Collection {#1299 ▼
#items: array:1 [▼
0 => {#1308 ▼
+"id_itemnfe": 1
+"fk_nfe": 1
+"nitemped": "1"...
So i have to ready as:
$data[0]->id_itemnfe;
or use foreach.
I was a Codeigniter user, what there was a row() method where i could print as $data->id_itemnfe directly.
Thank you all.
The alternative for Codeigniter row() method in laravel is first() method, it will return the first row and you can access your variables directly e.g. $data->id_itemnfe
Syntax:
Model::where('fieldname',$value)->first();
Example:
$user = User::where('email',$email)->first();
I have data like this:
array:1 [
0 => "No Brand,ddfg"
]
First of all this data is wrong, what I want is to have something like this:
array:2 [
0 => "No Brand"
1 => "ddfg"
]
So now its really an array :)
Then I need my array data transform to lower case like:
array:2 [
0 => "no brand"
1 => "ddfg"
]
Code
$sibarBrandsArray = SidebarManager::first()->pluck('brands')->toArray();
This return data like:
array:1 [
0 => "No Brand,ddfg"
]
And this is how my data looks like in database:
Any idea?
Solved
// get my table row
$sibarBrandsArray = SidebarManager::first();
// get my row column
$getBrandColumn = $sibarBrandsArray->brands;
// separate data in that column with comma
$separateBrands = explode(',', $getBrandColumn);
// lowercase each separated data
$brandsArray = array_map('strtolower', $separateBrands);
// dump the result
dd($brandsArray);
Result
array:2 [
0 => "no brand"
1 => "ddfg"
]
Laravel has a very efficient and easy way to work with arrays. It's called a collection. Click here to learn more. Don't convert your response to the array, use collection directly.
$sibarBrandsCollection = SidebarManager::first()->pluck('brands');
Laravel eloquent by default gives you collection instance when you get a response. so pluck in above call is nothing but calling pluck on collection instance. We can chain method to the collection and do manipulation as needed.
$sibarBrandsCollection = $sibarBrandsCollection->map(function ($name) {
return strtolower($name);
});
Above code will automatically convert all of your values to lowercase. Similarly, you can explode the value to get your intended result. At last, if you have to send data as array to the browser just add toArray() method at the end of your collection.
I would not use core PHP array function unless needed, Laravel collection is great way to work with arrays.
$yourArray = array_map('strtolower', $yourArray);
$yourArray = array_map('nestedLowercase', $yourArray);
function nestedLowercase($value) {
if (is_array($value)) {
return array_map('nestedLowercase', $value);
}
return strtolower($value);
}
or you can use:
$query->whereRaw('LOWER(`newsTitle`) LIKE ? ',[trim(strtolower($newsTitle)).'%']);
I'm running this query
$results = DB::connection('selection')
->select("
SELECT id, name, email
FROM users
WHERE email = $this->email
");
I expect to only get on result from this, which I do get, this is my output
array:1 [
0 => {
+"id": 1
+"name": "Ted Wood"
+"email": "tedwood#email.com"
}
]
What I would like to know is how can I access name without having to do $results[0]->name I would like to do $results->name.
Since it's only 1 item I'm getting I don't see a need for a foreach loop
Use reset pointer like this
reset($results)->name
using first
$results =collect(DB::connection('selection')
->select("
SELECT id, name, email
FROM users
WHERE email = $this->email
"))->first();
but i think the best way to do it would be like:
$results = DB::connection('selection')->table('users')->select('id', 'name', 'email')
->where('email',$this->email)->limit(1)->first();
DB::select() returns an array. The first() method works with collections, so you need to wrap the array in the collection to use the first() method.
$results = collect($results)->first()