This is my route.
Route::get('discussion/{slug}',[
'use' => 'DiscussionsController#show',
'as' => 'discussion.show'
]);
This is show function
public function show($slug)
{
$discussion = Discussion::where('slug', $slug)->first();
return view('discussions.show', compact('discussion'));
}
i am getting this error.
view file like this
#section('content')
<div class="card">
<div class="card-header">{{$discussion->tittle}}</div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
</div>
</div>
#endsection
here i call redirect the route, and get the error
$discussion = Discussion::create([
'tittle' => $request->title,
'content' => $request->contant,
'chanel_id' => $request->channel_id,
'user_id' => Auth::id(),
'slug' => str_slug($request->title)
]);
return redirect()->route('discussion', ['slug' => $discussion->slug]);
ERR_MSG:
Route
Route::get('discussion/{slug}',['as'=>'discussion.show','use'=>'DiscussionsController#show']);
Controller
public function show($slug){
$discussion = Discussion::where('slug', $slug)->first();
return view('discuss', compact('discussion'));
}
Your blade file must be discuss.blade.php
You are going to use that discussion.show if you only need something like this on your view page
View Slug
Related
I'm trying to pass anINT from this URL: myapp.build/courses/anINT (implemented in the CoursesController) to $id in the Lesson_unitsController function below. I've tried a lot of solutions, but I can't seem to get it right.
The function in the CoursesController which implements the url is:
public function show($id)
{
$course = Course::find($id);
return view('courses.show')->with('course', $course);
}
Part of the show.blade.php file is:
#if(!Auth::guest())
#if(Auth::user()->id == $course->user_id)
Edit Course
Lesson Units
{!!Form::open(['action'=> ['CoursesController#destroy', $course->id], 'method' => 'POST', 'class' => 'float-right'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
#endif
#endif
The Lesson_unitsController functions are:
public function index()
{
$lesson_units = Lesson_unit::orderBy('title','asc')->paginate(10);
return view('lesson_units.index')->with('lesson_units', $lesson_units);
}
public function specificindex($id)
{
$course = Course::find($id);
return view('lesson_units.specificindex')->with('lesson_units', $course->lesson_units);
}
And the specificindex.blade.php file is:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Dashboard</div>
<div class="card-body">
Create lesson unit
<p>
<h3>Your lesson_units</h3>
#if(count($lesson_units) > 0)
<table class="table table-striped">
<tr><th>Title</th><th></th><th></th></tr>
#foreach($lesson_units as $lesson_unit)
<tr><td>{{$lesson_unit->title}}</td>
<td>Edit</td>
<td>
{!!Form::open(['action'=> ['Lesson_unitsController#destroy', $lesson_unit->id], 'method' => 'POST', 'class' => 'float-right'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
</td>
</tr>
#endforeach
</table>
#else
<p>You have no lesson unit.</p>
#endif
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
You are logged in!
</div> </div> </div> </div> </div>
#endsection
The routes in web.php are:
Route::resource('courses', 'CoursesController');
Route::resource('lesson_units', 'Lesson_unitsController');
Route::get('/courses/{id}', 'Lesson_unitsController#specificIndex');
I want that when the link for Lesson Units is clicked on the page, the id in the url is passed to the specificindex function in the Lesson_unitsController. Now, I get just a blank page. What am I doing wrong?
Try to understand the concept of RESTful and CRUD.
By using Route::resource('courses', 'CoursesController');, Laravel has helped you to register the following routes:
Route::get('courses', 'CoursesController#index');
Route::get('courses/create', 'CoursesController#create');
Route::post('courses/{course}', 'CoursesController#store');
Route::get('courses/{course}/edit', 'CoursesController#edit');
Route::put('courses/{course}', 'CoursesController#update');
Route::delete('courses/{course}', 'CoursesController#destroy');
Then, when you make GET request to myapp.build/courses/123, Laravel will pass the request to the show function of your CoursesController like:
public function show(Course $course)
{
return view('lesson_units.index')->with('lesson_units', $course->lesson_units);
}
Laravel will automatically resolve the Course from your database using the parameter passed into the route myapp.build/courses/{course}.
Note: The variable name $course has to match with the one specify in route /{course}.
You don't have a route set up to handle the $id coming in. The resource method within the Route class will provide a GET route into your Lesson_unitsController controller without an expectation of any variable. It is the default index route, and by default doesn't pass a variable.
There are a couple of ways to do this, but the easiest is to just create a new route for your specific need:
Route::get('lesson_units/{id}', 'Lesson_unitsController#specificIndex');
And then make your specificIndex function in your controller with an incoming variable:
public function specialIndex($id)
{
$course = Course::find($id);
// return view to whatever you like
}
HTH
Faced a problem) As far as I know, Session::flash(...) should be recorded in a session for only one show, but it does not disappear for me, when I update the page, I always have it.
Here is an example code:
public function update(Request $request){
$this->validate($request, [
'amount' => 'required|integer'
]);
$user = Auth::user();
$user->balance += $request->amount;
$user->save();
Session::flash('success', "Balance updated");
return redirect('/balance');
}
The message is displayed like this
#if(Session::has('success'))
<div class="alert alert-success alert-dismissable">
{{ Session::get('success') }}
</div>
#endif
#if(Session::has('error'))
<div class="alert alert-danger alert-dismissable">
{{ Session::get('error') }}
</div>
#endif
Help, please, I can not understand what the problem is. I will be very grateful.
try this, i hope help you
public function update(Request $request){
$this->validate($request, [
'amount' => 'required|integer'
]);
$user = Auth::user();
$user->balance += $request->amount;
$user->save();
return redirect('/balance')->with('success', 'Balance updated');
}
#if (session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
#endif
for more info please check document :
https://laravel.com/docs/5.7/redirects#redirecting-with-flashed-session-data
and
https://laravel.com/docs/5.7/session#flash-data
I am new to laravel and I want to let the user update his/her profile when logged in. I want to get the ID of the user when updating his/her profile but when I click on the edit view to pass the data using the id I got this error:
(1/1) ErrorException
Missing argument 1 for App\Http\Controllers\Applicant\HomeController::edit()
Here is the code to my controller:
public function edit($id)
{
$applicant = $this->applicantRepository->findWithoutFail($id);
if (empty($applicant)) {
Flash::error('Applicant');
return redirect(route('applicant.home'));
}
return view('applicant-dashboard.edit')->with('applicants', $applicant);
}
public function update($id, UpdateApplicantRequest $request)
{
$applicant = $this->applicantRepository->findWithoutFail($id);
if (empty($applicant)) {
Flash::error('Applicant not found');
return redirect(route('applicant.index'));
}
$input = $request->all();
$cashier = $this->applicantRepository->update([
'name' => $input['name'],
'email' => $input['email'],
'password' => bcrypt($input['password']),
'address' => $input['address'],
'cellphone_no' => $input['cellphone_no']], $id);
Flash::success('Profile updated successfully.');
return redirect(route('applicant.index'));
}
Here is the code in my routes file:
Route::get('/edit', 'HomeController#edit')->name('applicant.edit');
Here is the code in my blade file:
#extends('layouts.app')
#section('content')
<section class="content-header">
<h1>
Applicant Profile
</h1>
</section>
<div class="content">
{{-- #include('adminlte-templates::common.errors') --}}
<div class="box box-primary">
<div class="box-body">
<div class="row" style="padding-left: 20px">
{!! Form::model($applicant, ['route' => ['applicant.update', $applicant->id], 'method' => 'patch']) !!}
#include('applicant-dashboard.fields')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
#endsection
You need to pass the id into your route:
Route::get('/edit/{id}', 'HomeController#edit')->name('applicant.edit');
You pass the ID into web.php:
Route::get('edit/{ID}', 'HomeController#edit')->name('applicant.edit');
I'm new to laravel and trying to learn using it. I created a little project for threads. Nothing special. The thing is, the function to edit something doesn't work and I cant see why. Maybe someone of you see the mistake?
thats my Routes:
Route::get('/index', 'Test\\TestController#index');
Route::get('/add', 'Test\\TestController#add');
Route::post('/test', 'Test\\TestController#store');
Route::get('/show/{id}', 'Test\\TestController#show');
Route::get('/show/{id}/edit', ['as' => 'edit', 'uses' => 'Test\\TestController#edit']);
Route::put('/show/{id}/edit', ['as' => 'editing', 'uses' => 'Test\\TestController#update']);
thats the important parts of the edit method:
public function edit($id) {
$thread = Thread::query()->findOrFail($id);
return view('test.edit', [
'thread' => $thread
]);
}
public function update($id, StoreRequest $request) {
$thread = Thread::query()->findOrFail($id);
$thread->fill($request->all());
$thread->save();
return redirect(action('Test\\TestController#show', [$thread->id]));
}
}
show.blade
#extends('master')
#section('content')
<div class="panel panel-primary">
<div class="panel-heading">
<div class="panel-title">
{{ $thread->thread }}
</div>
</div>
<div class="form-body">
<div class="form-group"><br>
<ul>
{{$thread->content }}<br><br>
{{$thread->created_at->format('d.m.Y H:i:s')}}
</ul>
</div>
</div>
<div class="panel-footer">
<div class="btn btn-primary">Thread bearbeiten</div>
</div>
</div>
#stop
edit blade ( formular where the text I want to add is in the textbox and the save button )
#extends('master')
#section('content')
{!! Former::horizontal_open()->method('PUT')->action(action("Test\\TestController#update", [$thread->id])) !!}
{!! Former::populate($thread) !!}
{!! Former::text('thread')->label('Thread') !!}
{!! Former::text("content")->label("Content") !!}
{!! Former::large_primary_submit('Save!') !!}
{!! Former::close() !!}
#stop
model:
<?php
namespace App\Models\Thread;
use Illuminate\Database\Eloquent\Model;
class Thread extends Model {
public $table = 'thread';
public $fillable = [
'thread',
'content',
];
}
ERROR MESSAGE :
No query results for model [App\Models\Thread\Thread].
So I cant figure out why this isnt passing the variable to my controller.
Heres my /Controllers/FriendController.php getAccept Function:
public function getAccept($username)
{
$user = User::where('username', $username)->first();
if (!$user) {
return redirect()->route('home')->with('info', 'That user could not be found!');
}
if (!Auth::user()->hasFriendRequestRecieved($user)) {
return redirect()->route('home');
}
Auth::user()->acceptFriendRequest($user);
return redirect()->route('profile.index', ['username' => $user->username])->with('info', 'Friend request acccepted.');
}
}
Heres my blade where the accept friend request button is:
#extends('templates.default')
#section('content')
<div class="row">
<div class="col-lg-5">
#include('user.partials.userblock')
<hr>
</div>
<div class="col-lg-4 col-lg-offset-3">
#if (Auth::user()->hasFriendRequestPending($user))
<p>Waiting for {{ $user->getNameOrUsername() }} to accept your request.</p>
#elseif (Auth::user()->hasFriendRequestRecieved($user))
Accept friend request
#elseif (Auth::user()->isFriendWith($user))
<p>You and {{ $user->getNameOrUsername() }} are friends.</p>
#else
Add as friend
#endif
<h4>{{ $user->getFirstNameOrUsername() }}'s friends.</h4>
#if (!$user->friends()->count())
<p>{{ $user->getFirstNameOrUsername() }} has no friends.</p>
#else
#foreach ($user->friends() as $user)
#include('user/partials/userblock')
#endforeach
#endif
</div>
</div>
#stop
Ok I fixed this myself, left out a variable in the routing so now my routing looks like this:
Route::get('friends/accept/{username}', [
'uses' => '\Aries\Http\Controllers\FriendController#getAccept',
'as' => 'friends.accept',
'middleware' => ['auth'],
]);
Forgot that:
{username}