behnampmdg3

behnampmdg3

Member Since 4 Years Ago

Experience Points 19,590
Experience Level 4

410 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 89
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

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

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

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

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    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 Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

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

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

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

23 Mar
2 days ago

behnampmdg3 started a new conversation How Many Rows Are Too Many Rows For Browsers?

How many rows are too many rows for browsers?

Can all browsers handle 1200 rows?

I am trying to avoid implementing "Load more Results" for many reasons.

I can allow the users to have 30 modules and 40 lessons per module.

For several reasons they gotta be able to see all in one page.

Can all browsers handle 1200 rows...

22 Mar
3 days ago

behnampmdg3 left a reply on Can I Minify This Code?

@ETTORE - Thanks but @ this point I don't have access to Laracasts series.

behnampmdg3 left a reply on Convert To Gate Or Policy?

@MARTINBEAN - Sorry we are off a bit haha I don't see anywhere you check this

if(auth()->id()=== $course->user_id)

behnampmdg3 left a reply on What Is The Proper Day To Do Batch Update With Laravel?

2 things.

1 - I think whereIn needs id column. 2 - It doesn't update.

public function sort(Request $request)
    {
        $modules = Module::whereIn('id', $request->modules)
                ->where('user_id', \Auth::id())
                ->get(['id','sort']);
        $modules->each(function($module, $key) 
            {
                echo $key;
                echo "<br />";
                echo $module;
                if($module->sort != $key) 
                    {
                        $module->sort = $key;
                        $module->save();
                    }
            });

prints

0
{"id":13,"sort":0}1
{"id":37,"sort":1}2
{"id":92,"sort":2}3
{"id":125,"sort":3}4
{"id":159,"sort":4}5
{"id":173,"sort":5}6
{"id":185,"sort":6}7
{"id":281,"sort":7}8
{"id":313,"sort":8}9
{"id":333,"sort":9}10
{"id":339,"sort":10}11
{"id":346,"sort":11}12
{"id":347,"sort":12}13
{"id":367,"sort":13}14
{"id":377,"sort":14}15
{"id":403,"sort":15}16
{"id":410,"sort":16}17
{"id":421,"sort":17}18
{"id":438,"sort":18}

behnampmdg3 started a new conversation Allowing Users To Create Sub Domain

Hi;

I am building this small membership portal and the way it works... each course author can build their own course and their users log in and consume the material.

It's in progress and I don't see any major issues however there is one thing I am not sure how to do.

I want to be able to give them specific sub domain. I have seen other sites do it automatically on sign up.

For example:

jeffway.goldmembers.com ben34.goldmembers.com tennishero.goldmembers.com

And so on.

And when someone logs in, everything is through that url. Fore example:

tennishero.goldmembers.com/course-slug tennishero.goldmembers.com/course-slug/modules tennishero.goldmembers.com/course-slug/module-slug/lesson-slut

Of course the code is the same, the "sub-domain" doesn't affect the code. The code works properly with sessions and ids like the blog Jeff shows.

Ideal would be to store the "user_subdomain" in this table.

CREATE TABLE `users` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

What is the best practice?

Thanks

behnampmdg3 started a new conversation How Can I Mix These 2 Eloquant Queries?

$this_users_course_counter = \App\Course::where('user_id', auth()->id())->count();

$courses = \Auth::user()->courses()->withCount('members')->withCount('modules')->orderBy('title', 'asc')->simplePaginate(10);
CREATE TABLE `courses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `description` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `users` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

behnampmdg3 left a reply on What Is The Proper Day To Do Batch Update With Laravel?

@SNAPEY - How would the performance be different between the 2 solutions above?

behnampmdg3 left a reply on How Can I Write This With Eloquant

Thanks guys. Thoughts on this?

$members = \DB::table('members')
        ->join('courses', function ($join) {
            $join->on('courses.id', '=', 'members.course_id')
                 ->where('courses.user_id', '=', auth()->id());
        })
         ->where(function($query) {
        $query->where('members.email', '=', Input::get('search_keyword'))
            ->orWhere('members.name', '=', Input::get('search_keyword'));
            })
        ->select('members.email','members.name','courses.title','members.created_at', 'members.id')
         ->get();

behnampmdg3 started a new conversation What Is The Proper Day To Do Batch Update With Laravel?

Hello;

This is what the app does https://ibb.co/R6qfjym

The users click up and down arrow and it updates the 'sort' column of the modules in the database.

It's working well however my code looks very questionable.

I don't really care if it's perfect code or not... my concern is the sql in loop.

Are these loops (both validation and update) avoidable at all?

public function sort()
    {
        print_r($_POST);
        foreach($_POST as $val=>$row)
            {
                foreach($row as $val)
                    {
                        $modules[]=$val;
                    }
            }
        $sort=0;
        foreach($modules as $module)
            {
                $sort++;
                $module = \App\Module::find($module);
                if($module->user_id==auth()->id())
                    {
                        \DB::table('modules')
                        ->where('id', $module->id)
                        ->update(['sort' => $sort]);           
                    }
            }
    }
$('.up_button').on('click', function () {
                    var hook = $(this).closest('.module_item').prev('.module_item');
                    var elementToMove = $(this).closest('.module_item').detach();
                    hook.before(elementToMove);
                    sort_modules=[];
                    $('.module_item[data-id]').each(function(index, val)
                        {
                           sort_modules.push($(this).attr('id'));
                        });
                    console.log(sort_modules);
                    //Post    
                    $.ajaxSetup({
                    headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                    });
                    $.ajax({
                        type:'post',
                        url: '/modules/sort',
                        data:  { modules: sort_modules }
                    });
                });

behnampmdg3 left a reply on Javascript Array Question

@STEFANR - Thanks

I am trying to learn this manual it says:

21 Mar
4 days ago

behnampmdg3 started a new conversation Javascript Array Question

Hi;

I need a name for my array so I can access it. How can I do this?

$('.up_button').on('click', function () {
                    var hook = $(this).closest('.module_item').prev('.module_item');
                    var elementToMove = $(this).closest('.module_item').detach();
                    hook.before(elementToMove);
                    sort_modules=[];
                    $('.module_item[data-id]').each(function(index, val)
                        {
                           sort_modules.push($(this).attr('id'));
                        });
                    //Post    
                    $.ajaxSetup({
                    headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    }
                    });
                    $.ajax({
                        type:'post',
                        url: '/modules/sort',
                        data: sort_modules
                    });
                });

In console I see

(3) ["20", "28", "21"]
0: "20"
1: "28"
2: "21"
length: 3
__proto__: Array(0)

Network (the controller) shows

class ModuleController extends Controller
{
    
    public function sort()
    {
        print_r($_POST);

prints

Array
(
    [undefined] => 
)

behnampmdg3 left a reply on Convert To Gate Or Policy?

@MARTINBEAN - Where do I put the logic?

$course = \App\Course::findOrFail($id);
        if(auth()->id()=== $course->user_id)
            {

behnampmdg3 left a reply on How Can I Serialize A Ul List?

Who do you cycle through < li data-id = 'ids'> ?

behnampmdg3 left a reply on ErrorException (E_ERROR) Call To A Member Function DiffForHumans() On String

Non worked! Weird! Same with this

$members = \DB::table('members')
        ->join('courses', function ($join) {
            $join->on('courses.id', '=', 'members.course_id')
                 ->where('courses.user_id', '=', auth()->id());
        })
         ->where(function($query) {
        $query->where('members.email', '=', Input::get('search_keyword'))
            ->orWhere('members.name', '=', Input::get('search_keyword'));
            })
        ->select('members.email','members.name','courses.title','members.created_at', 'members.id')
         ->get();

This

{{ $member->created_at }}

prints

2019-03-17 12:31:04

behnampmdg3 started a new conversation How Can I Write This With Eloquant

SELECT members.name, members.email, members.created_at, courses.title 
    FROM members
        INNER JOIN courses ON courses.id = members.course_id
            AND courses.user_id = auth()->id()
    WHERE members.email = 'Input::get('search_keyword') ' or name = 'Input::get('search_keyword')'
CREATE TABLE `members` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `course_id` bigint(20) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `member_course_email_course_id_unique` (`email`,`course_id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `courses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `description` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

behnampmdg3 started a new conversation How Can I Serialize A Ul List?

When user clicks up and down, I want to post the order of the items to a controller to update.

Front end is working but I am not sure how to serialize the new sorted list.

How can we do it?

Thanks

$('.up_button').on('click', function () {
                    var hook = $(this).closest('.module_item').prev('.module_item');
                    var elementToMove = $(this).closest('.module_item').detach();
                    hook.before(elementToMove);
                    
                });
                $('.down_button').on('click', function () {
                    var hook = $(this).closest('.module_item').next('.module_item');
                    if (hook.length) {
                        var elementToMove = $(this).closest('.module_item').detach();
                        hook.after(elementToMove);
                    }
                });
<ul class="sortable_modules">
     @forelse ($modules as $module)
          <li id = "module_{{ $module->id }}" class='module_item'>...

behnampmdg3 started a new conversation This Forum...

I just wanted to make this post and thank Jeff and everyone else who is contributing to this forum.

This is an exceptionally amazing community!

I don't code much, but with the help of this forum every time I have a project we succeed.

Thanks everyone and keep on the good work!

behnampmdg3 started a new conversation Sortable, Variable Loses Value After "stop"

This is very strange. The first console.log pronts 1 but the second one is totally different.

Does"stop" do something to the variables?

https://ibb.co/Np2tfFs

$( ".sortable_lessons" ).sortable({
        update: function( ) {
            var idsInOrder = $(this).sortable('serialize');
            var module_id= idsInOrder.substr(0, idsInOrder.indexOf('_')); 
            $.ajaxSetup({
            headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
            });
            $.ajax({
                type:'post',
                url: '/lessons/sort',
                data: idsInOrder
            });
            console.log(module_id)
        },
        stop: function () 
            {
              console.log(module_id)
              $('.sortable_lessons > li').each(function (i) {
              });
            }
    });
20 Mar
5 days ago

behnampmdg3 left a reply on Convert To Gate Or Policy?

@MARTINBEAN - cannot seems to be for blade. I am talking about controllers.

behnampmdg3 started a new conversation Convert To Gate Or Policy?

Hi

I am not sure what's the best practice but I want:

1 - If user tries an id that doesn't exist, get redirected. 2 - If user tries an id that doesn't belong to him, get redirected.

Is there any other validation needed?

Anyways the code below does that but it's not neat. I am not looking for perfect code but I wanna make sure it doesn't break when we go live.

Route::get('/course/members/{id}', '[email protected]');
public function members($id)
    {
        $course = \App\Course::findOrFail($id);
        if(auth()->id()=== $course->user_id)
            {
                $course = \App\Course::with('members')->where('courses.user_id', 3)->find($id);
                $members = $course->members;
                return view('members.show', ['members' => $members, 'course'=>$course]);    
            }
        else 
            {
                return redirect('/courses');
            }    
    }

I thought of

public function members(Course $course) 

so maybe I pass an instance to gates, but it didn't work out... doesn't seem to pass values.

Course {#300 ▼
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: []
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▶]
}

Recommendations?

behnampmdg3 left a reply on Can I Minify This Code?

Thanks guys! Both these seem to work nicely...

public function members($id)
    {
        $course = \App\Course::with('members')->find($id);
        $members = $course->members;
        return view('members.show', ['members' => $members, 'course'=>$course]);    
    }
    public function members(Course $course)
    {
        $members = $course->members;
        return view('members.show', ['members' => $members, 'course'=>$course]);    
        
    }

behnampmdg3 left a reply on Uncaught Error: Syntax Error, Unrecognized Expression:

First consloe.log prints the number correctly. 5 in this case.

behnampmdg3 started a new conversation Uncaught Error: Syntax Error, Unrecognized Expression:

Hi;

First consloe.log prints the number correctly however #module_counter_' + module_id produces the error. What am I missing?

Uncaught Error: Syntax error, unrecognized expression: #module_counter_[object HTMLCollection]
$( ".sortable_lessons" ).sortable({
        update: function( ) {
            var idsInOrder = $(this).sortable('serialize');
            var module_id= idsInOrder.substr(0, idsInOrder.indexOf('_')); 
            console.log(module_id);  //  First consloe.log
            $.ajaxSetup({
            headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
            });
            $.ajax({
                type:'post',
                url: '/lessons/sort',
                data: idsInOrder
            });
        },
        stop: function () 
            {
              $('.sortable_lessons > li').each(function (i) {
                $(this).find('#module_counter_' + module_id).html(i + 1);
              });
            }
    });

behnampmdg3 started a new conversation Can I Minify This Code?

Hi;

I get the course details + members who are signed up in this course in "members" method. Is this the right approach?

<?php

namespace App\Http\Controllers;

use App\Course;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;


class CourseController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }
    
public function members($id)
    {
        $members = \App\Course::find($id)->members;
        $course = \App\Course::find($id);
        return view('members.show', ['members' => $members, 'course'=>$course]);    
    }


CREATE TABLE `courses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `description` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `members` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `course_id` bigint(20) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `member_course_email_course_id_unique` (`email`,`course_id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

behnampmdg3 left a reply on What Is The Best Solution To Import List Of Customers To This Table?

I have never worked with CSV. Can you please link me to the docs in Laravel?

Thank you

behnampmdg3 started a new conversation What Is The Best Solution To Import List Of Customers To This Table?

Hi;

This software is for small businesses that have a list of customers.

1 - Which one is better? Get them upload Excel, CSV? 2 - The values get inserted to the members table, however they should assign the course_id to each value as well.

CREATE TABLE `members` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `course_id` bigint(20) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `member_course_email_course_id_unique` (`email`,`course_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `courses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `description` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Thanks

behnampmdg3 started a new conversation Do I Have To Pass Id As Hidden Input To Update?

Do I have to pass id as hidden input to update? Or can we somehow access it via $request?

public function update(Request $request)
    {
            $member = \App\Member::find($request->id);
            $member->name = $request->name;
            $member->email = $request->email;
            $member->save();
            return redirect('/members/'.$request->id."/edit")->with('status', 'Member updated!');
    }
<input type="hidden" class="form-control" name="id"  value="{{ $member->id }}">

behnampmdg3 left a reply on Eloquant Relationship Not Working Returning Some Weird $hit

Yeh it's got too messy. I'll come back with a new design. Things can get very hairy fast.

behnampmdg3 started a new conversation Eloquant Relationship Not Working Returning Some Weird $hit

Hey bros;

This is supposed to show the list of members who are in this course but it doesn't. I screenshot what it shows.

What am I doing wrong?

public function members($id)
    {
        $members = \App\Course::find($id)->members();
         dd($members);
    }

Prints https://ibb.co/1KMMy3F

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    public function modules()
    {
        return $this->hasMany('App\Module')->orderBy('sort');
    }
    public function members()
    {
        return $this->hasMany('App\MemberCourse')->orderBy('created_at');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class MemberCourse extends Model
{
     protected $table = 'member_course';

     public function course()
        {
            return $this->belongsTo('App\Course');
        }
}
CREATE TABLE `courses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `description` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `member_course` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `course_id` bigint(20) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `member_course_email_course_id_unique` (`email`,`course_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

behnampmdg3 started a new conversation What Is This Bug In Laracasts? Ironic

I am sure everyone knows... when we post is says 11h.

Don't really care... but how come Jeff doesn't fix it?

I've heard him commenting on little things in code like bad indentation etc give him anxiety.

He seems to have mild OCD like myself! I am surprized he can live with this 11h thing.

Is it a Laravel thing?

behnampmdg3 left a reply on Come On Laravel There Has To Be A Way To Do This The Right Way...

Thanks guys.

This seems to work.

$courses = \Auth::user()->courses()->withCount('members')->withCount('modules')->get();
19 Mar
6 days ago

behnampmdg3 left a reply on Query Returning Wrong Results

@SNAPEY - Sorry snapey that way typo. I fixed the question.

behnampmdg3 started a new conversation Query Returning Wrong Results

It's ignoring the "where" part.

Showing courses that the user_id is not the current logged in user. What am I doing wrong?

public function index()
    {
        echo auth()->id();
        $courses = \App\Course::withCount(['modules' => function ($query) {
            $query->where('user_id', auth()->id());
        }])->get();
        return view('courses.show', ['courses' => $courses]);
    }
CREATE TABLE `courses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `description` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

America is responsible for this.

behnampmdg3 left a reply on How Much Does It Cost To Hire Someone On Snapy Or Cronix Level?

I am not a coder bro. I run biz don't have time to do everything. I got a team, it's time to bring a Laravel fellow on board.

behnampmdg3 started a new conversation How Much Does It Cost To Hire Someone On Snapy Or Cronix Level?

I am considering someone to clean the code for me, add validations and discuss a few things.

1 - How to know the person is credible and legit?

2 - How much does it cost to hire someone on Snapy or Cronix level for a day?

3 - I don't do charge by hour. Quote must be charge by task.

Sweet

18 Mar
1 week ago

behnampmdg3 left a reply on SQLSTATE[23000]: Integrity Constraint Violation

@STEREOH - Yo;

I don't want to group by email , name and member_course.id

That gives me wrong results.

What is the right way to get unique emails?

behnampmdg3 left a reply on SQLSTATE[23000]: Integrity Constraint Violation

@MULUGU - That does not give correct results. Still duplicate emails.

behnampmdg3 left a reply on SQLSTATE[23000]: Integrity Constraint Violation

@MUNAZZIL - You missed the point. That's not what I am talking about.

I am retrieving data. Need to write a query that gets the results I want. Read it again plz.

behnampmdg3 started a new conversation SQLSTATE[23000]: Integrity Constraint Violation

Hello friends;

How can I get unique emails? Each email only once (group by doesn't work)...

CREATE TABLE `member_course` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `course_id` bigint(20) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
$members = \DB::table('member_course')
        ->join('courses', 'courses.id', '=', 'member_course.course_id')->where('courses.user_id', '=', auth()->id())
        ->select('email', 'name', 'member_course.id') 
        ->groupBy('email')
        ->get();
Illuminate \ Database \ QueryException (42000)
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'member_course.name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select `email`, `name`, `member_course`.`id` from `member_course` inner join `courses` on `courses`.`id` = `member_course`.`course_id` where `courses`.`user_id` = 3 group by `email`)```
17 Mar
1 week ago

behnampmdg3 left a reply on Is This The Correct Way Of Rewriting This Query In Eloquant?

You're not answering my question :)

I know there are many ways to do this.

Is my approach correct?

behnampmdg3 started a new conversation ErrorException (E_ERROR) Call To A Member Function DiffForHumans() On String

Hi what's up it's Monday and I get this error.

View

{{ $course->created_at->diffForHumans() }}

Value in DB

2019-03-17 12:31:04

Controller

$courses = \DB::table('courses')
            ->join('member_course', 'courses.id', '=', 'member_course.course_id')
            ->join('members', 'members.id', '=', 'member_course.member_id')
            ->where('members.id', '=', $member->id)
            ->select('courses.title','courses.id','member_course.created_at')
            ->get();

behnampmdg3 started a new conversation Come On Laravel There Has To Be A Way To Do This The Right Way...

Hello;

Hardcore Larafan here. But I am in shock or still in denial that I have to loop through queries.

Snapey ot some bad a$$ Laramaster tell me how to do it right way...

I wanna get a list of all courses that belong to the logged user along with the number of modules per course and members who have signed up for it.

Like this https://ibb.co/ckncQxM

I am not expert on Mysql but i am pretty sure this is doable with group_concat...

CREATE TABLE `courses` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `active` int(11) NOT NULL DEFAULT '1',
  `description` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `slug` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `modules` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `course_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `sort` int(11) NOT NULL,
  `release_after` int(11) NOT NULL DEFAULT '0',
  `active` int(11) NOT NULL DEFAULT '1',
  `description` text COLLATE utf8mb4_unicode_ci,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Member is student

CREATE TABLE `members` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `members_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=305 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE `member_course` (
  `member_id` bigint(20) NOT NULL,
  `course_id` bigint(20) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`member_id`,`course_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

User is the course author

CREATE TABLE `users` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `email_verified_at` timestamp NULL DEFAULT NULL,
  `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

behnampmdg3 left a reply on Is It Possible To Avoid This Loop Using Eloquant Relationships?

What about group concat? I am pretty sure there is a way to do this.

behnampmdg3 started a new conversation Is This The Correct Way Of Rewriting This Query In Eloquant?

Is this the correct way of rewriting this query in Eloquant?

SELECT name, email FROM members 
    INNER JOIN member_course 
        ON  members.id = member_course.member_id
    INNER JOIN courses 
        ON courses.id = member_course.course_id
            AND courses.user_id = 3
$members = \DB::table('members')
    ->join('member_course', 'members.id', '=', 'member_course.member_id')
    ->join('courses', 'courses.id', '=', 'member_course.course_id')->where('courses.user_id', '=', '3')
    ->select('name', 'email')
    ->get();```

behnampmdg3 left a reply on Unique Mix Columns?

Ok I go ahead with this:

$table->primary(['member_id', 'course_id']);

How does the model work for relationships like this?

I wanna make a factory but not sure how to build the model and factory (how to name etc).