Laravel tasklist tutorial showing page not found - php

I am trying to learn laravel so I started by following the tasklist tutorial from the laravel website. The page shows fine but when I try to add a task it goes to a page that says: 'Not Found'.
My code:
task.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
//
}
Web.php (routes):
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
use App\Task;
use Illuminate\Http\Request;
// Display tasks
Route::get('/', function () {
$tasks = Task::orderBy('created_at', 'asc')->get();
return view('tasks', [
'tasks' => $tasks
]);
});
// Add new tasks
Route::post('/task', function (Request $request) {
$validator = Validator::make($request->all(), [
'name' => 'required|max:255',
]);
if ($validator->fails()) {
return redirect('/')
->withInput()
->withErrors($validator);
}
$task = new Task;
$task->name = $request->name;
$task->save();
return redirect('/');
});
//Remove tasks
Route::delete('/task{id}', function ($id) {
Task::findOrFail($id)->delete();
return redirect('/');
});
Auth::routes();
Route::get('/home', 'HomeController#index');
And my tasks.blade.php :
#extends('layouts.app')
#section('content')
<!-- Bootstrap Boilerplate... -->
<div class="panel-body">
<!-- Display Validation Errors -->
#include('common.errors')
<!-- New Task Form -->
<form action="/task" method="POST" class="form-horizontal">
{{ csrf_field() }}
<!-- Task Name -->
<div class="form-group">
<label for="task" class="col-sm-3 control-label">Task</label>
<div class="col-sm-6">
<input type="text" name="name" id="task-name" class="form-control">
</div>
</div>
<!-- Add Task Button -->
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button type="submit" class="btn btn-default">
<i class="fa fa-plus"></i> Add Task
</button>
</div>
</div>
</form>
</div>
<!-- Current Tasks -->
#if (count($tasks) > 0)
<div class="panel panel-default">
<div class="panel-heading">
Current Tasks
</div>
<div class="panel-body">
<table class="table table-striped task-table">
<!-- Table Headings -->
<thead>
<th>Task</th>
<th> </th>
</thead>
<!-- Table Body -->
<tbody>
#foreach ($tasks as $task)
<tr>
<!-- Task Name -->
<td class="table-text">
<div>{{ $task->name }}</div>
</td>
<!-- Delete Button -->
<td>
<form action="/task/{{ $task->id }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button>Delete Task</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
#endif
#endsection
This is the page I see when clicking 'add task':
What am I doing wrong? I'm using Laravel 5.4

Related

SOLVED! Call to undefined method App\Category::posts() Laravel

