Skip to content

Instantly share code, notes, and snippets.

View rakeshsoni18's full-sized avatar
🎯
Focusing

Rakesh Soni rakeshsoni18

🎯
Focusing
View GitHub Profile
@rakeshsoni18
rakeshsoni18 / MANY-TO-MANY RELATIONSHIP ( PIVOT TABLE )
Created July 7, 2019 18:53
MANY-TO-MANY RELATIONSHIP ( PIVOT TABLE )
------------------------------------------------------------
MANY-TO-MANY RELATIONSHIP ( PIVOT TABLE )
------------------------------------------------------------
1) Shop.php
2) Product.php
class Shop extends Model
{
/**
* The products that belong to the shop.
@rakeshsoni18
rakeshsoni18 / Eloquent Relationships – with “automatic” orderBy
Created July 7, 2019 18:56
Eloquent Relationships – with “automatic” orderBy
Should we add orderBy in every query? No, we can define that in relationship itself.
Our usual Eloquent relationship looks something like this:
class Category extends Model
{
public function products()
{
return $this->hasMany('App\Product');
}
}
@rakeshsoni18
rakeshsoni18 / Filter Eloquent relationships “on-the-fly”,
Created July 7, 2019 19:01
Filter Eloquent relationships “on-the-fly”, when you need it
I find more and more Eloquent functions that are rarely used, so continue posting these short tips on the blog. Here’s the next one.
Simple Eloquent relationship goes like this.
class Author extends Model
{
public function books()
{
return $this->hasMany(Book::class);
}
@rakeshsoni18
rakeshsoni18 / Get Data without column names in eloquent.
Last active July 11, 2019 17:32
Get Data without column names in eloquent.
How can i get data into this format:
[
[
1499779800000,
145.53
],
[
1499866200000,
145.74
],
@rakeshsoni18
rakeshsoni18 / whereHas where multiple
Created July 11, 2019 17:40
whereHas where multiple (must contain)
I'm in a bit of a situation with whereHas, the problem is I got a array with keys, I need to use those selected key to find results that contains all (ids that is contained within the array). The problem is it works with one, but when having more than one it does not work.
Solution:
Way-1
$keys = array_keys($request->genres);
$list = Movie::with('genres');
foreach($keys as $k => $v) {
$list->whereHas('genres', function ($query) use($v) {
@rakeshsoni18
rakeshsoni18 / Foreach in javascript
Created July 12, 2019 05:16
Foreach in javascript
<input type="text" name="sdms[]">
<script>
$(document).on("keyup", ".sdms", function() {
var sum = 0;
$(".sdms").each(function(){
sum += +$(this).val();
});
document.getElementById("corporate").innerHTML = sum;
});
</script>
@rakeshsoni18
rakeshsoni18 / Add Social Share Link On Custom Button
Created July 15, 2019 09:11
Add Social Share Link On Custom Button
@rakeshsoni18
rakeshsoni18 / WordPress Custom Query
Created July 15, 2019 11:22
WordPress Custom Query
<?php
$i = 1;
global $wpdb;
$getdonar = $wpdb->get_results("SELECT * FROM wp_donors");
foreach ($getdonar as $row){ ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo ucfirst($row->donor_name); ?></td>
</tr>
<?php $i++; } ?>
@rakeshsoni18
rakeshsoni18 / Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Auth\UserInterface, null given open:
Created July 28, 2019 16:35
Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Auth\UserInterface, null given open:
in the create() method in the LoginController you must return the $user that worked for me
protected function create(array $data)
{
$user = User::create([
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
@rakeshsoni18
rakeshsoni18 / Multi Level Category in Select Option
Last active July 28, 2019 17:51
Multi Level Category in Select Option
// Two Level
@foreach($categories as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
@foreach($category->subcategory as $sub)
<option value="{{$sub->id}}">-{{$sub->name}}</option>
@endforeach
@endforeach
// Three Level