Laravel - include few views in one with controllers - laravel

Im working on Laravel and i have problem.
I created two controllers: PostController - has a view and PostController has a view.
I created next Controller called HomeController and i want to execute both Controller here PostController and MyProfileController.
I created a method in HomeController:
public function index()
{
$profile_view = app('App\Http\Controllers\MyProfileController')->index();
$post_view = app('App\Http\Controllers\PostController')->index();
return view('home',
[
'profile_view' => $profile_view,
'post_view' => $post_view
]
);
}
And im trying to show in view (home.blade.php)
#extends('layout')
#section('main-content')
Something
{!! $profile_view !!}
{!! $post_view !!}
#endsection
and it is viewing only one view from $post_view.
Anyone has a idea for this problem?

You need to create another view that would use ®include to do that and refer that view from your controller.
See https://laravel.com/docs/5.5/blade#including-sub-views

Related

how to pass one data view and others view page in laravel

i have two pages file in my view
first is my home blade then second is category blade and both of them using extends asidenav
my problem is when im redirecting to category file it gives me a error Undefined variable: categories but in my home file when im redirecting it's working fine . i will describe my code.
in my controller HomeController i pass one data categories to home.blade.php
class HomeController extends Controller
{
public function home()
{
$categories = Category::select('id','name')->get();
return view('home', compact('categories'));
}
public function category(){
return view('category');
}
}
the output of my home.blade.php
#extends('asidenav')
#section('section')
<main>
this is homepage
</main>
#endsection
you can see in my home blade there is a extends asidenav . inside of my asidenav extends the data categories i passed is inside of that asidenav to make a list of categories
the output of my extends asidenav
where the data i pass categories
<ul>
#foreach($categories as $category)
<li><span>{{ $category->name }}
#endforeach
</ul>
when i click the ahref to redirect the category blade it gives the error undefined variable categories. but when im redirecting to home blade it's working
the output of my category.blade.php
#extends('asidenav')
#section('section')
<main>
this is category
</main>
#endsection
but when i tried this code in my category controller it's working fine the error of undefined variable categories not showing
public function category()
{
$categories = Category::select('id','name')->get();
return view('category', compact('categories'));
}
my point here is i want to pass only single data categories in my home blade and others file blade like a props but i don't have a idea how to do that
You could create a service provider responsible for inyecting those variables without declaring them in your controller, in your boot method :
use Illuminate\Support\Facades\View;
View::composer(['asidenav', 'home'], function($view){
$categories = Category::all();
return $view->with('categories', $categories);
})

Laravel: Retrieve data from DB and diplay in View

I have a bug here in my code that show me a probleme while displaying data in the home page
Controller
class Annonce_indexController extends Controller
{
public function index()
{
$annonce_residentiel = Annonce_residentiel::all();
return view('/' , compact('annonce_residentiel'));
}
}
Route
Route::get('/index','Annonce_indexController#index');
Blade View
{{ $annonce_residentiel->prix }}
It says that $annonce_residentiel is undefined
Edit:
The problem is I have two routes to the same view:
Route::get('/','Admin\Annonce_indexController#index');
Route::get('/',array('as' =>'viewville','uses'=>'VilleController#index'));
Solution
Change the second route to post !
Route::get('/','Admin\Annonce_indexController#index');
Route::post('/',array('as' =>'viewville','uses'=>'VilleController#index'));
$annonce_residentiel is an object and not a variable so you cannot just call it and expect it to pop a value. For just demo purpose and to understand how it works try the following code in your view.
#foreach($annonce_residentiel as $data)
{{ $data->prix }}
#endforeach
Controller
class Annonce_indexController extends Controller
{
public function index()
{
$annonce_residentiel = Annonce_residentiel::first();
return view('/' , compact('annonce_residentiel'));
}
}
Blade View
{{ $annonce_residentiel->prix }}

NotFoundHttpException Laravel

