How to call multiple methods or controllers to a same route in laravel - laravel-4

Find below the controller code with two methods and suggest me how to call these two methods in the same route or whether I have to create two different controllers for the same page(route).
class TicketController extends Controller
{
public function show(){
$results=Whmcs::GetTickets([
]);
return view('clientlayout.main.index',compact('results'));
}
public function set(){
$test=Whmcs::GetInvoices([
]);
return view('clientlayout.main.index',compact('test'));
}
}
The route file:
Route::get('clientlayout.main.index','TicketController#show');
Route::get('clientlayout.main.index','TicketController#set');
Find the code in the blade file and after running this I'm getting an error
undefined index:Results.
#foreach($results['tickets']['ticket'] as $key)
{{$key['subject']}}
#endforeach
#foreach($test['invoices']['invoice'] as $value)
{{$value['firstname']}}
#endforeach
When I run these two foreach loops in a different blade file it executes correctly, but I need these two results to be viewed in the same file.
How to view both tickets and invoices in the same index page?

Combine the two controllers into one and perform both queries in a single method:
class InvoiceTicketController extends Controller
{
public function show(){
$tickets = Whmcs::GetTickets([]);
$invoices = Whmcs::GetInvoices([]);
return view('clientlayout.main.index',compact('tickets', 'invoices'));
}
}
Then update one of the those routes to use the combined controller:
Route::get('clientlayout.main.index','InvoiceTicketController#show');
You'll have access to both $tickets and $invoices collections in the blade file this way:
#foreach($tickets as $ticket)
{{ $ticket->subject }}
#endforeach
#foreach($invoices as $invoice)
{{ $invoice->firstname }}
#endforeach

Related

Method Illuminate\Database\Eloquent\Collection::links does not exist

I created a model relationship between User and Message. I want to implement a list of messages for the authenticated user but I get the following error.
Method Illuminate\Database\Eloquent\Collection::links does not exist
Controller
public function index()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('message.index')->with('messages', $user->message);
}
Message provider
class message extends Model
{
public function user() {
return $this->belongsTo('App\User');
}
}
User provider
public function message ()
{
return $this->hasMany('App\Message');
}
index.blade.php
#extends('layouts.app')
#section('content')
<h1>messages</h1>
#if(count($messages)>0)
#foreach ($messages as $message)
<div class="well">
<h3>{{$message->user}}</h3>
<small>Written on {{$message->created_at}} </small>
</div>
#endforeach
{{$messages->links()}}
#else
<p> no post found </p>
#endif
#endsection
Error
"Method Illuminate\Database\Eloquent\Collection::links does not exist.(View: C:\xampp\htdocs\basicwebsite\resources\views\message\index.blade.php)"
Check your view blade, that method (links()) only could be used when your data model is implementing paginate() method.
If you dont use paginate(), remove this part:
{{$messages->links() }}
If you are trying to paginate your data when it gets to the view then you need to add the paginate in your controller before passing the data to the view. Example
return $users = Users::select('id','name')->paginate(10);
with that paginate method in your controller, you can call the links method to paginate your object in view as shown below
{{$users->links()}}
hope it helps you
There are 2 ways to resolve this issue:
Either use paginate function while searching data from database:
$users = DB::table('users')->where('id',$user_id)->paginate(1);
Remove links() function from index.blade.php
{{ $messages->links() }}
Remove {{ $messages->links() }} to in your index.blade.php because {{ $messages->links() }} is supported only when you use paginate
You can do something like this in your controller file.
public function index()
{
$messages = Message::all()->paginate(5);
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('message.index')->with('messages', $messages, $user->message);
}

how to access function defined in model to view?