i want to create some CMS project with the laravel, when the user trying to click the categories, the app will show the post that only shows the category that user clicked. When the user click the categories, the app says error Bad Method Call. What should i do? Any help will be appreciated.
Here's the code from web.php
<?php
use App\Http\Controllers\Blog\PostsController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'WelcomeController#index')->name('welcome');
Route::get('blog/posts/{post}', [PostsController::class, 'show'])->name('blog.show');
Route::get('blog/categories/{category}', [PostsController::class, 'category'])->name('blog.category');
Route::get('blog/tags/{tag}', [PostsController::class, 'tag'])->name('blog.tag');
Auth::routes();
Route::middleware(['auth'])->group(function () {
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('categories', 'CategoriesController');
Route::resource('posts','PostsController');
Route::resource('tags','TagsController');
Route::get('trashed-posts','PostsController#trashed')->name('trashed-posts.index');
Route::put('restore-post/{post}', 'PostsController#restore')->name('restore-posts');
});
Route::middleware(['auth', 'admin'])->group(function () {
Route::get('users/profile', 'UserController#edit')->name('users.edit-profile');
Route::put('users/profile', 'UserController#update')->name('users.update-profile');
Route::get('users','UserController#index')->name('users.index');
Route::post('users/{user}/make-admin', 'UserController#makeAdmin')->name('users.make-admin');
});
Here's the code from PostsController.php
<?php
namespace App\Http\Controllers\Blog;
use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Http\Request;
use App\Tag;
use App\Category;
class PostsController extends Controller
{
public function show(Post $post) {
return view('blog.show')->with('post', $post);
}
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->posts()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
public function tag(Tag $tag) {
return view('blog.tag')
->with('tag', $tag)
->with('posts', $tag->posts()->simplePaginate(3));
}
}
Here's the code from App\Category.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $fillable = ['name'];
public function post() {
return $this->hasMany(Post::class);
}
}
Here's the code from category.blade.php
#extends('layouts.blog')
#section('title')
Category {{ $category->name }}
#endsection
#section('header')
<header class="header text-center text-white" style="background-image: linear-gradient(-225deg, #5D9FFF 0%, #B8DCFF 48%, #6BBBFF 100%);">
<div class="container">
<div class="row">
<div class="col-md-8 mx-auto">
<h1>{{ $category->name }}</h1>
<p class="lead-2 opacity-90 mt-6">Read and get updated on how we progress</p>
</div>
</div>
</div>
</header><!-- /.header -->
#endsection
#section('content')
<main class="main-content">
<div class="section bg-gray">
<div class="container">
<div class="row">
<div class="col-md-8 col-xl-9">
<div class="row gap-y">
#forelse($posts as $post)
<div class="col-md-6">
<div class="card border hover-shadow-6 mb-6 d-block">
<img class="card-img-top" src="{{ asset($post->image) }}" alt="Card image cap">
<div class="p-6 text-center">
<p>
<a class="small-5 text-lighter text-uppercase ls-2 fw-400" href="#"></a>
{{ $post->category->name }}
</p>
<h5 class="mb-0">
<a class="text-dark" href="{{ route('blog.show',$post->id) }}">
{{ $post->title }}
</a>
</h5>
</div>
</div>
</div>
#empty
<p class="text-center">
No Results found for query <strong>{{ request()->query('search') }} </strong>
</p>
#endforelse
</div>
{{-- <nav class="flexbox mt-30">
<a class="btn btn-white disabled"><i class="ti-arrow-left fs-9 mr-4"></i> Newer</a>
<a class="btn btn-white" href="#">Older <i class="ti-arrow-right fs-9 ml-4"></i></a>
</nav> --}}
{{ $posts->appends(['search' => request()->query('search') ])->links() }}
</div>
#include('partials.sidebar')
</div>
</div>
</div>
</main>
#endsection
If you guys didn't see which file that i don't show up, just ask,.
If you guys see any problems that confusing, i can use teamviewer.
Any Help will be appreciated.
Thank you.
The error told that you don't have posts. Indeed it is. What you have is post() method on Category class, as evidently seen in this line:
public function post() {
You should change this ->with('posts', $category->posts()->simplePaginate()) to be:
public function category(Category $category) {
return view('blog.category')
->with('category', $category)
->with('posts', $category->post()->simplePaginate())
->with('categories', Category::all())
->with('tags', Tag::all());
}
In your model, you defined a post relationship whereas you use a posts relationship then, they're not the same one. You should replace post by posts because it's a to-many relationship.
Your relationship is post and you have multiple ->with('posts', $category->posts()->simplePaginate()). Can you change those to ->with('post') and try again?

Laravel - Data not pulling through to form

I have a create form for creating courses, I have assigned users to these courses however for some reason it is not working anymore. I have been through my code umpteen times and I can't find anything that has changed - so i have come here for help.
To give some context below is my index.blade.php. As you can see I have previously assigned users to a course.
Create.blade.php;
#extends('layouts.app')
#section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Course</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label class="required" for="name">Course Title</label>
<input class="form-control" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
#if($errors->has('name'))
<div class="invalid-feedback">
{{ $errors->first('name') }}
</div>
#endif
</div>
<div class="form-group">
#can('create_courses')
{!! Form::label('Instructor', 'Instructor', ['class' => 'control-label']) !!}
{!! Form::select('Instructor[]', $instructors, Request::get('Instructor'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!}
#if($errors->has('Instructor'))
{{ $errors->first('Instructor') }}
#endif
#endcan
</div>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</div>
</form>
</div>
</div>
#endsection
Index.blade.php;
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-10">
<p>
#can('create_courses')
<button type="button" class="btn btn-success">Create Course</button>
#endcan('create_courses')
</p>
<div class="card">
<div class="card-header">Courses</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Course Title</th>
<th>Instructor</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#foreach($course as $course)
<tr>
<th scope="row">{{ $course->id }}</th>
<td>{{ $course->title}}</td>
<td>{{ implode (', ', $course->instructors()->pluck('name')->toArray()) }}</td>
<td>
#can('edit_courses')
<a class="btn btn-xs btn-secondary" href="{{ route('admin.modules.index', $course->id) }}">
Modules
</a>
#endcan
</td>
<td>
#can('edit_courses')
<a class="btn btn-xs btn-primary" href="{{ route('admin.courses.edit', $course->id) }}">
Edit
</a>
#endcan
</td>
<td>
#can('delete_courses')
<form action="{{ route('admin.courses.destroy', $course->id) }}" method="POST" onsubmit="return confirm('Confirm delete?');" style="display: inline-block;">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" class="btn btn-xs btn-danger" value="Delete">
</form>
#endcan
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- <div class="col-md-2 col-lg-2">
<div class="list-unstyled">
Courses
Modules
</div>
</div> -->
</div>
</div>
#endsection
CoursesController.php;
<?php
namespace App\Http\Controllers\Admin;
use Gate;
use App\User;
use App\Course;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
class CoursesController extends Controller
{
public function __construct()
{
//calling auth middleware to check whether user is logged in, if no logged in user they will be redirected to login page
$this->middleware('auth');
}
public function index()
{
if(Gate::denies('manage_courses')){
return redirect(route('home'));
}
$courses = Course::all();
return view('admin.course.index')->with('course', $courses); //pass data down to view
}
public function create()
{
if(Gate::denies('create_courses')){
return redirect(route('home'));
}
$instructors = User::whereHas('role', function ($query) {
$query->where('role_id', 2); })->get()->pluck('name'); //defining instructor variable to call in create.blade.php. Followed by specifying that only users with role_id:2 can be viewed in the select form by looping through the pivot table to check each role_id
return view('admin.course.create', compact('instructors')); //passing instructor to view
}
public function store(Request $request)
{
$course = Course::create($request->all()); //request all the data fields to store in DB
$course->instructors()->sync($request->input('instructors', [])); //input method retrieves all of the input values as an array
if($course->save()){
$request->session()->flash('success', 'The course ' . $course->title . ' has been created successfully.');
}else{
$request->session()->flash('error', 'There was an error creating the course');
}
return redirect()->route ('admin.courses.index');
}
public function destroy(Course $course)
{
if(Gate::denies('delete_courses'))
{
return redirect (route('admin.course.index'));
}
$course->delete();
return redirect()->route('admin.courses.index');
}
public function edit(Course $course)
{
if(Gate::denies('edit_courses'))
{
return redirect (route('admin.courses.index'));
}
$instructors = User::whereHas('role', function ($query) {
$query->where('role_id', 2); })->get()->pluck('name');
return view('admin.course.edit')->with([
'course' => $course
]);
}
public function update(Request $request, Course $course)
{
$course->update($request->all());
if ($course->save()){
$request->session()->flash('success', $course->title . ' has been updated successfully.');
}else{
$request->session()->flash('error', 'There was an error updating ' . $course->title);
}
return redirect()->route('admin.courses.index');
}
public function show(Course $course)
{
return view('admin.course.show', compact('course'));
}
}
I am new to laravel so I would appreciate any help.

Migration nullable field causing issues

I have created a migration called 'create_modules_table' see code below.
public function up()
{
Schema::create('modules', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('course_id')->nullable();
$table->foreign('course_id')->references('id')->on('courses');
$table->string('title');
$table->timestamps();
});
}
I have added a FK 'course_id'. Without 'nullable' i faced an error relating to 'course_id' not having a default value. So i added 'nullable'. However now it is throwing me further problems, when i create a new module and select the course to attach to the module, the field in the DB is null (despite selecting an existing course from my form). I have added an image for context. Can anyone tell me where i am going wrong? I'm not sure how to go about fixing this.
Module.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Module extends Model
{
protected $fillable = [
'title', 'course_id', 'created_at', 'updated_at',
];
public function course()
{
return $this->belongsTo(Course::class, 'course_id');
}
}
ModulesController;
<?php
namespace App\Http\Controllers\Admin;
use App\Module;
use App\Course;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ModulesController extends Controller
{
public function index(Request $request)
{
$modules = new module();
if ($request->input('course_id')) {
$modules = $modules->where('course_id', $request->input('course_id'));
}
$modules = $modules->all();
return view('admin.module.index', compact('modules'));
}
public function create()
{
$courses = Course::all()->pluck('title', 'id');
return view('admin.module.create', compact('courses'));
}
public function store(Request $request)
{
$module = Module::create($request->all());
return redirect()->route('admin.modules.index', ['course_id' => $request->id]); //redirects to correct route by adding course_id in parameter
}
}
create.blade.php;
#extends('layouts.app')
#section('content')
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Create Module</div>
<div class="card-body">
<form method="POST" action="{{ route('admin.modules.store') }}" enctype="multipart/form-data">
#csrf
<div class="form-group">
<label class="required" for="name">Module Title</label>
<input class="form-control" type="text" name="title" id="id" required>
#if($errors->has('name'))
<div class="invalid-feedback">
{{ $errors->first('name') }}
</div>
#endif
</div>
<div class="form-group">
{!! Form::label('Courses', 'Course', ['class' => 'control-label']) !!}
{!! Form::select('Course[]', $courses, Request::get('Course'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!}
</div>
<div class="form-group">
<button class="btn btn-danger" type="submit">
Save
</button>
</div>
</div>
</form>
</div>
</div>
#endsection
index.blade.php;
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<p>
<button type="button" class="btn btn-success">Create Module</button>
</p>
<div class="card">
<div class="card-header">Modules</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Module Title</th>
<th>Course Title</th>
<th>Instructor</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#foreach($modules as $key => $module)
<tr>
<th scope="row">{{ $module->id }}</th>
<td>{{ $module->title }}</td>
<td>{{ $module->course->title ?? ''}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
#endsection
I would like to add there is no error, the DB is simply not displaying an input.. for some reason. Any help is kindly appreciated.
In your form there is no field named course_id, instead you've a field with name Course[] which is a multi-select, so in your store method when you are using Module::create($request->all()); there is no field matching in your $fillable array in the Module model, so you need to map fields manually and also, since you are using an array (because multi-select produces array), you need to use single element from the array to create a relation.
So, one easy approach could be ():
Module::create([
'title' => $request->input('title'),
'course_id' => reset($request->input('course')) // get the first item from array
]);
I think your are missing something, you should not use a multi-select for a foreign key unless you've a many-to-many relation and in that case you'll create/attach multiple related entries in a pivot table.

Laravel Errors validation returning null

Yesterday I started with Laravel, currently busy with my first project, a simple news page.
Unfortunately, I've met some problems while validating my post request, I've tried to search on Google for my issue. And I tried to use those fixes, but strange enough I had no result.
The problem is:
When I post data the normal 'form page' will be shown without any errors. I'm aware that I have double error outputs, it's just for the test.
What do I want to reach?
I want that the validation error will be shown
routes.php
<?php
Route::group(['middleware' => ['web']], function() {
Route::get('/', function() {
return redirect()->route('home');
});
Route::get('/home', [
'as' => 'home',
'uses' => 'HomeController#home',
]);
Route::get('/add_article', [
'as' => 'add_article',
'uses' => 'HomeController#add_article',
]);
Route::post('/add_article', [
'as' => 'add_article.newarticle',
'uses' => 'HomeController#post_article',
]);
});
HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\News;
class HomeController extends Controller
{
public function home(News $news)
{
$articles = $news->orderBy('id','desc')->take(3)->get();
return view('home.home')->with('articles', $articles);
}
public function add_article()
{
return view('home.add_article');
}
public function post_article(Request $request)
{
$this->validate($request, [
'title' => 'required|max:64',
'content' => 'required|max:2048',
]);
// $newNews = new News;
// $newNews->title = $request->title;
// $newNews->content = $request->content;
// $newNews->save();
}
}
add_article.blade.php
#extends('templates.default')
#section('content')
<div class="row">
<div class="col-sm-12 col-md-6 col-lg-6 col-xl-6 offset-md-3 offset-lg-3 offset-xl-3">
<p class="lead">New news article</p>
<div class="card">
<div class="card-header">
<h5 class="mb-0"> </h5>
</div>
<div class="card-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form method="post" action="{{ route('add_article.newarticle') }}">
<div class="form-group">
<label for="title" style="margin-bottom:0px;">
Title:
</label>
<input class="form-control" type="text" name="title" placeholder="Please enter your title!" id="title">
#if ($errors->has('title'))
{{ $errors->first('title') }}
#endif
</div>
<div class="form-group">
<label for="content" style="margin-bottom:0px;">
Content:
</label>
<textarea class="form-control" rows="3" name="content" placeholder="Please enter your message!" id="content" style="resize:none;"></textarea>
#if ($errors->has('content'))
{{ $errors->first('content') }}
#endif
</div>
<div class="form-group text-right">
<button class="btn btn-primary" type="submit">
Create news
</button>
</div>
{{ csrf_field() }}
</form>
</div>
</div>
</div>
</div>
#endsection
I hope someone can help me to resolve this issue!
use App\Http\Requests;
Remove This from HomeController.php and try.
Your validation is passing but you are not doing anything after so it's not going to show anything unless you tell it to.
Also make sure you use $request->all() instead of $request in the validator as the first one will return the actual input values that were submitted.
use Validator;
public function post_article(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|max:64',
'content' => 'required|max:2048',
]);
if ($validator->fails()) {
return redirect('home')
->withErrors($validator)
->withInput();
}
// $newNews = new News;
// $newNews->title = $request->title;
// $newNews->content = $request->content;
// $newNews->save();
return redirect()->route('home')->with('message', 'Article created.');
}
}
Then in your view add the following at the top:
#if(Session::has('message'))
<div class="alert alert-success">
×
{{ Session::get('message') }}
</div>
#endif
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Based on those validation rules you will only see errors when the title is empty or longer than 64 characters, or the content is empty or longer than 2048 characters. Make sure the data you are testing with is long enough to trigger any errors.
For data that passes validation the code currently does not save (commented out), nor does it return a new location or view so it will show a blank page. You should probably save the data and redirect to the index or a show page with a success message.

Multiple select values being retrieved and not being sent as a single slug ID

Ok I've looked through the site and a few others trying to find an answer to this, but I am struggling. Essentially I am sending from my users index page -> user change primary and from there the user then chooses a house from the select box and then when accepts it sends the ID back so that I can modify and designate the house as being primary.
The code has been changed quite often as tried different solutions, however what happens is that when a user sends the form back to the index page it then submits all house ID's instead of only the specific ID of the house that has been selected.
This is the changeprimary view
#extends('app')
#section('links')
<ul class="nav navbar-nav">
<li>Back</li>
</ul>
#stop
#section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="form-box">
<div class="panel panel-default">
<div class="panel-heading" style="padding-left: 30px;">Change your password</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<p>
{!! Form::model($houses,['method' => 'POST','route'=>['user.cprimary',$house_id->$houses->id]]) !!}
{!! Form::open(['url' => 'user']) !!}
<div class="form-group">
#if($houses != null)
<div class="form-group">
{!! Form::label('primary', 'Choose a house to make primary') !!}
{{--{!! Form::select('primary', $houses, $houses) !!}--}}
{!! Form::select('primary', $houses) !!}
</div>
{!! Form::close() !!}
<div class="container">
{!! Form::submit('Make House Primary House', ['class' => 'btn btn-primary']) !!}
</div>
#else
<li><a href={{url('/house/create')}}>Add new house</a></li>
</form>
#endif
</div>
</div>
</div>
</div>
</div>
</div>
</div>
This is the user controller for show primary and change priamry
public function showcp()
{
$houses = House::where('user_id', '=', Auth::id())
->lists('h_name');
$house_id = House::where('user_id', '=', Auth::id())
->lists('id');
// foreach($house_ids as $house_id) {
// $house_id = $house_id->id;
// }
// $house = House::find($house_id)->h_name->first();
if (!$houses->isEmpty()) {
return view('users.changeprimary', compact('houses','house_id'));
}
return view('house.nohouse');
}
public function changeprimary($id)
{
//Find and update password linked to User ID.
$house_all = House::all()->where('user_id', Auth::id());
foreach($house_all as $houses)
{
$houses->primary = '0';
$houses->save();
}
$houses->save();
$updateHouse = House::find($id);
$updateHouse->primary = '1';
$updateHouse->save();
return redirect('user')->with('message', 'House '.House::find($id)->h_name.' is primary');
}
And this is the code for the index view
#extends('app')
#section('links')
<ul class="nav navbar-nav">
<li>Back</li>
</ul>
#stop
#section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="form-box">
<div class="panel panel-default">
<div class="panel-heading">Profile Options</div>
<div class="panel-body">
#if (session('message'))
<div class="alert alert-info" style="text-align: center; width: 200px; ">
{{ session('message') }}
</div>
#endif
{!! Form::open() !!}
<div class="form-group">
<!-- Modify settings accordingly -->
<div class="form-group">
Change User Settings and Details
</div>
<div class="form-group">
Change Password
</div>
<div class="form-group">
Change Primary House
</div>
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
</div>
#stop
And this is my routes file
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
//Home Routes
Route::get('/', 'HomeController#index');
Route::get('home', 'HomeController#index');
//User Routes
Route::post('user/{id}/cprimary',['as' => 'user.cprimary', 'uses' => 'UserController#changeprimary']);
Route::get('user/changeprimary',['as' => 'user.changeprimary', 'uses' => 'UserController#showcp']);
Route::get('user/changepassword', 'UserController#changepassword');
Route::any('user/cpassword',['as' => 'user.cpassword', 'uses' => 'UserController#cpassword']);
Route::resource('user','UserController');
//House Routes
Route::get('createfirsthouse', 'CreateFirstHouseController#index');
Route::get('/house/alternate', 'HouseController#alternate_index');
Route::resource('house', 'HouseController');
//Google Routes
Route::get('googlemap', 'GoogleMapsTest#index');
//Profile Routes
Route::resource('profile', 'ProfileController');
Route::get('profile/resetpassword', 'ProfileController#resetpassword');
//Route to reverse schemas and get migrations.
//Route::get('tc', 'TController#index');
//Any Controller Routes not covered above
Route::controllers([
'auth' => 'Auth\AuthController',
'password' => 'Auth\PasswordController',
]);
Route::get('/dash', 'DashController#index');
Route::get('/', 'DashController#index');
Route::get('/temp', 'TempReset#index');
Route::resource('dash', 'DashController');
Ok the correct answer was changing the controller, not sure why I was adding both options for the form::open, I saw on laracasts and assumed it to be the standard.
public function changeprimary($id)
{
// Get the current users ID
$users= Auth::id();
// Fetch all houses linked to user and loop through collection setting primary to 0
$allhouses=House::where('user_id', $users)->get();
foreach($allhouses as $all) {
$all->primary = '0';
$all->save();
}
// Retrieve the selected input
$houses = Input::get('primary');
// Find house linked to ID
$house = House::find($houses);
$house->primary = '1';
$house->save();
return redirect('user')->with('message', 'House '.$house->h_name.' is primary');
}
That was the correct code I needed to solve this :)

Categories