SM_Berwari
SM_Berwari

Reputation: 691

PHP : update related id in laravel

I have this two models : first for categories which connected with courses in id:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Catagory extends Model
{
    use HasFactory;

    protected $fillable = [
        'catagory',
    ];

    public function Course()
    {
        return $this->hasMany('App\Models\Course');
    }
}

the course model have belong to catagory table :

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    use HasFactory;
    protected $fillable = [
        'course_name',
        'trainer_id',
        'catagory_id',
        'price',
        'video_link',
        'image_link',
    ];
// belongs
    public function Catagory()
    {
        return $this->belongsTo('App\Models\Catagory','catagory_id');
    }

// has
    public function Lession()
    {
        return $this->hasMany('App\Models\Lession');
    }
    public function Order()
    {
        return $this->hasMany('App\Models\Order');
    }
}

Now I made controller to update the course ,but the category name is send with request instead of category id and i want to edit the category id in course table ,I made this method in course controller :

public function update(Request $request ,$id)
        {
            $course = Course::find($id);
            
            if ($request->ismethod('post')){
                $catagory = Catagory::where('catagory' , $request->catagory);
                $trainer = Trainer::where('trainer' , $request->trainer);
   
                $course->course_name = $request->course_name;
                $course->trainer_id = $trainer->Course->id;
                $course->catagory_id = $catagory->Course->id;
                $course->price = $request->price;
                $course->video_link=$request->video_link;
                $course->image_link=$request->image_link;

                $course->save();
                return back();
            }
            else {
                $arr['course']=$course;
                return view('admin.courses.course_update',$arr);
            }
        }

I know it is wrong , but what is the correct method to update category id depend on category name comes with request?

my routes :

Route::prefix('course')->group(function () {
        //Edit Courses
        Route::get('edit', [App\Http\Controllers\course\Course_controller::class,'index'])->name('edit_course');
        //Show courses to add
        Route::get('/', [App\Http\Controllers\course\Course_controller::class,'add'])->name('show_course');
        //Add Course
        Route::post('/', [App\Http\Controllers\course\Course_controller::class,'add'])->name('add_course');
        //Show Course to update
        Route::get('update/{id}', [App\Http\Controllers\course\Course_controller::class,'update'])->name('show_update_course');
        //Update Course
        Route::post('update/{id}', [App\Http\Controllers\course\Course_controller::class,'update'])->name('update_course');
        // Delete Course
        Route::get('delete/{id}', [App\Http\Controllers\course\Course_controller::class,'delete'])->name('delete_course');
    });

Upvotes: 0

Views: 163

Answers (1)

Bulent
Bulent

Reputation: 3411

You didn't get category or trainer. Add ->first() to the end of those lines.

I don't know why you create one-to-many relation between category and course, but if a course can have more category, you should go with many-to-many relations. If any other things besides course can have a category, you need many-to-many polymorphic relations.

For the sake of following the naming convention, it would be better if you use "courses" instead of Course in your Category model.

Upvotes: 1

Related Questions