my laravel model funciton :
public function isAdminOrSuperAdmin()
{
return $this->role() == config('custom_config.constants.user_types.SUPER_ADMIN')
|| $this->role() == config('custom_config.constants.user_types.ADMIN');
}
i try to access in view :
#if($user->isAdminOrSuperAdmin())
<a class="btn btn-primary pull-right" style="margin-top:
-10px;margin-bottom: 5px" href="{!! route('admin.users.create') !!}">
Add New
</a>
#endif
but it show error:
Method Illuminate\Database\Eloquent\Collection::isAdminOrSuperAdmin does not exist. (View:/resources/views/admin/users/index.blade.php)
thanks in advance.
Check the error:
Method Illuminate\Database\Eloquent\Collection::isAdminOrSuperAdmin does not exist. (View:/resources/views/admin/users/index.blade.php)
This means, you are trying to call a method of your model on a Collection instance instead of an actual User model instance.
When querying several items from your database, Laravel returns an instance of the Collection class that contains all the resulting model objects.
Maybe you are doing something like this:
public function aCoolFunction()
{
$user = User::where('column', 'value')
->get(); // <-----
return view('my_view')->with('user', $user);
}
The get() method returns a Collection, not a single element.
Try the first() instead:
public function aCoolFunction()
{
$user = User::where('column', 'value')
->first(); // <-----
return view('my_view')->with('user', $user);
}
Now in your view the $user variable will actually hold and instance of your User model user in which the isAdminOrSuperAdmin() method is defined, and not a collection of it.
I don't think this is the best way but you can pass the function to view using your controller :
in your controller :
public function index(User $user)
{
$ModelFunction = $user->yourModelFunction();
return View('test',compact('user','ModelFunction'));
}
And in your View :
{{ $ModelFunction }}
you must call function like this:
#if(is_admin_or_super_admin())

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 }}

Laravel pass exact parameter to route

i have translated url which i need to redirect to the specific controller function, but i need also to pass an exact parameter.
For example i want to show all football news, but in the url i do not have the ID of the sport football (id=1) so i need to pass the parameter id=1 to the index() function.
Route::get('/football-news/', ['as' => 'news.index', 'uses' => 'NewsController#index']);
it is not an option to pass 'football' as a parameter, because it is just an example. The real route is translated and the code looks like that:
Route::get(LaravelLocalization::transRoute('routes.football.news'), ['as' => 'news.index', 'uses' => 'NewsController#index']);
suppose you have a NewsController to fetch all news be like
class NewsController extends Controller
{
public function index()
{
$news = News::all(); //you have to create News model
return view('news.index', compact('news')); //use to pass data in view
}
public function show($id)
{
$news_detail=News::find($id); //to fetch detail of news from database
return view('news.show', compact('news_detail'));
}
}
create index.php and show.php in views/news folder. in index.php
#foreach($news as $news_item)
<div>
{{ $news_item->title }}
</div>
#endforeach
here using "/news/{{$news_item->id}}" you can pass id of specific news into route file.
in show.php
<h1>news</h1>
<h1>
{{ $news_detail->title }}
</h1>
<ul class="list-group">
#foreach($news_detail->detail as $details)
<li class="list-group-item">{{$details}}</li>
#endforeach
</ul>
in route file
Route::get('/news/{news}', 'NewsController#show');
now you have to create show($id) function in NewsController.php which parameter is id.
You can append the index URL with ?id=1 parameter (eg. domain.com?id=1) and get it in your index controller action by using Request::get('id');
For example:
Url in template file:
<a href="domain.com?id=1" />
In your NewsController:
public function index(Request $request){
$id = $request->get('id');
}
You should be able to have access to the parameter even though you didn't specify wildcards in the route file.
Edit:
you will have to call a different #action for a different route. You can pass in an id wildcard.
For example, in Route file:
Route::get('tennis-news/{id}', 'NewsController#tennisIndex');
Route::get('football-news/{id}', 'NewsController#footballIndex');
Then in the NewsControlleryou must have public methods tennisIndex($id) and footballIindex($id), these methods will have access to the wildcard you set in the route.
For example, in NewsController
public function tennisIndex($id){
$tennnis_news = News::where('sport'='tennis)->where('id', $id)->get();
return view('tennis_news', compact('tennnis_news'));
}

Laravel passing data to page

I am trying to pass data to Controller of Laravel. here is how I do that :
in PagesController::
class PagesController extends Controller
{
public function contact(){
$data="some random ";
return view('contact',compact("data"));
}
}
now in contact.blade.php :
contact pages {{ $data }}
and it s hows
Whoops, looks like something went wrong.
What may be a problam?
try this
public function contact()
{
$data = 'some random';
return View('contact')->with('data' , $data );
}
make sure that the view works fine (if its inside a folder use return View('foldername.contact')
make sure that your view name is contact.blade.php (check uppercase letters)
and inside your view
this is my {{ $data }}

Resources