naekobest

naekobest

Member Since 3 Months Ago

Experience Points
530
Total
Experience

4,470 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
0
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
530 XP
Mar
09
2 months ago
Activity icon

Started a new Conversation Routing Question

Hello! I dont know how to get forward with my problem.

I have 2 careers options "mD" and "gD".

My view layout, for lets say "exam", is for each of the careers the same, only the input/output coming from my database does change for each career. So "mD" has different exams than "gD" has.

Now i want to seperate both like so

domain.com/md

domain.com/gd

I currently have my navigation setup like so

@if (\Request::is('md'))
--- navbar for md here ---
@elseif(\Request::is('gd'))
--- navbar for gd here ---

this actually work just fine, but here is my problem, how do i seperate my exam for each career, like;

domain.com/md/exam

domain.com/gd/exam

with only one view for exam

Feb
27
3 months ago
Feb
24
3 months ago
Activity icon

Replied to Store Multiple Relations In One Table

Version 1

SQLSTATE[HY000]: General error: 1364 Field 'exam_id' doesn't have a default value (SQL: insert into `subjects` (`name`, `abbr`, `user_id`, `updated_at`, `created_at`) values (Verwaltungsrecht, VwR, 1, 2020-02-24 16:22:26, 2020-02-24 16:22:26))

Version 2

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `subjects` (`name`, `abbr`, `updated_at`, `created_at`) values (Verwaltungsrecht, VwR, 2020-02-24 16:22:52, 2020-02-24 16:22:52))
Feb
23
3 months ago
Activity icon

Replied to Store Multiple Relations In One Table

Correct. Once created the teacher creating the subject will (for now) forever be the only one associated with it

Activity icon

Replied to Store Multiple Relations In One Table

It’s supposed to be the teacher, but I don’t have role auth. yet - I need to get the basics first

Activity icon

Replied to Store Multiple Relations In One Table

Exam_table is just a general container for subjects like math, english, german, sports and then i have topics table in which there are specific topics for each container

Exam -> has many subjects

Subject -> belongs to exam // Subject -> has many topics

Topic -> belongs to subject

Now i want each subject belong to an user creating it

Activity icon

Replied to Store Multiple Relations In One Table

Does not work because then my "belongsTo" relation to exam doesnt work anymore

Activity icon

Replied to Store Multiple Relations In One Table

I dont quite understand what you mean.

I just tested the relation on the exam table, i dont need the user_id there.

I need the user_id on my subject table. hence i deleted it on my exam table

Activity icon

Replied to Store Multiple Relations In One Table

Yea exam had it. I was just trying it out on exam. The problem now is the subject

Subject Model

public function user()
    {
        return $this->belongsTo(User::class);
    }

User Model

public function subjects()
    {
        return $this->hasMany(Subject::class);
    }

Subject Controller

public function store(Exam $exam)
    {
        $data = request()->validate([
            'name' => 'required',
            'abbr' => 'required'
        ]);

        $subject = $exam->subjects()->create($data);

        return redirect('/exam/'.$exam->id);
    }
Activity icon

Started a new Conversation Store Multiple Relations In One Table

Im currentlly struggling with relations (again)

This

$exam = auth()->user()->exams()->create($this->validateData());
protected function validateData()
    {
        return request()->validate([
            'name' => 'required',
            'lehrgang' => 'required',
            'abbr' => 'required'
        ]);
    }

works just fine to put the user_id into my exam table. I just did it for testing purpose...

Now i want to use it on my subject table

Subject Controller

public function store(Exam $exam)
    {
        $data = request()->validate([
            'name' => 'required',
            'abbr' => 'required'
        ]);

        $subject = $exam->subjects()->create($data);

        return redirect('/exam/'.$exam->id);
    }

I dont understand how to put php auth()->user()this into my function. The subject belongsTo an exam (which already works just fine without the user_id wannabe implementation)

Activity icon

Started a new Conversation Authentication, Best Way To Block Access For Guests?

Hey! So currently im blocking all access for guests in my controller

Controller Example

<?php

namespace App\Http\Controllers;

Use App\Exam;
Use App\Subject;
Use App\Topic;

use Illuminate\Http\Request;

