Sorry about restating my other question but the people that commented wanted more information, like uploading snapshot or the log file I don't know how to upload here.
I wan't to add a method(not function my mistake) to my PhotoController
public function search(){
return view('photos.search');
}
My route
Route::get('/photos/search','PhotosController#search');
I have created the file search.blade.php in the /photos in that file is 1 word "search"
Here is the error I get when I try it in the browser.
Facade\Ignition\Exceptions\ViewException
Trying to get property 'title' of non-object (View:/Web/PhotoAlbum/resources/views/photos/show.blade.php)
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
#section('content')
<h3>{{$photo->title}}</h3>
/{{$photo->photo}}" alt="{{$photo->title}}">
/{{$photo->photo}}" alt="{{$photo->title}}">
{!!Form::open(['action'=> ['PhotosController#destroy', $photo->id],'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete',['class'=> "btn btn-primary"])}}
{!!Form::close()!!}
#endsection
Note that this error is on another page. I have not edited this page, what I stated above is all I have done also note that the program work well and this error only shows when I try to access the search page.
Thank you
The $photo variable doesn't exist in your view nor was it provided by the controller.
Try this:
public function search(){
$photo = ... // retrieve the photo
return view('photos.search', ["photo" => $photo]);
}
It is because, you have not compact the variable. write the function like below:
Use Your model name at the top of your controller.
use App\YourModelName;
public function search(){
$photo = YourModelName::all();
return view('photos.search', compact('photo'));
}
Hope it will work.
Related
Im working on editing of an already saved product and i am getting the following error message on my browser
ErrorException in ProductsController.php line 88:
Missing argument 1 for App\Http\Controllers\ProductsController::edit()
in ProductsController.php line 88
My route controller is as below:
Route::get('productsedit', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
The function is as below
public function edit($id)
{
//find the post in the db and sav it as a variable
$product = Products:: findOrFail($id);
//return view and pass in the var previously created
return view('/productsedit')->withProducts($product);
}
Where iam i going wrong
In your edit method on your ProductsController you require a parameter ($id), but you don't have that value in your route . Which is what this error is saying.
[Missing argument 1 for
App\Http\Controllers\ProductsController::edit()]
Your route:
Route::get('productsedit', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
Will have to change to something like this:
Route::get('products/{$id}/edit', 'ProductsController#edit');
When calling the route in your view it will have to look something like this:
'products/{{$product->id}}/edit'
Extra:
You might want to take a look at Resource controllers since you are not really following restfull practices when it comes to your routes
https://laravel.com/docs/5.4/controllers#resource-controllers
your should give route id
Route::get('productsedit/{id}', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
and in your function should like this
`public function edit($id = null)
{
//find the post in the db and sav it as a variable
$product = Products:: findOrFail($id);
//return view and pass in the var previously created
return view('/productsedit')->withProducts($product);
}`
hope this work !!!
I have master.blade.php as one of my layouts. When I load post.blade.php in browser without a wildcard like url:localhost8000/post it works and full required page is loaded but when I use a wild card url:localhost8000/post/1 the page is not completely loaded. Only the footer is displayed. Header and body appears white fire bug shows the data is present which is passed through the wildcard.
Both the pages should appear as same because the difference is only of wildcard in url.
My route file is
Route::get('post','PostsController#index');
Route::get('post/{post}','PostsController#show');
Here is my PostsController
class PostsController extends Controller{
public function index(){
return view('posts.post');
}
public function show(Post $post){
return view('posts.post',compact('post'));
}
}
When this kind of error happens, there is most likely a PHP variable error in your project.
A way to find out is to view source of the HTML and look at the first or last part of the page depending on where the error is occurring.
Also, I assume you have properly bound the keyword 'id' in your routes, in your Post model.
You should pass id instead of object because your url is passing an integer localhost:8000/post/1
class PostsController extends Controller{
public function index(){
return view('posts.post');
}
public function show($id){
$post = Post::find($id);
return view('posts.post',compact('post'));
}
}
if u include some assets files like CSS or JS files to the HTML document the file path should start with a forward slash for example instead of 'assets/css/foo.css' you should write '/assets/css/foo.css' it worked for me when all the document was loading but the footer didn`t
I am trying to pass data to a view using the boot method in the AppServiceProvider, and have followed the steps outlined in one of the Laracast fundamentals videos. However, I am getting an error in my controller when trying to use this variable.
In my AppServiceProvider:
public function boot()
{
view()->composer('home', function($view){
$view->with('current', Post::orderBy('created_at', 'desc')->first());
});
}
So this should pass the data to my home view as far as I understand, so that I don't need to keep referencing it in all the methods I have in the controller for that view. I then have a homeController with the following method:
public function index()
{
//Get user
$user = Auth::user();
//OMDB API setup - Get Movie name, remove spaces, add into API request
$movie_one = str_replace(" ", "+", $current->movie_one);
$movie_two = str_replace(" ", "+", $current->movie_two);
return view('home', compact(array('user')));
}
Previously I was adding a variable called current in this method to get an entry from my Post table, but wanted to add it to the AppServiceProvider as I will have to re-declare it in other methods as well. The issue I have is that I try to use the $current object again in this method, but it isn't available to it? I get the following error:
ErrorException in homeController.php line 30:
Undefined variable: current
What can I do here? Can I pass that data from to a controller as well as the home view? Or is that not possible?
the code you written inside the boot method, will only be accessible inside home view, if you want to access the Post object inside controller too, you have to follow this, through this you can access your Post object in controller, as will as view.
app()->singleton('current',function($app){
return Post::orderBy('created_at', 'desc')->first();
});
So in your code, you can access the Post object like this,
$post_obj = App::make('current');
$post_obj->movie_two;
or directly
App::make('current')->movie_two;
I'm trying to learn laravel 4. I created a form(using view) and returned it via a controller(testController) using index method. I had created this controller using artisan command.
i created another method (dologin) in the controller which would process the form. In the form url parameter i gave the address of dologin method.
This is the route:
Route::resource('test', 'testController');
This is the controller
<?php
class testController extends \BaseController {
public function index()
{
return View::make('test.index');
}
public function dologin(){
echo "working";
}
and this is the index view file
{{ Form::open(array('url'=>'test/loginform')) }}
{{ Form::text('username', null, array('placeholder'=>'Username')) }}<br/>
{{ Form::password('password', array('placeholder'=>'Password')) }}<br/>
{{ Form::submit('Login') }}
{{ Form::close() }}
After submitting form, it should echo "working" in the browser. But after submitting the form, page is blank. The url changes though from
/laravel/public/index.php/test/
to
/laravel/public/index.php/test/loginform
umefarooq's answer is correct, however hopefully this answer should give you a bit more insight into getting a head-start in your Laravel development as well as a consistent best-practice programming style.
Firstly, class names should really start with a capital letter. Try to keep methods / function names starting with a lower case letter, and class names starting with a capital.
Secondly, you don't need the \ in front of BaseController. You only need the backslash if you are name-spacing your controller. e.g. if your controller is in the folder Admin\TestController.php, and you put your TestController in the Admin namespace by typing <?php namespace Admin at the beginning of the file. This is when you should use \BaseController because you are telling your TestController to extend BaseController from the Global Namespace. Alternatively, before you declare your class, you can type use BaseController; and you don't need to put a \ in every time.
Specifically related to your question:
When you use resource routes in your routes file, you are telling Laravel that the controller can have any or all of the following methods: index, show, create, store, edit, update and destroy.
As such, Route::resource('test', 'TestController'); will point to TestController.php inside your controllers folder.
Your TestController should be structured as follows, most restful controllers will use the below as some kind of boilerplate:
<?php
class TestController extends BaseController
{
public function __construct()
{
}
// Typically used for listing all or filtered subset of items
public function index()
{
$tests = Test::all();
return View::make('test.index', compact('tests'));
}
// Typically shows a specific item detail
public function show($id)
{
$test = Test::find($id);
return View::make('test.show', compact('test'));
}
// Typically used to show the form which creates a new resource.
public function create()
{
return View::make('test.create');
}
// Handles the post request from the create form
public function store()
{
$test = new Test;
$test->attribute1 = Input::get('attribute1');
$test->attribute2 = Input::get('attribute2');
$test->attribute3 = Input::get('attribute3');
$test->attribute4 = Input::get('attribute4');
if ($test->save())
{
return Redirect::route('test.show', $test->id);
}
}
// Shows the edit form
public function edit($id)
{
$test = Test::find($id);
return View::make('test.edit', compact('test'));
}
// Handles storing the submitted PUT request from the edit form.
public function update($id)
{
$test = Test::find($id);
$test->attribute1 = Input::get('attribute1');
$test->attribute2 = Input::get('attribute2');
$test->attribute3 = Input::get('attribute3');
$test->attribute4 = Input::get('attribute4');
if ($test->save())
{
return Redirect::route('test.show', [$id]);
}
}
// Used to delete a resource.
public function destroy($id)
{
$test = Test::find($id);
$test->delete();
return Redirect::route('test.index');
}
}
Also, the beauty of using Resource Controllers is that you can take advantage of named routes.
in the terminal window, type in php artisan routes.
You should see 7 named routes.
test.index
test.destroy
test.show
test.edit
test.destroy
test.create
test.update
So within your form, instead of doing
{{ Form::open(array('url'=>'test/loginform')) }} you can point the url to a named route instead:
{{ Form::open(array('route' => array('test.store')) }}
That way if you ever change the url, or need to move around your site structure, this will be easy, because the forms post url will auto bind to the named route within the routes file. You wont need to update every single one of your views to ensure that the url's are pointing to the correct location.
Finally, as a starting point, I would recommend using JefreyWay/Laravel-4-Generators package. https://github.com/JeffreyWay/Laravel-4-Generators . Use them to create your resources, controllers, views etc. and see how the generators scaffold your models, views and, controllers for you.
Here is another resource to help you get started:
https://laracasts.com/lessons/understanding-rest
Route::resource('test', 'testController');
will work for RESTful method of controller, like index, edit, destroy, create and now you are using custom method of controller for this you need to create another route
Route::post("test/loginform",'testController#dologin');
hope this will work for you. read route documentation http://laravel.com/docs/routing
In addition to what umefarooq said, which is 100% accurate. You need to look into flash messages as well.
public function dologin(){
//do login verification stuff
If login validated
Return redirect::to(logged/page)->with('message', 'You're logged in');
If login failed
Return redirect::to('test')->with('message', 'You login credentials fail');
}
For further research:
http://laravel.com/docs/responses
I want to grab some data from a database and display on a layout page, I've basically started building a small CMS to get into Laravel and all has gone fine so far but now i'm at a wall, and can't find a solution.
I have a layout blade file like so: http://paste.laravel.com/1fB1 nothing majot but you will see i have used $page->meta_title etc in there and in my controller i have:
public function home()
{
$pages = Pages::all();
return View::make('frontend/home')->with('pages',$pages);
}
Which I have a pages model doing nothing else really like so:
class Pages extends Eloquent {
protected $table = 'pages';
}
So why is it trying to get property of non-object and I don't really want to use a foreach because this is going to be the frontend of my 'test' website so a foreach wouldn't suite.
You'll need to access these items as a multi-dimensional array if you don't want to loop through them.
$pages[0]['field_name_here']
or
$pages[1]['field_name_here']
Its a bit of a tough one to answer without knowing how you want your CMS to work.
For example, you could have a route as {pagename} in your routes.php file, then have a page controller where you would get the requested route from the variable passed in. This would then load the page you wanted using the variable
public function page( $pagename ) {
$page = Page::where('page_title', '=', $pagename)->first();
View::make('frontend/page', array( 'page' => $page ));
}
Using a route like that, and the controller, in your view you could use {{ $page->content }} to get the content of the requested page from the database and display it.
Hope this helps.
Edit: Example Route:
Route::get('{pagename}', 'PageController#page');