I am very new in learning Laravel. I want to fetch data from a database and show it. I can do it. But I want to use the title (fetched from the database) as a link. but then I get a NotFoundHttpException.
Routes
Route::get('articles', 'ArticleController#index');
Route::get('articles/{id}', 'ArticleController#show');
Controller
class ArticleController extends Controller
{
public function index()
{
$articles = Article::all();
return view('articles.index', compact('articles'));
}
public function show($id){
$article = Article::find($id);
return view('articles.show', compact('article'));
}
}
View
#extends('new_welcome')
#section('content')
<h1>Articles</h1>
#foreach($articles as $article)
<article>
<h2>
{{$article->title}}
</h2>
<div class="body">{{ $article->body}}</div>
</article>
#endforeach
#stop
Can someone help me in this case?
Your problem is because of You've "eat" one curly brace (blade engine skips it):
was:
href="{url ('/articles',$article->id)}"
have to be:
href="{{url ('/articles',$article->id)}}"
as You said:
if I click on any single article title then it can not show me the
specific article. But, if I give the URL "homestead.app/articles/2";
so You can see that when You click on link Your browser's address bar becomes:
homestead.app/{url ('/articles',$article->id)}
Because You're beginner so I'll give You advice to not to set direct url in views using url() helper.
Named routes are better if You want to have app that will work properly if in future You decide to change url from: articles to artcls. In this named routes will save You from bulk changing urls in view files.
set name to Your route using 'as' directive that makes Your routing flexible for changes (when You need to change URL so You change only path and keep views unchanged):
Route::get('articles/{id}', ['as' => 'article', 'uses' => 'ArticleController#show']);
Route::get('articles', ['as' => 'articles', 'uses' => 'ArticleController#index']);
change Your view file (find route helper in href):
#extends('new_welcome')
#section('content')
<h1> Articles </h1>
#foreach($articles as $article)
<article>
<h2>
{{$article->title}}
</h2>
<div class="body">{{ $article->body}}</div>
</article>
#endforeach
#stop

Store method not working using resource route

I am having trouble figuring out why my data is not being posted and stored in my database. I have used the resource routes for another form and it works fine, but here for some reason it won't work. Clicking submit just seems to refresh the page, no errors to work from!
So I have a form which gets the workout routines from a database, and on submission I want this to create a new Workout "session" in my database table (called "Workouts"). The form is this:
{{ Form::open(array('url' => '/')) }}
<div class="form-group">
{{ Form::text('workout_name', Input::old('workout_name'), array('class' => 'form-control', 'placeholder' => 'Session Name')) }}
</div>
<div class="form-group">
{{ Form::select('routines', $routine_names, null, array('class' => 'form-control')) }}
</div>
{{ Form::submit('Select Routine', array('class' => 'btn btn-success pull-right')) }}
{{ Form::close() }}
In my HomeController I have this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Routine;
use App\Workout;
class HomeController extends Controller
{
public function index()
{
$routines = Routine::all();
$routine_names = Routine::lists('routine_name');
return view('workout')->with(array('routines'=>$routines, 'routine_names'=>$routine_names));
}
public function store()
{
$workout = new Workout;
$workout->workout_name = Input::get('workout_name');
$workout->save();
}
}
I have a model created for the Workout, and the route for this page is the following:
Route::resource('/', 'HomeController');
I can't figure out where I'm going wrong. The index method in my controller is working, as it is returning the correct view with the data I need. The form also looks OK I think, as I'm posting to the same page, but submitting doesn't seem to carry out the code I have in the store method of the HomeController.
Any help would be appreciated!
Thanks :)
Change your route declaration from:
Route::resource('/', 'HomeController');
To something like this:
Route::resource('/workout', 'WorkoutController');
If you are using the resources controller creator command of php artisan then all the specific routes are created for you. To see all listed routes you can type , php artisan routes. This will show you RESTFUL routes even for your POST method .
And also even you did not created the resources controller and did made the routes with manual way then you can create ,
Route::POST('/workout' , SomeController#post);
I am trying to say , you have to use the different POST method for the form submission .
Hope this will solve your problem . Thanks.

Laravel 4 - Route not defined

I've created my route:
Route::get('fichas/orden/{id}', 'FichaController#orden');
My Controller:
public function orden($id)
{
return $id;
}
And I have this in my View:
{{ link_to_route('fichas.orden', 'Guardar orden', array($ficha->id), array('class' => 'btn btn-primary')) }}
And I don't know why I'm having this error. I have been using this kind of routes and views all the time and I haven't got any problem until now. Does anybody know why it doesn't work now?
If you want to link to a named route, you have to define the route as follows:
Route::get('fichas/orden/{id}', array('as'=>'fichas.orden', 'uses'=>'FichaController#orden'));

Resources