ramniksingh

ramniksingh

Web Developer at INDIA

Member Since 8 Months Ago

Jalandhar, Punjab

Experience Points
4,340
Total
Experience

660 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
37
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
4,340 XP
Jul
25
2 months ago
Activity icon

Started a new Conversation Cannot Use Object Of Type Stdclass As Array

Hello, I think it is a basic question but I am not getting to understand the issue:

I have a below code:

$monthdata = array();
$months = DB::table('appcal')->select('monthyear')->distinct()->get();
        foreach($months as $month){
            $monthdata[$month->monthyear] = $month;
        }
        dd($monthdata);


// Output :


array:96 [▼
  "Jan-2016" => {#5590 ▼
    +"monthyear": "Jan-2016"
  }
  "Feb-2016" => {#5591 ▼
    +"monthyear": "Feb-2016"
  }
  "Mar-2016" => {#5592 ▼
    +"monthyear": "Mar-2016"
  }
  "Apr-2016" => {#5593 ▼
    +"monthyear": "Apr-2016"
  }

But i want to add an empty array field to use later. So I add this :

$monthdata = array();
$months = DB::table('appcal')->select('monthyear')->distinct()->get();
        foreach($months as $month){
            $monthdata[$month->monthyear] = $month;
            $monthdata[$month->monthyear]['wd']=0;
        }
        dd($monthdata);

But it throws an error:

Cannot use object of type stdClass as array.

Please suggest what I am doing wrong.

Jul
05
2 months ago
Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@lancashireman Thanks . Your snippet is right when we have a different datatype/format stored.

Actually the issue was of using query inside a loop which sometimes run and sometimes not. May be because of size of result set. That confused for this wheredate issue.

Jul
04
2 months ago
Activity icon

Replied to Running A Query Multiple Times Inside Foreach Loop And Storing The Results In An Array.

Thanks. I have read about N+1 and solving this with some other method either with join or eager loading.

But What is happening here? Why is it presents different behaviour. It should have run despite being slow. Is there some memory issue?

And I think it will easily run in traditional php in comparison to laravel framework.

Activity icon

Started a new Conversation Running A Query Multiple Times Inside Foreach Loop And Storing The Results In An Array.

Hello Everyone, I have encountered a strange problem. I am trying to run a query multiple times with foreach loop and storing the results in an array to use later for some calculations.

Below is the detail:

This works

$test = DB::table('appcal')
                    ->select('monthyear','appdate')
                    ->whereDate('appdate','>=', '2018-04-01')
                    ->get();

        dd($test);

But this does not work

foreach($activities as $activity){
            $qry[] = DB::table('appcal')
                    ->select('monthyear','appdate')
                    ->whereDate('appdate','>=', '2018-04-01')
                    ->get(); 
        }
            dd($qry);

But this work

foreach($activities as $activity){
            $qry[] = DB::table('appcal')
                    ->select('monthyear','appdate')
                    ->whereDate('appdate','=', '2018-04-01')
                    ->get(); 
        }
            dd($qry);

Can you suggest what is the issue here? Am I doing something wrong ?

What should I correctly do to store the query results in an array to use later for some calculations.

Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@lancashireman I want to store results in an array and then perform some calculations with that array.

Yes Initial question was a bit different but that issue still remains as well. And somehow another question of storing query results in array from within a loop came out.

Jul
03
2 months ago
Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@lancashireman Thanks for your input, But datatype is date already.

Actually if you read the full conversation after that, the problem came out to be is of storing the query result in an array from inside the loop.

Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@meeshal Same behaviour as before

This does not produce any output and blank screen occurs without any error

$qry = [];
        foreach($activities as $activity){
                array_push($qry, DB::table('appcal')->get());
        }

        dd($qry);

But this produces output as raw array

$qry = [];
        foreach($activities as $activity){
                array_push($qry, DB::table('appcal')->get());
 		dd($qry);
        }

Jul
01
2 months ago
Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@meeshal

Secondly, try a simple query, like DB::table('xyz')->all();, inside the loop and dd($qry) outside.

When I do this :

foreach($activities as $activity){
            $qry[] = DB::table('appcal')->get();
                    //dd(DB::getQueryLog());
        }

        dd($qry);

Same thing happens as before - Inside the loop, dd($qry) shows the output and outside the loop dd($qry) shows nothing just a blank screen.

Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@meeshal What is dd(the_whole_query) ? the_whole_query .

I have done dd($qry) inside and outside above. Kindly check.

Sorry I couldn't understand this --------- and lastly, define the array $qry = [] before the array.

Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@meeshal

Actually the problem is when I use the query inside a loop . Outside the loop query works fine.

When I put dd($qry) outside the loop, Nothing happens. ----- It means when I run query inside the loop, and write dd($qry) outside the loop nothing happens, But When I put dd($qry) inside the loop , it shows me raw results.

Hope it is clear now.

Jun
30
2 months ago
Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@meeshal When I Put dd($qry) inside the loop below is the output :

array:1 [▼
  0 => Illuminate\Support\Collection {#5589 ▼
    #items: array:2101 [▼
      0 => {#5591 ▼
        +"monthyear": "Apr-2018"
        +"appdate": "2018-04-01"
      }
      1 => {#5592 ▼
        +"monthyear": "Apr-2018"
        +"appdate": "2018-04-02"
      }
      2 => {#5593 ▼
        +"monthyear": "Apr-2018"
        +"appdate": "2018-04-03"
      }
      3 => {#5595 ▼
        +"monthyear": "Apr-2018"
        +"appdate": "2018-04-04"
      }
.............................. Total 2102 Records

When I put dd($qry) outside the loop, Nothing happens. Blank Screen with no error or anything else.

Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@meeshal No, the result still same.

Actually the problem is when I use the query inside a loop .

foreach($activities as $activity){
            $qry[] = DB::table('appcal')
                    ->select('monthyear','appdate')
                    ->whereDate('appdate','>=', '2018-04-01')
                    ->get(); 
                    //dd(DB::getQueryLog());
        }

            dd($qry);

Outside the loop it works fine. Am I doing anything wrong in storing the results in array ? I want to use the data for each activity , so I try to stored it in qry[] array

Jun
29
2 months ago
Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@snapey No, If I put dd(DB::getQueryLog()); outside the loop, It doesn't show anything .

Same is the case when dd($qry) is written after the loop . If dd($qry) is written inside the loop it shows the output.

Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@snapey I think I have found the issue. Please see below:

This works

$test = DB::table('appcal')
                    ->select('monthyear','appdate')
                    ->whereDate('appdate','>=', '2018-04-01')
                    ->get();

        dd($test);

But this does not work

foreach($activities as $activity){
            $qry[] = DB::table('appcal')
                    ->select('monthyear','appdate')
                    ->whereDate('appdate','>=', '2018-04-01')
                    ->get(); 
                    //dd(DB::getQueryLog());
        }
            dd($qry);

But this work

foreach($activities as $activity){
            $qry[] = DB::table('appcal')
                    ->select('monthyear','appdate')
                    ->whereDate('appdate','=', '2018-04-01')
                    ->get(); 
                    //dd(DB::getQueryLog());
        }
            dd($qry);

Can you suggest what is the issue here? Am I doing something wrong ?

Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@snapey That's right I am looking at the exact database. I have only one . so no chance of confusion.

Jun
28
2 months ago
Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@snapey

This is the actual code I run

DB::connection()->enableQueryLog();

        foreach($activities as $activity){
            $qry[] = DB::table('appcal')
                    ->select('monthyear','appdate')
                    ->where('appdate','>=', $activity->actualStart)
                    ->get(); 
                    dd(DB::getQueryLog());
        }

And query log results in

array:1 [
  0 => array:3 [
    "query" => "select `monthyear`, `appdate` from `appcal` where `appdate` >= ?"
    "bindings" => array:1 [
      0 => "2017-10-20"
    ]
    "time" => 8.0
  ]
]

But no output after running query or using

dd($qry);

Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@guybrush_threepwood It doesn't change anything whether to use where or whereDate.

Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@snapey Below output is given in query log

array:1 [
  0 => array:3 [
    "query" => "select `monthyear`, `appdate` from `appcal` where date(`appdate`) >= ?"
    "bindings" => array:1 [
      0 => "2018-04-01"
    ]
    "time" => 6.0
  ]
]

And When I run below query in phpmyadmin, query works fine :

select `monthyear`, `appdate` from `appcal` where date(`appdate`) >= "2018-04-01" 

Activity icon

Replied to Filter Records Via Query Where Date Is Greater Than Some Date

@guybrush_threepwood @snapey

It is of datatype 'date'.

When I run the below in phpmyadmin, It works

select monthyear,appdate FROM `appcal` WHERE appdate>= '2018-04-01'

Why is it so hard to run simple queries through laravel ?

Activity icon

Started a new Conversation Filter Records Via Query Where Date Is Greater Than Some Date

Hi, I have been trying this and searching for solution:

$qry = DB::table('appcal')
                    ->select('monthyear','appdate')
                    ->whereDate('appdate','>=', '2018-04-01')
                    ->get(); 

It does not work & gives me no records at all ( even the query shows nothing without even giving any error).

But when I use the below it gives me correct records

$qry = DB::table('appcal')
                    ->select('monthyear','appdate')
                    ->whereDate('appdate','=', '2018-04-01')
                    ->get(); 

Why is that behaviour? The actual problem was with a date record from database, but I converted it to static date 2018-04-01 to test the logic.

May
05
4 months ago
Activity icon

Replied to Access Related Tables With HasManyThrough Non-linear Relations

User Model :

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'phone', 'address', 'is_admin'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


    public function docs(){
        return $this->hasMany('App\Doc', 'id', 'doc_id');
    }

    public function courseUsers(){
        return $this->hasMany('App\CourseUser', 'id', 'user_id');
    }

   
}

CourseUser Model:


namespace App;

use Illuminate\Database\Eloquent\Model;

class CourseUser extends Model
{
    public $timestamps = false;

    
    protected $fillable = [
        'user_id',
        'course_id',
        'start',
        'end',
        'remarks',
    ];

    public function course() {
        return $this->belongsTo('App\Course', 'course_id', 'id');
    }

    public function user() {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }

}

Course Model:


namespace App;

use Illuminate\Database\Eloquent\Model;

class Course extends Model
{

    public $timestamps = false;

    
    protected $fillable = [
        'title',
        'description',
        'type',
        'duration',
        'fee',
        'image',
        'remarks',
        'is_active',
    ];

    public function videos(){
        return $this->hasMany('App\Video', 'id', 'video_id');
    }

    public function courseUsers(){
        return $this->hasMany('App\CourseUser', 'id', 'course_id');
    }

}

Video Model:


namespace App;

use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    protected $fillable = [
        'course_id',
        'title',
        'type',
        'description',
        'link',
        'image',
        'remarks',
        'display',
    ];

    public function course() {
        return $this->belongsTo('App\Course', 'course_id', 'id');
    }
}

May
04
4 months ago
Activity icon

Replied to Access Related Tables With HasManyThrough Non-linear Relations

No replies yet! Is there something wrong in question?

May
01
4 months ago
Activity icon

Replied to Using A Common Storage Folder For Two Laravel Projects

But how to connect both to a single upload location ? Both projects share one database but different storage location linked in each project's public folder.

I want that when i upload file from one project, that file should be accessible by other project as well .

Thanks

Activity icon

Started a new Conversation Using A Common Storage Folder For Two Laravel Projects

Hello, I want to ask whether it is possible to use a common storage folder for two laravel projects hosted on same location ? Because I have a common database for both projects and Uploaded from one project is not available in another project.

I use symlink to use storage directories of each project at the moment. Please help.

Should I need to change filesystem.php? If yes, then how to change?

Apr
20
5 months ago
Activity icon

Started a new Conversation Access Related Tables With HasManyThrough Non-linear Relations

I have 4 tables / models

1 . User -- Linked to CourseUser (one to many) hasMany

id, name, email .....

2 . CourseUser -- JunctionTable of course & user (many to many)

id,user_id, course_id, ....

3 . Course -- Linked to CourseUser (one to many) hasMany . Linked to video (one to many)

id,name,title.....

4 . Video -- Linked to Course

id,course_id, title, link.....

How can I access the videos as per Specific User?

Can I define hasThroughMany relation ?

Running the below in controller returns related arrays but video array shows empty items.

$courses = CourseUser::with('course.videos')->get();

dd($courses);

Apr
19
5 months ago
Activity icon

Replied to Resource Controller For Many To Many Pivot Table To Enter Data With Separate Form.

presently i have made a new resource controller - CourseUsersController.php , linked model and CRUD views.

My models looks like below

CourseUser.php

class CourseUser extends Model
{
    public $timestamps = false;

    
    protected $fillable = [
        'user_id',
        'course_id',
        'start',
        'end',
        'remarks',
    ];

    public function course() {
        return $this->belongsTo('App\Course', 'course_id', 'id');
    }

    public function user() {
        return $this->belongsTo('App\User', 'user_id', 'id');
    }

}

User.php

 public function courseUsers(){
        return $this->hasMany('App\CourseUser', 'id', 'user_id');
    }

Course.php

public function courseUsers(){
        return $this->hasMany('App\CourseUser', 'id', 'course_id');
    }

Is this method correct to implement many to many relationship ? It is working fine in CRUD functionality. Is there anything that I am doing wrong ?

If this will work fine , do I also need the BelongsToMany methods in each of the models User.php & Course.php ??

Activity icon

Replied to Why Is Default Storage Engine MyISAM In Laravel Migrations

Thanks. I got it.

Can you suggest why laravel doesn't enforce relationships.

Isn't it the best practice to have relational linked tables in the database via primary-foreign keys ?

Activity icon

Replied to Resource Controller For Many To Many Pivot Table To Enter Data With Separate Form.

Sorry I couldn't understand your suggestions completely.

I have some doubts below related to the above.

How will I add data to other fields present in my joined table?

Why is it important to have single many to many relation instead of 2 one to many relations like we do in traditional approach ? Does it not work in Laravel?

Activity icon

Replied to Resource Controller For Many To Many Pivot Table To Enter Data With Separate Form.

okay. So you mean I should use existing model 'user.php' for the registration controller ?

Earlier, I created resource controller named 'UserController.php' and in the User.php I use fillable to fill the fields directly.

So would I need to add the rest of fields in that fillable array ? As my pivot table has extra fields.

Actually I didn't use Db Facades for inserting data. I used User::create to do that.

I hope I made my point clear, I just started to learn laravel so my knowledge is limited.

Activity icon

Replied to Resource Controller For Many To Many Pivot Table To Enter Data With Separate Form.

Okay. So what you are saying is I should follow traditional approach to have 2 one to many relations with :

  1. user to registration (one to many)

  2. course to registration (one to many)

And I don't need to add belongsToMany method. Is it what you are saying ?

If yes, I want to ask what is the problem i would have faced if I use pivot table ?

Activity icon

Replied to Resource Controller For Many To Many Pivot Table To Enter Data With Separate Form.

Thanks. Yes that was an error in the function name.

What if I need to assign courses to users later via a separate form and provide functionality to edit or delete ?

I also have some other columns that i need to fill in like: start date, end date etc

Activity icon

Started a new Conversation Resource Controller For Many To Many Pivot Table To Enter Data With Separate Form.

I have a many to many relations applied between third table courses_users via models .

I want to do data entry CRUD in joined many to many table via a separate forms from a resource controller actions and separate model . Is it possible ??

Table 1 : users

Table 2: courses

Join Table : courses_users

Model: User.php

public function courses(){
        return $this->belongsToMany('App\Course', 'courses_users', 'user_id', 'course_id');
    }

Model: Course.php

public function courses() {
        return $this->belongsToMany('App\User', 'courses_users', 'course_id', 'user_id');
    }

Model: CourseUser.php

Now I want to ask can I create a resource controller for this?

If I can, should I need to provide any function which define the relation in the CourseUser.php model ??

Apr
18
5 months ago
Activity icon

Started a new Conversation Why Is Default Storage Engine MyISAM In Laravel Migrations

I noticed, Laravel creates tables via storage engine as MyISAM which does not support relational features.

I have setup the foreign keys via migrations but when I checked in Phpmyadmin, it shows no relational features due to storage engine being MyISAM.

I have read that if I change to InnoDB the relationships can be applied.

But my question is if I cannot add relationships, then why Laravel took that storage engine as MyISAM ? Is there no need to put relationships in database ?? That would make my database vulnerable.

What is the best practice as per Laravel ?