Method links do not exist - Laravel 5.5 - php

I'm getting the following error:
Method links do not exist
I also tried render function which gives me the same result.
This is my code:
public function welcome() {
$products = DB::table('products')->paginate(12);
return view('welcome', compact('products', $products));
}
When I call {{ $products->links() }}, it shows an error but if I remove {{ $products->links() }} I can see the results.
This is my view:
#foreach($products as $product)
<div class="col-sm-6 col-md-4 col-lg-3">
<!-- Product item -->
<div class="product-item hover-img">
<a href="product_detail.html" class="product-img">
<img src="images/default.png" alt="image">
</a>
<div class="product-caption">
<h4 class="product-name">{!! $product->name !!}</h4>
<div class="product-price-group">
<span class="product-price">{!! $product->price !!} KM</span>
</div>
</div>
<div class="absolute-caption">
<form action="{!! route('store') !!}" method="POST">
{!! csrf_field() !!}
<input type="hidden" name="id" value="{{ $product->id }}">
<input type="hidden" name="name" value="{{ $product->name }}">
<input type="hidden" name="price" value="{{ $product->price }}">
<input type="submit" class="btn btn-success btn-lg" value="Dodaj u korpu">
</form>
</div>
</div>
</div>
#endforeach
{{ $products->links() }}

You should use Eloquent instead of query builder.
Use:
$products = \App\Product::paginate(12);
instead of
$products = DB::table('products')->paginate(12);
to make it work.
(Of course you need to have created Product model first extending Eloquent model)

Related

Laravel HTML form issue

<form action="/group/{id}/save/group" method="POST">
#csrf
<table id="modal-table">
#foreach($user_list->get() as $f)
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
#endforeach
</table>
</form>
Hey a small problem! in the Foreach loop and every member in the list, I tried some bare-bones to get each users id and hobby id (to add to database, but that's not important). For some reason, when I take the input 'name' and click the Go button, All "id's" of everyone in the list are executed, Not the individual id of each user. Meaning.. each user displays their individual id and a go button, but click go and all id's are collected, not just one. It's probably really obvious but any ideas why this is happening?? Thanks!
Like said Tim Lewis in the comment, you need to put your form inside your foreach, so when the "Go" button is clicked, only the corresponding form will send the data, like this :
<table id="modal-table">
#foreach($user_list->get() as $f)
<form action="/group/{id}/save/group" method="POST">
#csrf
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
</form>
#endforeach
</table>
<table id="modal-table">
#foreach($user_list->get() as $f)
<form action="/group/{id}/save/group" method="POST">
#csrf
<tr>
<td>
<div class="image">
#if($f->follower->avatar_location)
<img src="{{asset('storage/'.$f->follower->avatar_location)}}" class="img-circle" style="max-height:50px;" />
#else
<img src="{{url('/')}}/assets/media/icons/socialbuttons/user.png" class="img-circle" style="max-height:50px;"/>
#endif
</div>
<div class="detail">
#if($f->follower->verified == 1)
{{ $f->follower->name }}<img id="verified_img_sm_mess_list" src="{{url('/')}}/assets/media/icons/socialbuttons/octagonal_star.png" alt="Recensioni">
#else
{{ $f->follower->name }}</h3>
#endif
<small id="mess_list_uname">{{ '#'.$f->follower->username }}</small>
</div>
<input name="group" value="{{ $group->hobby->id }}">
<input name="person" value="{{ $f->follower->id }}">
<button type="submit" class="btn-link">Go</button>
</td>
</tr>
</form>
#endforeach
</table>
check this part of Jérôme W post
<form action="/group/{id}/save/group" method="POST">
Change to this
<form action="/group/{{$group->id}}/save/group" method="POST">

Laravel Form displaying correct data but storing incorrect values in DB

I have come across a bug on my website and I am baffled as to how to fix it.. basically I have a view to create courses, I type a course title and secondly assign an instructor to that course and click save. But when i return to my index page.. the instructor i assigned is completely different. It appears there must be an issue with my select but I am not sure how to go about fixing it.. I am new to laravel so any help is greatly appreciated.
I have added a picture to try to further explain my issue, here i have selected an instructor -
And when i return to my index page, the newly created course 'test' has the admin user assigned... I am very confused -
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 {{ $errors->has('instructors') ? 'has-error' : '' }}">
<label class="required" for="name">Instructors</label>
<select class="form-control select2" name="instructors[]" id="instructors" multiple>
#foreach($instructors as $id => $instructors)
<option value="{{ $id }}" {{ in_array($id, old('instructors', [])) ? 'selected' : '' }}>{{ $instructors }}</option>
#endforeach
</select>
</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
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', compact('instructors'));
return view('admin.course.edit', compact('instructors'))->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'));
}
}
Referring to latest comment -
you are updating data with key not id
try this
in controller
$instructors = User::whereHas('role', function ($query) {
$query->where('role_id', 2); })->select('id','name')->get();
in blade
you will need instructors->id to update right value
#foreach($instructors as $id => $instructor)
<option value="{{ $instructor->id }}" {{ in_array($id, old('instructors', [])) ? 'selected' : '' }}>{{ $instructor->name }}
</option>
#endforeach

