LiamA

LiamA

Member Since 3 Months Ago

Experience Points
490
Total
Experience

4,510 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
490 XP
Jun
30
1 week ago
Activity icon

Started a new Conversation Serving Non Ascii File Names

In my /public/images/ folder I have some image files. Some of these files are in Hebrew and I can't get them to be served by Laravel, so

http://localhost/images/abcd.png

works (image displayed by the browser), but

http://localhost/images/אבגד.png

does not (404 error). I should say that I have no problem using Hebrew characters in url slugs.

Any idea how this issue might be fixed?

Jun
28
1 week ago
Activity icon

Replied to Can't Sort A Merged Collection

OK, I figured it out.

When I traverse $sorted using

foreach($sorted as $key => $value) 

I get the results properly sorted. The original $key for each $value is kept, hence the keys are not longer incremental.

Activity icon

Replied to Can't Sort A Merged Collection

Sorry, I copy and pasted the wrong code.

$sorted = $collection->sortByDesc('pivot.order');

Just like you wrote. Still doesn't get sorted

Jun
27
1 week ago
Activity icon

Replied to Can't Sort A Merged Collection

The output for this code:

$pageVids = $page->videos;
$pageTexts = $page->texts;
$collection = collect();
foreach ($pageVids as $pageVid) $collection->push($pageVid);
foreach ($pageTexts as $pageText) $collection->push($pageText);
$collection->sortByDesc('pivot.order');
return $sorted[0]->pivot->order .' | '. $sorted[1]->pivot->order .' | '. $sorted[2]->pivot->order;

is

2 | 1 | 3

(There is a total of 3 items in the collection)

Activity icon

Replied to Can't Sort A Merged Collection

Thanks Snapey,

But I just tried what you suggested, and $sorted is exactly the same $collection

Activity icon

Replied to Can't Sort A Merged Collection

Sorry about that, just edited my post accordingly

Activity icon

Started a new Conversation Can't Sort A Merged Collection

I have a Page model , a Video model and a Text model.

A page can contain several videos and texts, therefor the relationships are set as follows:

public function videos() {
	$this->belongsToMany(\App\Models\Video::class, 'page_video')->withPivot('order');
}

public function texts() {
	$this->belongsToMany(\App\Models\Text::class, 'page_text')->withPivot('order');
}

'page_video' and 'page_text' are the pivot tables. Both tables have an 'order' field.

I get all videos and texts for a centerin page:

$page = App\Models\Page::find(1)
$pageVids = $page->videos;
$pageTexts = $page->texts;

Then I merge them into one collection, and want to sort them based of the 'order' field:

$collection = collect();
foreach ($pageVids as $pageVid) $collection->push($pageVid);
foreach ($pageTexts as $pageText) $collection->push($pageText);
$collection->sortByDesc('pivot.order');

but the order if the items never changes, and the collection always stays the same.

I should say that I've also used several other techniques to merge $pageVids and $pageTexts. I always get the merged collection with all values, including an 'order' field, nested within the 'pivot' field. But the sorting never works.

I would be very appreciative if anybody could explain to me why that happers.

Jun
06
1 month ago
Activity icon

Replied to Pivot Table Ignored On Many-to-Many Relationship

You were right tykus. It needed another migration. Since I've run migrate:fresh several times I was sure everything will be up to date, but I guess I don't fully understand the migration process. It'll come with time I guess. Thanks for your help!

Jun
05
1 month ago
Activity icon

Replied to Pivot Table Ignored On Many-to-Many Relationship

Thanks tykus!

I now understand the logic of what has to be done. Yet, something still doesn't work..

If I do this:

$page = factory('App\Models\Page')->create();

then

$video = factory('App\Models\Video')->create();

and then as you suggested

$page->videos()->attach($video->id);

I get the following error:

Illuminate/Database/QueryException with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testing.page_video' doesn't exist (SQL: insert into `page_video` (`page_id`, `video_id`) values (10, 35))'

Am I defining the belongsToMany() relationship properly?

public function videos()
{
    return $this->belongsToMany(Video::class,'page_video');
}

public function pages()
{
    return $this->belongsToMany(Page::class,'page_video');
}
Activity icon

Started a new Conversation Pivot Table Ignored On Many-to-Many Relationship

I have a Page model and a Video Model. A video can appear on multiple pages, and a page can display several videos. This is a Many-to-Many relationship from what I understand from the Laravel documentation.