class ExamController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $exams = Exam::with(['subjects'])->get();

        return view('exam.index', compact('exams'));
    }

    public function create()
    {
        return view('exam.create');
    }

    public function store()
    {
        $exam = Exam::create($this->validateData());

        return redirect('/exam');
    }

    public function show(Exam $exam)
    {
        $exam->load('subjects');

        return view('exam.show', compact('exam'));
    }

    public function edit()
    {
        
    }

    public function update(Exam $exam)
    {
        $exam->update($this->validateData());

        return redirect('exam');
    }

    public function destroy()
    {
        
    }

    protected function validateData()
    {
        return request()->validate([
            'name' => 'required',
            'lehrgang' => 'required',
            'abbr' => 'required'
        ]);
    }
}

but i cant figure out how to only apply the function to specific methods like create/store/edit/destory.

I could use php ->middleware('auth') behind each route in my web.php but this looks like not the right way to do it

Activity icon

Replied to SQLSTATE[HY000] - No Default Value, I Dont Want A Default

Hardcoding works just fine https://i.gyazo.com/9f59650e42063ac0a524ddfb1f1e173c.png

Regarding the unique, so it would be enough to validate it in the controller and not in the migration?

Activity icon

Replied to SQLSTATE[HY000] - No Default Value, I Dont Want A Default

@lbecket I dont understand why its null, im actually choosing something that i want the form to store in my table

@a4ashraf

public function up()
    {
        Schema::create('subjects', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('exam_id');
            $table->string('name')->unique();
            $table->string('abbr')->unique();
            $table->timestamps();
            $table->softDeletes();
        });
    }

@snapey Ive been following an tutorial 1:1 and the guy basically showcased it like this, it worked and i stuck with it

Feb
22
3 months ago
Activity icon

Started a new Conversation SQLSTATE[HY000] - No Default Value, I Dont Want A Default

Regardless of what im trying, im getting this error message SQLSTATE[HY000]: General error: 1364 Field 'abbr' doesn't have a default value. Its not supposed to be null nor nullable. I dont quite understand why name.name does work yet abbr doesnt want to get stored.

I want to store the full name from my subject and the abbreviation.

Exam Model

class Exam extends Model
{
    protected $guarded = [];

    public function subjects()
    {
        return $this->hasMany(Subject::class);
    }
}

Subject Model

class Subject extends Model
{
    protected $guarded = [];

    public function exam()
    {
        return $this->belongsTo(Exam::class);
    }

    public function topics()
    {
        return $this->hasMany(Topic::class);
    }
}

Subject Controller (Store)

 public function store(Exam $exam)
    {
        $data = request()->validate([
            'name.name' => 'required',
            'abbr' => 'required'
        ]);

        $subject = $exam->subjects()->create($data['name'], ['abbr']);

        return redirect('/exam/'.$exam->id);
    }

Subject.Create

@extends('layouts.app')

@section('content')
    <div class="container">
        <form action="/exam/{{ $exam->id }}/subject" method="post">
            @csrf
            
            <div class="form-group">
                <label for="name">Name</label>
                <input name="name[name]" type="text" class="form-control" id="name" aria-describedby="nameHelp" value="{{ old('name.name') }}" autocomplete="off">
                <small id="nameHelp" class="form-text text-muted">Unterrichtsfach für <strong>Klausur: {{ $exam->name }}</strong></small>

                @error('name.name')
                <small class="text-danger">{{ $message }}</small>
                @enderror
            </div>

            <div class="form-group">
                <label for="abbr">Abkürzung</label>
                <input name="abbr" type="text" class="form-control" id="abbr" aria-describedby="abbrHelp" value="{{ old('abbr') }}" autocomplete="off">
                <small id="abbrHelp" class="form-text text-muted">Abkürzung</small>

                @error('abbr')
                <small class="text-danger">{{ $message }}</small>
                @enderror
            </div>

            <button type="submit" class="btn btn-primary">Create Subject</button>
        </form>
    </div>
@endsection
Activity icon

Replied to ForElse Single Ouput Instead Of Multipe Outputs - Problem

If i do it like this

