Use of Undefined Constant Demolays - Assumed [Laravel Error] - php

I got an error in laravel, but idk if is on controller or blade file.
Controller Code:
public function edit($id)
{
$demolays = tbl_demolay::find($id);
return view('demolay.edit', compact(demolays));
}
Blade Code:
{!! Form::model(['method' => 'PATCH','route' => ['demolay.update', $demolays->id]]) !!}
#include('demolay.form')
{!! Form::close() !!}

You're getting this error because you need to pass string to the compact(). So, change it to:
return view('demolay.edit', compact('demolays'));

Related

Cannot pass full array from controller in laravel to a view using redirect()

I am unable to solve passing of array issue
below is my function in controller
public function fetchData($id)
{
$id=base64_decode(urldecode($id));
prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}
below is my route
Route::get('/product_view', function(){
return view('/admin/product_d_mgt');
})->name('prod_d_view');
below is my error
Undefined variable: prod_detail (View: \admin\product_d_mgt.blade.php)
I am unable to pass the full array from one controller using redirect()->route() to another view
Maybe you can use something like this:
In your controller function:
...
return Redirect::to('product_view')->with('prod_detail', $prod_detail);
And in your product_view.blade.php file (in resources/view directory):
#if(Session::has('prod_detail'))
#foreach (Session::get('prod_detail')as $key => $value)
{{ $value->ColumnName }}
{{ $value->ColumnName2 }}
#endforeach
#endif
It has typo. Missing $ symbol before variable name prod_detail.
correct version:
public function fetchData($id)
{
$id = base64_decode(urldecode($id));
$prod_detail=ProductDetail::select('prod_id','supplier_id','price','open_stock','discount_rate','min_order_level')->where('prod_id','=',$id)->get();
return redirect()->route('prod_d_view', compact($prod_detail));
}

Trying to get property of non-object laravel when accessing database

