Answers for "laravel return data from model to another controller"

PHP
1

laravel return data from model to another controller

Relation : Parent has many Childrens

ParentController:
	use App\Models\Children;

    public static function showChildrens($parent_id) {
        $children = new Children;
        $childrens = $children->scopeShow($parent_id);
        return compact('childrens');
    } 



Children Model:
	use App\Models\Parent;

	public function parent() {
        return $this->belongsTo(Parent::class);
    }

    public static function scopeShow($parent_id) {
        $childrens = Children::where('parent_id', $parent_id)->get();
        return $childrens;
    }

// Note: Don't use the word "parent" in your app as table it will sometimes create conflicts, this was just for the example
// Like the post if you found it usefull and help other devs to find the good answer
Posted by: Guest on March-09-2022

Code answers related to "laravel return data from model to another controller"

Browse Popular Code Answers by Language