how to fix "Undefined variable: subtask" in Laravel 5.2

need edit My subtask table records, SubtaskController
public function edit($projectId,$taskId,$subtaskId)
{
$subtask = Subtask::where('project_id', $projectId)
->where('task_id', $taskId)
->where('id', $subtaskId)
->first();
return view('subtasks.edit')->withTask($subtask)->with('projectId', $projectId)->with('id',$subtaskId);
//
blade file link to edit button is this,
tasks/index.blade.php
#foreach ($task->subtasks as $subtask)
<ul>
<li>
<div>
<div class="pull-right icons-align">
<i class="glyphicon glyphicon-pencil"></i>
<i class="glyphicon glyphicon-trash"></i>
</div>
{{ $subtask->subtask_name }}
</div>
#endforeach
}
and route is this
Route::get('projects/{projects}/tasks/{tasks}/subtasks/{subtasks}/edit', [
'uses' => '\App\Http\Controllers\SubtasksController#edit',
]);
and edit.blade.php
subtasks/edit.blade.php
<div class="col-lg-6">
<form class="form-vertical" role="form" method="post" action="{{ url('projects/' .$projectId .'/tasks/' . $task->id.'/subtasks/'.$subtask->id) }}"> //this is line 9
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<input type="text" name="task_name" class="form-control" id="name" value="{!! $subtask->subtask_name ?: '' !!}">
#if ($errors->has('name'))
<span class="help-block">{{ $errors->first('name') }}</span>
#endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Update Subtask</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
{!! method_field('PUT') !!}
</form>
but when I click edit button fron blade file got this error massage,
ErrorException in 9b021095a9def87aed61575bb51efc07798e08da.php line 9: Undefined variable: subtask (View: C:\Users\dnk\Desktop\ddd\resources\views\subtasks\edit.blade.php)
how can solve this problem?
Instead of:
->withTask($subtask)
Do this:
->withSubtask($subtask)
And in the edit view change this:
$task->id
To:
$subtask->task->id
To make the second fix work, you also need to have a properly defined relationship in Subtask model:
public function task()
{
return $this->belongsTo(Task::class);
}

How i delete name of one image from name of images in array?