I am trying to code the edit route for laravel and for some reason keep getting the error "Trying to get property of non-object laravel". The Create controller works fine, however when I use the controller#update route I keep getting this error
My Controller for adding an event: (update)
public function update(Request $request, $id)
{
//create event
$my_user = my::find($id);
$my_user->programme = $request->input('programme');
$my_user->goal1 = $request->input('goal1');
$my_user->goal2 = $request->input('goal2');
$my_user->goal3 = $request->input('goal3');
$my_user->goal4 = $request->input('goal4');
$my_user->goal5 = $request->input('goal5');
$my_user->user_id = auth()->user()->id;
$my_user->save();
return redirect('/home')->with('success','Event Created');
}
edit page
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Edit Post</h1>
{!! Form::open(['action' => ['myUserController#update', $my_user], 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('title', 'Event Name')}}
{{Form::text('goal1', $my_user->goal1, ['class' => 'form-control', 'placeholder' => 'Goal One'])}}
</div>
{{Form::hidden('_method','PUT')}}
{{Form::submit('Submit', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
</div>
#endsection
Given that you are using a Route::resource you can type-hint your parameters by writing something like
public function update(Request $request, MyUser $myuser){
// The $myuser parameter comes from the Route::resource and can be verified using php artisan route:list in your terminal
// edit $my_user
$my_user->save();
return redirect('/home')->with('success','Event Created');
}
Update after reviewing LaravelCollective documentation for Form
Thank you Sohel0415 for mentioning that you do not need to call $my_user->id for providing the route parameter with the Form facade.
You can use this method on your code:
{{ Form::open(array('url'=>'admin/users/store' , 'method' =>'POST')) }}
and your route define by this method in web.php file:
Route::post('users/store', 'admin\UserController#store');

Getting MethodNotAllowedHttpException error in Laravel

I'm learning Laravel. I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car (identified by $modelesc).
This form sends the data to a "orders" table. With the code that I have now, I'm not able to post the order on the order table. it gets the message: "MethodNotAllowedHttpException in RouteCollection.php line 233:"
This is the code
Web.php
Route::get('catalog', 'carController#catalog');
Route::get('orders/', 'CarController#orders')->name('orders');
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders(Request $request) {
$modelesc = $request->modelesc;
$cars = DB::table('cars')->where('Model', $modelesc)->get();
$colours = DB::table('colours')->get()->pluck('Colour');
return view('orders', compact('cars', 'colours'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'CarController#orders', 'method' => 'GET')) !!}
{!! Form::hidden('$modelesc', $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
#endforeach
Orders.blade.php
#foreach($cars as $car)
{!! Form::open(['method' => 'POST']) !!}
{{ $car->Model }}
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::select('Colour_id', $colours) !!}
{!! Form::hidden('Order_status_id', '1') !!}
{!! Form::submit('Ok') !!}
{!! Form::close() !!}
#endforeach
The table orders has following structure:
$table->increments('id');
$table->integer('users_id')->unsigned();
$table->foreign('users_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('Model');
$table->date('Fabrication_date');
$table->integer('Colour_id')->unsigned();
$table->foreign('Colour_id')->references('id')->on('colours')->onDelete('cascade')->onUpdate('cascade');
$table->integer('Order_status_id')->unsigned();
$table->foreign('Order_status_id')->references('id')->on('order_status')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
Update your orders function
public function orders(Request $request)
{
$modelesc = $request->modelesc;
$cars = DB::table('cars')->where('Model', $modelesc)->get();
...
}
Catalog.blade.php
Form::hidden('modelesc', $car->Model)
The value of your hidden input will be read by request->modelesc and you will endup with a URL that looks like this http://mysite.dev/orders?modelesc=toyota.
Some advice
You using a form to only submit a hidden input. No user input. It seems to me it would be a easier to use simple anchors <a></a>.
#foreach($cars as $car)
{{ $car->Model }}
#endforeach
Same result.
Make it nice with pretty URLs. Change your route definition
Route::get('orders/{modelesc}', 'CarController#orders')->name('orders');
And the anchor
{{ $car->Model }}
With named route
{{ $car->Model }}
And the function
public function orders($modelesc)
{
$cars = DB::table('cars')->where('Model', $modelesc)->get();
...
}
change your route to:
Route::get('orders/{model}', 'CarController#orders')->name('orders');
after this change your Controller to:
public function orders($model) {
$cars = DB::table('cars')->where('Model', $model)->get();
if(!count($cars)){
abort(404);
}
$colours = DB::table('colours')->get()->pluck('Colour');
//$status = DB::select('select * from order_status where id = ?', [1])->get(); //this variable is never used
return view('orders', compact('cars', 'colours'));
}
and now change your Catalog.blade.php
in your case you don't need a form for go in your order page
#foreach($cars as $car)
{{ $car->Model }}
#endforeach

Laravel5: Can't delete from DB

This is in controller MessagesController:
public function destroy(Messages $id)
{
\DB::delete(
'DELETE FROM messages WHERE id=' . $id
);
return redirect()->route('messages.index')->with('message', 'Deleted.');
}
This is routes.php:
Route::delete('messages.destroy', ['as' => 'messages.destroy', 'uses' => 'MessagesController#destroy']);
This is view file:
{!! Form::open(array('route'=>['messages.destroy',$message->id],'method'=>'DELETE')) !!}
{!! Form::button('Delete',['class'=>'btn btn-danger','type'=>'submit']) !!}
{!! Form::close() !!}
So, I have an error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near '[]' at line 1
(SQL: DELETE FROM messages WHERE id=[])
I understand that there is no id going to controller. How to solve this?
P.S. Also, tried like this in controller:
public function destroy(Messages $message)
{
$message->delete();
return redirect()->route('messages.index')->with('message', 'Deleted.');
}
It's just showing message Deleted. but it's not deleting a thing.
Here is an example of what i'm doing:
routes/web.php :
Route::resource('products', 'Products');
controller:
public function destroy($id)
{
Product::destroy($id);
return redirect()->route('products.index')
->with('success','Product deleted successfully');
}
View:
{!! Form::open(['method' => 'DELETE','route' => ['products.destroy', $product->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
Try this route:
Route::delete('messages.destroy/{id}', ['as' => 'messages.destroy', 'uses' => 'MessagesController#destroy']);
and this controller:
public function destroy($id)
{
Message::destroy($id);
return redirect()->route('messages.index')->with('message', 'Deleted.');
}

laravel 5 won't delete files from storage section

when I try to delete specific file trough my controller I get an error
Sorry, the page you are looking for could not be found.
NotFoundHttpException
This is my controller:
public function destroyFile($file_name)
{
$file = storage_path('documents').'/'.$file_name;
Storage::delete($file);
return redirect('/documents');
}
My route:
Route::delete('documents/{file}','FilesController#destroyFile');
And my view:
{!! Form::open(['method' => 'DELETE', 'action' => ['FilesController#destroyFile', $file->name] ]) !!}
{!! Form::hidden('_method', 'DELETE') !!}
{!! Form::token() !!}
{!! Form::submit(trans('buttons.del-cat'),['class'=>'btn btn-danger user-delete push-right']) !!}
{!! Form::close() !!}
I had the same problem, everything was set right but it still didn't work.
Instead of Storage::delete() I used File::delete(), that fixed it.
Hope this works!
public function images(Request $request,$id){
$user_id=$request->session()->get('user_id');
$data=DB::table('company_images')->where('id',$id)->first();
//deleteing the image from database and server
DB::table('company_images')->where('id', '=', $id)->delete();
//delete the image form the server
$path=public_path("/assets/img/company/$data->image");
unlink($path);
return redirect("/company/$user_id/add_details")->with('delete','delete');
}
this is function i used to delete company images using unlink($path) this works well with me also the big note:you should write carefully the path to file you delete.
You must set public in filesystem.php :
'default' => env('FILESYSTEM_DRIVER', 'public')
instead of :
'default' => env('FILESYSTEM_DRIVER', 'local')
change local to public.

Categories