lundi 23 septembre 2019

Looping through result of relational tables in laravel?

I do have a problem with the out of this code this is the models used

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Navigation extends Model
{
    protected $table = "navigation" ;
    protected $primaryKey = "navi_id";

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

and this how the query goes

        <?php

    namespace App\Http\Controllers;

    use App\Navigation;
    use App\PrimaryNavigation;
    use Illuminate\Http\Request;

    class NavigationController extends Controller
    {
        /**
         * Display a listing of the resource.
         *
         * @return \Illuminate\Http\Response
         */
        public function index()
        {
             $mainNavi = Navigation::with('PrimaryNavigation')->get() ;
             return view('dump')->with('data',$mainNavi);
        }
}
?>

Whenever i try to print out the result coming from this query it never showing the correct output from the related table . when i print the output it shows correct result but when i tried to print out the related table data only it shows NULL value

@foreach($data as $single)
 </br>
@endforeach

this code shows the correct output

{"navi_id":8,"navi_name":"fouad","navi_route":"\/fouad","navi_sort":0,"navi_created_at":"2019-09-23 02:21:24.000000","navi_last_update":"2019-09-23 02:21:29.000000","primary_navigation":[{"primary_navi_id":5,"navigation_navi_id":8,"primary_navi_name":"tamer","primary_navi_route":"\/tamer","primary_navi_created_at":"2019-09-23 02:28:38.000000","primary_navi_last_update":"2019-09-23 02:28:40.000000","primary_navi_sort":0},{"primary_navi_id":13,"navigation_navi_id":8,"primary_navi_name":"momo","primary_navi_route":"\/momo","primary_navi_created_at":"2019-09-23 14:44:13.000000","primary_navi_last_update":"2019-09-23 14:44:15.000000","primary_navi_sort":0}]}
{"navi_id":9,"navi_name":"mohamed","navi_route":"\/mohamed","navi_sort":1,"navi_created_at":"2019-09-23 14:09:30.000000","navi_last_update":"2019-09-23 14:09:33.000000","primary_navigation":[{"primary_navi_id":6,"navigation_navi_id":9,"primary_navi_name":"soso","primary_navi_route":"\/SOSO","primary_navi_created_at":"2019-09-23 14:10:33.000000","primary_navi_last_update":"2019-09-23 14:10:36.000000","primary_navi_sort":0},{"primary_navi_id":14,"navigation_navi_id":9,"primary_navi_name":"yoyo","primary_navi_route":"\/yoyo","primary_navi_created_at":"2019-09-23 14:44:34.000000","primary_navi_last_update":"2019-09-23 14:44:37.000000","primary_navi_sort":0}]}
{"navi_id":10,"navi_name":"yasser","navi_route":"\/yasser","navi_sort":2,"navi_created_at":"2019-09-23 14:09:43.000000","navi_last_update":"2019-09-23 14:09:45.000000","primary_navigation":[{"primary_navi_id":7,"navigation_navi_id":10,"primary_navi_name":"mathy","primary_navi_route":"\/math","primary_navi_created_at":"2019-09-05 14:10:57.000000","primary_navi_last_update":"2019-10-09 14:10:59.000000","primary_navi_sort":0}]}
{"navi_id":11,"navi_name":"mahy","navi_route":"\/mahy","navi_sort":3,"navi_created_at":"2019-09-23 14:10:00.000000","navi_last_update":"2019-09-23 14:10:02.000000","primary_navigation":[{"primary_navi_id":8,"navigation_navi_id":11,"primary_navi_name":"toty","primary_navi_route":"\/toyt","primary_navi_created_at":"2019-09-23 14:11:20.000000","primary_navi_last_update":"2019-09-23 14:11:23.000000","primary_navi_sort":0},{"primary_navi_id":12,"navigation_navi_id":11,"primary_navi_name":"JAJA","primary_navi_route":"\/JAJ","primary_navi_created_at":"2019-09-23 14:43:54.000000","primary_navi_last_update":"2019-09-23 14:43:57.000000","primary_navi_sort":0}]}

but when i tried to show the related table only it gives null

@foreach($data as $single)
 </br>
@endforeach


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire