I'm trying to create a page dynamically for each courses that I'm adding in a database.
I have a CoursesController who is taking care of adding, displaying the courses.
So, when I click on a course, it should dynamically create a page for that course and show details in that course page.
In the route.php page, I have
Route::get('courses/{code}', [ 'as'=>'course-show', 'uses'=>'CoursesController#getShow']);
and in the
CoursesController.php
public function getShow($code){
return $code;
}
And in the index.blade.php for CoursesController,
<h4>{{ $course->name }}</h4>
Now, It create the link with a unique code (saved in database) and upon clicking there, it takes me to the course page with an error:
BadMethodCallException
Method [show] does not exist.
What might be the problem? Can anyone help me?
The getShow() function in your controller should be show().
Also URL::action() goes to a controller action.
You probably want URL::route()
<h4> {{ $course->name }} </h4>
or you could do this
<h4> {{ $course->name }} </h4>
Related
I have two table, one is User that stores login credentials and other is Userinfo that deals with storing personal information about the users. When a user sign up, they do not require filling up all the inputs for UserInfo table (i.e. they might only fill up the Gender and Religion fields out of a few other). So, the end result is that the UserInfo ends up being filled with only a couple of values or none at all.
The issue I am facing right now is when I pass through the data from both tables to view, and display them in typical laravel way, it throws an error Trying to get property of non-object. This is expected since not all the users have complete data from UserInfo table. I tried many methods both in controller and view to deal with the error message but failed to find a solution.
Is there a way to check the columns for empty or half complete value and deal with them properly in controller before passing onto the view? I think these checks should be performed in the controller or anywhere other than in the view. If there are none, how do I deal with them in the view? Maybe inside the loop? Solution and suggestions are greatly appreciated! Here's my code in the view:
#forelse($donors as $donor)
<div class="panel panel-default">
<div class="panel-heading"><i class="fa fa-list-alt"></i> {{$donor->name}}</div>
<div class="panel-body">
<div class="row">
<div class="col-md-6">
<div class="basic-info">
<h4>Basic Information</h4>
<p><strong>Blood Group: </strong>{{ $donor->user_info->blood }}</p>
<p><strong>Sex: </strong>{{ $donor->user_info->sex }}</p>
<p><strong>Religion: </strong>{{ $donor->user_info->religion }}</p>
<p><strong>Age: </strong>{{ \Carbon\Carbon::parse($donor->user_info->dob)->age }}</p>
</div>
</div>
</div>
</div>
</div>
#empty
<p>No records found!</p>
#endforelse
As you can see, because not all the user have complete data in the table, laravel throws the Trying to get property of non-object error. My controller has standard laravel method of passing data to views:
public function index(User $user, Request $request)
{
$donors = $user->with('user_info')->latest()->paginate(5);
return view('donors.index', compact('donors'));
}
Since version 5.5, Laravel has an optional() helper that I think it can help you.
Try something like this in your view:
optional($donor->user_info)->blood
I'm trying to get a view where the user is able to create a topic. I did this many times in my project but never got this error, Because all of the other ones work just fine.
I get the error No query results for model [App\Topic] create. Here is the code.
This is the link that is supposed to bring the user to the view.
<div class="col s12">
<div class="card">
<div class="card-content clearfix">
New topic <i class="material-icons right">edit</i>
</div>
</div>
</div>
These are the routes that are used in this problem.
Route::get('/theme/{theme_id}/topics/create', 'TopicsController#create')->name('createtopic');
Route::post('/theme/{theme_id}/topics/create', 'TopicsController#store')->name('savetopic');
The Controller method create.
dd('Hij is er ' . $id);
And the store method is empty, The link doesn't show the DumpDie method but shows me the error instead. So there is no need to post the view i'm trying to display because that's not where the problem is. Thanks in advance!
You need to understand how routes and controller methods are working
Route::get('/theme/{theme_id}/topics/create', 'TopicsController#create')->name('createtopic');
When you are hitting the above route, i mean something for example yourdomain/theme/1/topics/create in your browser, then it will take 1 in place of theme_id and it will go to the create method of your TopicsController which will accept one argument.
Your create method should be something like that
public function create($id)
{
dd($id);
}
Here you will get 1 as result, because you have passed this argument in your url instead of theme_id.
In my Laravel application, when I am trying to link the username of the person who has posted in the website with his profile page, with the code:
<div class="media-body">#{{ post.user.name }}
It is giving the error:
Sorry, the page you are looking for could not be found.
1/1 NotFoundHttpException in RouteCollection.php line 161:
But, when I am trying to print #{{ post.user.profileUrl }} it is giving the right address, also in the json response, it is giving the right address, and going to the address is also reaching the specific location.
So, I don’t think it is some problem with post.user.profileUrl, as it seems to work fine, it seems to be some problem with using it with href, the address of the error in Google Chrome is:
http://localhost:8000/%7B%7B%20post.user.profileUrl%20%7D%7D
and the address should have been
http://localhost:8000/users/2 where 2 refers to the id of the user, which I am passing to the user through Vue.js
The problem is #{{ post.user.profileUrl }} is not parsing. %7B%7B%20post.user.profileUrl%20%7D%7D is ASCII representation of {{ post.user.profileUrl }}
Check if the code is in .blade.php file. If it is, you should look into JS template engine if you're using any.
https://laravel.com/docs/5.3/blade#blade-and-javascript-frameworks
The # symbol will be removed by Blade; however, {{ name }} expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework
I think you have a $post model in your view. When you setup relationships between the model Post and User you can link to them through the following:
#{{ $post->user->name }}
This will turn the users profileUrl into a valid link. But therefor you had to setup relationships in the Models.
Post model:
public function user() {
return $this->hasMany('App\Users', 'id', 'user_id');
//first parameter is the Model class
//send is the id of the user table
//thirth is the user_id in the post table
}
And in the User model:
public function posts() {
return $this->hasMany('App\Posts', 'user_id', 'id');
}
Hope this works!
Try this... this should definitely work for you
#verbatim
<div class="media-body">
<a href="{{ post.user.profileUrl }}">
{{ post.user.name }}
</a>
</div>
#endverbatim
I got my problem, the issue is with the javascript library, I am using, known as vue.js, it used to allow usage inside quotes, but in vue 2, they don't, so there is a turnaround for this,
<a :href="post.user.profileUrl">#{{ post.user.name }}</a>
and this works fine.
Thanks to all the people who contributed by answering...
I use davejamesmiller Breadcrumbs package. I am wondering how to pass a parameter to a breadcrumb, something like an id.
In the docs (here) it says that is possible, but can't find the way to do it.
My goal es to do a breadcrumb like this: Dashboard \ User \ New Model. Where New Model its a form to add model data with some relationship with the user. Without the user_id param the link for User won't work.
Any idea?
You can pass global variable
\View::share ( 'variable2', $variable2 );
if render breadcrumbs in layout
or You need render breadcrumbs in `user.new_model.blade
#section('content')
{!! Breadcrumbs::render('page', $page) !!}
#stop`
my way
Create template
breadcrumbs.blade.php
with content
#if(!empty($breadcrumbs))
<ol class="breadcrumb">
<li>{!! link_to_route('main', 'Home') !!}</li>
#foreach($breadcrumbs as $bread)
#if(isset($bread['url']))
<li>{!! link_to($bread['url'], $bread['name']) !!}</li>
#else
<li>{!! $bread['name'] !!}</li>
#endif
#endforeach
</ol>
#endif
and connect it to layout
#include('breadcrumbs')
and in your action pass array of links
\View::share('breadcrumbs', [
['url' => route('collection.show', ['id'=>$data->collection, 'url'=>$data->collection]), 'name' => $data->collection->name],
['name' => $data->article]
]);
There is another way. As general, in each view just calling Breadcrumbs::render() should create the hierarchy of the breadcrumbs links depending on the routes defined in routes/breadcrumbs.php.
There are two essential points that you have keep in mind to go further with this solution:
The callback function that found in breadcrumbs.php routs definition is the place from which you should pass your parameters.
Giving correct name to your web route from routes/web.php which will be used later in routes/breadcrumbs.php
Checkout the following code snippets that demonstrates the above two points:
//Point1: routes/breadcrumbs.php
Breadcrumbs::register('job.edit', function($breadcrumbs, $job, $title)
{
$breadcrumbs->parent('job','job');
$breadcrumbs->push($title, route('job.edit', $job));
});
Breadcrumbs::register('job.edit.install', function($breadcrumbs, $job, $title)
{
$breadcrumbs->parent('job.edit',$job, $title);
$breadcrumbs->push('Job Install Equipments', route('job.edit.install','job'));
});
In the above code we passed $job and $title through the callback function.
//Point2 routes/web.php
Route::get('/job/edit/{job}', 'JobController#edit')->name('job.edit');
Route::get('/job/install-equipments/{job}', 'JobController#installEquipments')->name('job.edit.install');
We give a name to the route through the name method , Laravel 5.4, which allow us to define the routes correctly in Point1.
The last step, is what you have do in the view file. Here I will show you the last one regarding /job/install-equipments which should be rendered in the breadcrumb as the last element and its parent is job/edit with parameter job which handles the primary key id
//install.equipments.blade.php
#extends('layouts.main')
#section('content')
{!! Breadcrumbs::render('job.edit.install',$job->id, __('Edit').': '.$job->title) !!}
The above will render a breacrumbs look like:
Home / Job / Edit: title of
job / Job Install Equipment
The required parameters that handles breadcrumbs render are supplied un the above render method i.e through $job->id and __('Edit').': '.$job->title) , the last just adjusting the text and it could be done inside the callback function of breadcrumbs routes.
On a dashboard page, I've created a select list in a form that lists the names of components; the value that's passed from the select list is obviously the component id. On pressing submit, the user is routed to a page that displays the data about that component. Should be dirt simple...
Controller:
public function showDashboard()
{
$components = Component::lists('name','id'); ...
return View::make('dashboard', array('components'=>$components, ...))
}
dashboard.blade.php:
{{ Form::open(array('route' => array('components.show', $components->id), 'method'=>'get')) }}
{{ Form::Label('id','Component:') }}
{{ Form::select('id', $components) }}
{{ Form::submit('Show Component', array('class'=>'button')) }}
{{ Form::close() }}
I've tried various ways of doing this, and get a different error every time. The above code doesn't even let me display the dashboard page -- I get a "Trying to get property of non-object" error. Clearly, it's not liking $components because that was passed as a list array and not an object. As I said, I'm sure this is dirt simple, I just can't figure out the proper syntax, and Laravel docs aren't giving me the answer. Thanks!
The problem isn't the dropdown, or the lists method, but rather in your form opening. Here, you have $components->id as an argument to the route, but $components is an array and you can't access an id property on it.
Finally figured this out. I had posted a similar question here subsequent to this one, and rather than repeat the answer, it is here:
How to pass id value from select list to controller when controller expects an object? laravel-4
The very short version: change Route::get to Route::post. Details with code in the link above. Problem solved!