<tbody>
          @forelse ($exams as $exam)
            @foreach ($exam->subjects as $subject)
                <tr>
                  <td>{{ $subject->topic->name }}</td>
                  <td>{{ $subject->name }}</td>
                  <td>{{ $subject->exam->name }}</td>
                  <td class="w-25 p-3">
                    <li type="button" class="btn btn-primary btn-sm">Show</li>
                    <li type="button" class="btn btn-primary btn-sm">Edit</li>
                    <li type="button" class="btn btn-danger btn-sm">Delete</li>
                  </td>
                </tr>
            @endforeach 
          @empty
              <p class="text-center h5">Keine Themen vorhanden!</p>
          @endforelse
        </tbody>

I do get the error from <td>{{ $subject->topic->name }}</td>i cant load $topic->name from either subject->topic->name nor topic->name in this case (with 1 foreach less) Trying to get property 'name' of non-object (View:

Activity icon

Replied to ForElse Single Ouput Instead Of Multipe Outputs - Problem

One last thing, kinda off topic to this problem

<div class="col-md-12 mt-4">
        <p class="h1 text-center">Übersicht Themen</p>
        <table class="table table-striped">
        <thead>
          <tr>
            <th scope="col">Name</th>
            <th scope="col">Fach</th>
            <th scope="col">Klausur</th>
            <th scope="col">Action</th>
          </tr>
        </thead>
        <tbody>
          @forelse ($exams as $exam)
            @foreach ($exam->subjects as $subject)
              @foreach ($subject->topics as $topic)
                <tr>
                  <td>{{ $topic->name }}</td>
                  <td>{{ $topic->subject->name }}</td>
                  <td>{{ $topic->subject->exam->name }}</td>
                  <td class="w-25 p-3">
                    <li type="button" class="btn btn-primary btn-sm">Show</li>
                    <li type="button" class="btn btn-primary btn-sm">Edit</li>
                    <li type="button" class="btn btn-danger btn-sm">Delete</li>
                  </td>
                </tr>
              @endforeach
            @endforeach 
          @empty
              <p class="text-center h5">Keine Themen vorhanden!</p>
          @endforelse
        </tbody>
        </table>
      </div>
    </div>

This cant possibly be the right way to access topics or? This looks kinda hideous, but its works haha

Activity icon

Replied to ForElse Single Ouput Instead Of Multipe Outputs - Problem

Thank you so much for your help! :)

Activity icon

Replied to ForElse Single Ouput Instead Of Multipe Outputs - Problem