For that I have a pages_videos migration file that acts as a pivot table:

public function up()
{
    Schema::create('page_video', function (Blueprint $table) {
        $table->unsignedBigInteger('page_id');
        $table->unsignedBigInteger('video_id');
    });

    Schema::table('page_video', function (Blueprint $table) {
        $table->foreign('page_id')->references('id')->on('pages');
        $table->foreign('video_id')->references('id')->on('videos');
    });
}

The Page model:

public function videos()
{
    return $this->belongsToMany(Video::class);
}

The Video model:

public function pages()
{
    return $this->belongsToMany(Page::class);
}

When I try to add a page via a factory, with a video_id set to some value i get an error:

Column not found: 1054 Unknown column 'video_id' in 'field list' (SQL: insert into `pages`...

Same happens when I try to create a video with a page_id

I also tried adding the pivot table to the belongsToMany() methods:

public function videos()
{
    return $this->belongsToMany(Video::class,'page_video');
}

public function pages()
{
    return $this->belongsToMany(Page::class,'page_video');
}

So it seems Laravel is unaware of the pivot table and tries to enter the values of page_id and video_id to the main tables instead.

What step am I missing I here?

May
26
1 month ago
Activity icon

Started a new Conversation Migration File Of A Model That Extends Anothe Model

Suppose that I have a site with several types of content types published. A basic page has a title and some text content, and then I have a video page that also displays a video.

I have a Page model and a pages migration file with title and body fields. I want to also define a Video model that will extend the Page model. Should the migration file of the video only have the video url field. Is that possible/does that make sense? Or does the migration file of the video (and all other content types) needs to have a title and body fields too?

May
03
2 months ago
Activity icon

Started a new Conversation Date Serialization In Observer

instructions here demonstrate how to set pre Laravel 7 date format on a model: https://laravel.com/docs/7.x/upgrade#date-serialization

How can I do the same for a project observer?

Apr
06
3 months ago
Activity icon

Replied to Using Default Factory Property Value For Other Properties Within A Factory

Perfect!

Thanks etiennedeschenes, just what I was looking for

Activity icon

Started a new Conversation Using Default Factory Property Value For Other Properties Within A Factory

Say that I use a factory to create a model that has 2 properties:

$factory->define(MyModel::class, function (Faker $faker) { return [ 'property1' => $faker->numberBetween(1,10), 'property2' => $faker->numberBetween(1,10) ] }

If I set the value of property1: factory('App\MyModel')->create(['property1' => 5]), but also want to use that value in order to calculate the value of property2 (for example have property2 equal property1 + 10), how do I access it inside the factory?

If I want to use the value of property1 for calculating property2 I need to set a default value inside of the factory (for when no value is set via the create() method). But that default value will ALWAYS be used for property2, no matter if a value is passed to create() or not.

For example:

$factory->define(MyModel::class, function (Faker $faker) { $property1 = $faker->numberBetween(1,10); return [ 'property1' => $property1, 'property2' => $property1 * 2 ] }

factory('App\MyModel')->create(['property1' => 5])

Let's say the random number inside the function is 3, then the values within the newly created model will be: property1 = 5 property1 = 6 (3x2)

Any idea how I get property2 to use the property1 value passed to create()?

Apr
01
3 months ago
Activity icon

Started a new Conversation Pivot Table For Two Classes That Extend The User Class

Pivot table for two classes that extend the User class

Hi, I'm new to Laravel and I'm trying to figure out how to properly use a pivot table. I have two models that extend the User model: Teacher and Student. They both use the 'users' table in database.

class Teacher extends User { public function students(){ return $this->belongsToMany(Student::class); } }

class Student extends User { }

There is also a pivot table named 'student_teacher' with the relationships between students and teachers. When I create model instances and use attach() to attach instances of the other model, the pivot table is populated with their ids (teacher_id and student_id).

My problem is that when I try to make a query with some relational coniditions, such as:

students: Student::has('teachers') )

I get the following SQL error:

Column not found: 1054 Unknown column 'laravel_reserved_1.teacher_id' in 'where clause' (SQL: select * from users where exists (select * from users as laravel_reserved_1 where users.id = laravel_reserved_1.teacher_id) and type = teacher)'

So I understand that Laravel is searching for the 'teacher_id' column in the users table instead of in the pivot table where that data is stored. How do I fix this?