I have a Multiple images in array in row like:
["1.jpg","2.jpg"]
I need delete name of one image from name of images in array in db,
this is my form to show and delete submit:
#foreach(json_decode($post->images ,true) as $images)
<div class="col-lg-2 col-md-2 col-sm-2">
<div class="portfolio-box">
<img src="public/img/offers/{{ $images }}" class="img-responsive"alt="">
<div class="portfolio-box-caption">
<div class="portfolio-box-caption-content">
<form action="images/{{ $images }}/delete" method="get">
{!! csrf_field() !!}
<input type="submit" class="btn btn-danger" value="Delete">
</form>
</div>
</div>
</div>
</div>
#endforeach
and this is my Route:
Route::get('images/{images}/delete','DashboardController#deleteimg');
what i do in Controller ??
Pass post id in form
#foreach(json_decode($post->images ,true) as $images)
<div class="col-lg-2 col-md-2 col-sm-2">
<div class="portfolio-box">
<img src="public/img/offers/{{ $images }}" class="img-responsive"alt="">
<div class="portfolio-box-caption">
<div class="portfolio-box-caption-content">
<form action="images/{{ $images }}/delete" method="get">
{!! csrf_field() !!}
<input type="hidden" name="post_id" value="{{$post->id}}"/>
<input type="submit" class="btn btn-danger" value="Delete">
</form>
</div>
</div>
</div>
</div>
#endforeach
And in controller
public function deleteimg($image,Request $req){
$post=Post::find($req['post_id']);
$images=json_decode($post->images);
$_image=[];
$_image[]=$image;
$post->images=json_encode(array_values(array_diff($images,$_image)));
$post->save();
return redirect()->back();
}
OR
public function deleteimg($image,Request $req){
$post=Post::find($req['post_id']);
$images=json_decode($post->images);
unset($images[$image]);
$post->images=json_encode(array_values(images));
$post->save();
return redirect()->back();
}

Laravel 5.4: REST Controller 'Edit' view not displaying existing data, and throwing 'MethodNotAllowedHttpException' on update

The 'edit' view for my blog posts is accessed via the index view which looks like this:
#extends('layouts.app')
#section('stylesheet')
<link rel="stylesheet" href="{{ asset('css/backend.css') }}">
#endsection
#section('content')
<h1 class="backend_title">Viewing All Posts</h1>
<hr class="divider">
#foreach( $posts as $post )
<div class="backend_index-listing">
<label class="backend_index-listing_title">{{ $post->title }}</label>
<div class="backend_route-group">
<a class="backend_route" href="{{ route('posts.edit', $post) }}">Edit</a>
<form method="POST" action="{{ route('posts.destroy', $post) }}">
{{ csrf_field() }}
<input type="submit" value="Delete" class="backend_route">
<input type="hidden" name="_token" value="{{ Session::token() }}">
{{ method_field('DELETE') }}
</form>
</div>
</div>
#endforeach
#endsection
The edit view is as follows:
#extends('layouts.app')
#section('stylesheet')
<link rel="stylesheet" href="{{ asset('css/backend.css') }}">
#endsection
#section('content')
<h1 class="backend_title">Edit Post</h1>
<hr class="divider">
<form class="backend_form" method="POST" action="{{ route('posts.update', $posts) }}">
{{ csrf_field() }}
<div class="backend_form-group">
<label for="title" class="backend_label">Title</label>
<input id="title" name="title" class="backend_input" required value="{{ $posts->title }}">
</div>
<div class="backend_form-group">
<label for="slug" class="backend_label">Slug</label>
<input id="slug" name="slug" class="backend_input" required value="{{ $posts->slug }}">
</div>
<div class="backend_form-group">
<label for="body" class="backend_label">{{ $posts->body }}</label>
<textarea id="body" name="body" class="backend_textarea" rows="40" required>{{ $posts->body }}</textarea>
</div>
<input class="backend_submit" type="submit" value="Update">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
#endsection
Here's the edit function in the PostsController.
public function edit(Posts $posts)
{
return view('posts.edit')->withPosts($posts);
}
And the update function.
public function update(Request $request, Posts $posts)
{
$this->validate($request, array(
'title' => 'required|255',
'slug' => 'required|255',
'body' => 'required'
));
$posts->title = $request->title;
$posts->slug = $request->slug;
$posts->body = $request->body;
$posts->save();
return redirect('posts');
}
I have a test post that I created through my create form, so create and store are working. It shows up just fine on index with the information I want. But when I click the edit button, it takes me to an empty edit view, and if I fill the fields and hit the update button, I get the 'MethodNotAllowed' exception as in the title.
Try to change
href="{{ route('posts.edit', $post) }}"
on
href="{{ route('posts.edit', ['post' => $post]) }}"
Hope it help you to find right solution :)

Categories