I just dont understand relationships :( - It makes sense after reading the correct answer, probably would have never gotten to the point of using forelse/each

public function index()
    {
        $exams = Exam::with(['subjects'])->all();

        //dd($exams);

        return view('welcome', compact('exams'));
    }

gave me Call to undefined method Illuminate\Database\Eloquent\Builder::all() but using

public function index()
    {
        $exams = Exam::all();

        //dd($exams);

        return view('welcome', compact('exams'));
    }

worked for me

Activity icon

Replied to ForElse Single Ouput Instead Of Multipe Outputs - Problem

Ok, so ive re-done it with exam as starting point

@extends('layouts.app')

@section('content')
    <main role="main" class="container">
        <div class="container" style="display:inline-flex;">
            <div class="row w-100">
                <div class="col align-items-stretch">
                    <div class="row justify-content-center mt-4">
                        <p class="h1">Einführungslehrgang</p>
                    </div>
                    @forelse ($exams as $exam)
                    @if ($exam->lehrgang == 'Einführungslehrgang')
                      <div class="col-md-12 mt-4">
                        <div class="card">
                            <div class="card-body">
                                <h5 class="card-title font-weight-bold font-weight-bold">{{ $exam->name }}</h5>
                                <p class="card-text">
                                    <table class="table table-striped">
                                      <tbody>
                                        <tr>
                                          <td><a href="{{ route('subject') }}/{{ $exam->subject->id }}">{{ $exam->subject->name }}</a></td>
                                          <td><p class="card-text text-right"><small class="text-muted">Stand: {{ $exam->subject->updated_at->format('m/Y') }}</small></p></td>
                                        </tr>
                                      </tbody>
                                    </table>
                                </p>
                            </div>
                        </div>
                    </div>
                    @endif
                    @empty
                        Its empty in here :(
                    @endforelse

But im getting

Trying to get property 'id' of non-object

I know for a fact (at least) that $subject is the problem, when i comment it out from the view im getting a blank page and only {{ $exam->name }}is doing its job https://i.gyazo.com/d34ffe932209c1cd878c20d0015e9cf1.png

My controller is looking like that

public function index()
    {
        $exams = Exam::with(['subjects']);
        $exams = Exam::all();

        //dd($exams);

        return view('welcome', compact('exams'));
    }

i have both $exams in there to test around but it doesnt matter right now, they both work in their own way, that im probably not understanding

Activity icon

Replied to ForElse Single Ouput Instead Of Multipe Outputs - Problem

Thank you very much, ill try to re-do with with exam as starting point!

Kinda makes more sense since ive drawn it on paper

Activity icon

Replied to ForElse Single Ouput Instead Of Multipe Outputs - Problem

Oh sorry, i didnt update the table, here we go https://i.gyazo.com/debcecaf5380e8c84caeec7bade6272c.png IDs 3 and 9 relate to exam_id 3

Activity icon

Replied to ForElse Single Ouput Instead Of Multipe Outputs - Problem

I dont know exactly what you mean? Sorry

Basically everything is working "just fine" except that the foreach gives me one single output instead the complete tabble

Activity icon

Replied to ForElse Single Ouput Instead Of Multipe Outputs - Problem

@sinnbeck Thats how it should like with the database populated right now https://i.gyazo.com/b7424d2c3096e7ddaadad46bf510ab8c.png

@snapey exam model

class Exam extends Model
{
    protected $guarded = [];

    public function subjects()
    {
        return $this->hasMany(Subject::class);
    }
}

subject model

class Subject extends Model
{
    protected $guarded = [];

    public function exam()
    {
        return $this->belongsTo(Exam::class);
    }

    public function topics()
    {
        return $this->hasMany(Topic::class);
    }
}

topic model

class Topic extends Model
{
    protected $guarded = [];

    public function subject()
    {
        return $this->belongsTo(Subject::class);
    }
}

controller

    public function index()
    {
        $exams = Exam::all();
        $subjects = Subject::all();
        $topics = Topic::all();

        //dd($subjects);

        return view('welcome', compact('exams', 'subjects', 'topics'));
    }
Activity icon

Started a new Conversation ForElse Single Ouput Instead Of Multipe Outputs - Problem

Hey there! Im currently trying to get behind why on earth there is only one output in my forelse loop

Thats what it looks like https://i.gyazo.com/f67f297054f22c3a9cc0fdd1c8d7a22e.png

populated exam table https://i.gyazo.com/3d472807740e23dd17af9117e0e38a7c.png

populated subject table https://gyazo.com/6c7455af1060e0bc103d836538ea43cd

populated topics table https://gyazo.com/0d9d7c0dffe9d0651170d12d926802d0

For each topic belonging to the/an exam i want a list item There should be 2 subjects for exam_id 3 (Vollzugsrecht)

@extends('layouts.app')

@section('content')
    <main role="main" class="container">
        <div class="container" style="display:inline-flex;">
            <div class="row w-100">
                <div class="col align-items-stretch">
                    <div class="row justify-content-center mt-4">
                        <p class="h1">Einführungslehrgang</p>
                    </div>
                    @forelse ($topics as $topic)
                    @if ($topic->subject->exam->lehrgang == 'Einführungslehrgang')
                      <div class="col-md-12 mt-4">
                        <div class="card">
                            <div class="card-body">
                                <h5 class="card-title font-weight-bold font-weight-bold">{{ $topic->subject->exam->name }}</h5>
                                <p class="card-text">
                                    <table class="table table-striped">
                                      <tbody>
                                        <tr>
                                          <td><a href="{{ route('subject') }}/{{ $topic->subject->id }}">{{ $topic->subject->name }}</a></td>
                                          <td><p class="card-text text-right"><small class="text-muted">Stand: {{ $topic->subject->updated_at->format('m/Y') }}</small></p></td>
                                        </tr>
                                      </tbody>
                                    </table>
                                </p>
                            </div>
                        </div>
                    </div>
                    @endif
                    @empty
                    @endforelse
                </div>
            </div>
        </div>
    </main>
@endsection
Feb
21
3 months ago
Activity icon

Replied to Property [name] Does Not Exist On This Collection Instance.

Thank you very much! That worked! :)

Now onto the next problem haha

Activity icon

Replied to Property [name] Does Not Exist On This Collection Instance.

class Topic extends Model
{
    protected $guarded = [];

    public function subjects()
    {
        return $this->hasMany(Subject::class);
        // return $this->belongsToManu(Subject::class); // just in case you have many-many relation
    }
}

is what he suggested before

Topics DB https://i.gyazo.com/6290ea1b3f6b23ffef14c72569bf3801.png

Subjects DB https://i.gyazo.com/dc8e7ff3d8ab760c4fe1cdef3e2fde36.png

Only 2 options, i completly misunderstand the whole thing with relationships or i messed up somewhere in my code, i legit cant wrap my head around whats going on anymore

Activity icon

Replied to Property [name] Does Not Exist On This Collection Instance.

Thank you! I've updated that.

To your answer before: Why do i need to make it to "hasMany". The one topic i have, belongs to an subject, and the subject has many topics?! - or am i like THAT confused right now?

Now im getting "Trying to get property 'name' of non-object"

@forelse($topics as $topic)
                    <tr>
                        <td><a href="{{ route('topic') }}/{{ $topic->id }}">{{ $topic->name }}</a></td>
                        <td>
                        {{ $topic->subjects->name }} 
                        </td>
                        <td>
                            <li type="button" class="btn btn-primary btn-sm">Show Subjects</li>
                            <li type="button" class="btn btn-info btn-sm">Edit</li>
                            <li type="button" class="btn btn-danger btn-sm">Delete</li>
                        </td>
                    </tr>
                @empty
                    <p>Keine Fächer vorhanden.</p>
                @endforelse
Activity icon

Replied to Property [name] Does Not Exist On This Collection Instance.

Oops, my bad

    public function index(Topic $topic)
    {
        
        $topics = Topic::paginate(10);

        $topic->load('subjects');

        return view('topic.index', compact('topics'));
    }
Activity icon

Started a new Conversation Property [name] Does Not Exist On This Collection Instance.

Hello there, im stuck for almost 3days now on this error and i cant seem to get my head around on how to fix it.

Method Illuminate\Database\Eloquent\Collection::subjects does not exist. (View:\resources\views\topic\index.blade.php)

Im essentially trying to bring Exam in a relationship to subjects in a relationship to topics. Exams have many subjects, subjects have many topics Topics belong to subjects, subjects belong to exams

Exam Model

class Exam extends Model
{
    protected $guarded = [];

    public function subjects()
    {
        return $this->hasMany(Subject::class);
    }
}

Subject Model

class Subject extends Model
{
    protected $guarded = [];

    public function exams()
    {
        return $this->belongsTo(Exam::class);
    }

    public function topics()
    {
        return $this->hasMany(Topic::class);
    }
}

Topic Model

class Topic extends Model
{
    protected $guarded = [];

    public function subjects()
    {
        return $this->belongsTo(Subject::class);
    }
}

Topic Controller

    public function index(Topic $topic)
    {
        
        $topics = Topic::paginate(10);

        $topic->load('subjects');

        return view('topic.index', compact('topics'));
    }

Topic Index

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-12 mt-4">
            <p class="h1 text-center">Übersicht Fächer</p>
            <table class="table table-striped">
            <thead>
              <tr>
                <th scope="col">Name</th>
                <th scope="col">Fach</th>
                <th scope="col">Aktion</th>
              </tr>
            </thead>
              <tbody>
                @forelse($topics as $topic)
                    <tr>
                        <td><a href="{{ route('topic') }}/{{ $topic->id }}">{{ $topic->name }}</a></td>
                        <td>
                            {{-- @foreach ($topics->subjects()->name as $subject)
                                {{ $subject->name }}
                            @endforeach --}}
                        </td>
                        <td>
                            <li type="button" class="btn btn-primary btn-sm">Show Subjects</li>
                            <li type="button" class="btn btn-info btn-sm">Edit</li>
                            <li type="button" class="btn btn-danger btn-sm">Delete</li>
                        </td>
                    </tr>
                @empty
                    <p>Keine Fächer vorhanden.</p>
                @endforelse
              </tbody>
            </table>
            <div>
                <a class="btn btn-primary" href="{{ route('subject') }}">New subject</a>
                <p class="mx-auto">{{ $topics->links() }}</p>
            </div>    
        </div>
    </div>
 </div>
@endsection

Under "Fach" (subject) i want to display the corresponding subject name to each topic