Laravel :Attempt to assign property of non-object - php

Error:
Attempt to assign property of non-object; ErrorException in AdminController.php line 40:
AdminController:
public function createSlider(AdminRequest $request)
{
$input = Request::all();
Sliders::create($input);
if (Request::hasFile('image')) {
$imageName = Request::input('title'). '.' .
$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(
base_path() . '/public/assets/image/', $imageName
);
$input->image = $imageName; //------------> line 40.......
}
$input->save();
}
Html:
{!!Form::open(array('url' => 'admin/new_slider', 'files' => true)) !!}
<div class = "form-group">
{!!Form::label('title', 'Title:', ['class' => 'control-label']) !!}
{!!Form::text('title', null, ['class'=> 'input-mini ina tch'])!!}
{!!Form::label('title', 'Description:', ['class' => 'control-label']) !!}
{!!Form::text('description', null, ['class'=> 'input-mini '])!!}
</div>
<div class = "form-group">
{!!Form::label('title', 'Link:', ['class' => 'control-label']) !!}
{!!Form::text('link', null, ['class'=> 'input-mini'])!!}
{!!Form::label('title', 'Image:', ['class' => 'control-label']) !!}
{!! Form::file('image', ['id' => 'imgInp', 'class' => 'prev-upload']) !!}
</div>
<div class = "form-group">
{!!Form::submit('Submit', ['class'=> 'btn btn-default'])!!}
</div>
{!! Form::close() !!}
I've been struggling with this all morning. I want to be able to accept a file upload along with the form information. Renaming the file is not necessary just how i thought i could get this to work. Is there a better way to do this file upload and move?

I changed the format of everything and it worked.
public function createSlider(AdminRequest $request)
{
$slider = new Sliders(array(
'title' => $request->get('title'),
'description' => $request->get('description'),
'link' => $request->get('link')
));
$slider->save();
$imageName = $slider->title .'_gin_slider'. '.' .
$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(
base_path() . '/public/assets/image', $imageName
);
$slider->image = $imageName;
$slider->save();
return redirect('/admin');
}

For further reference: the issue occurs when your controller, router or middleware method does not return a valid response. You should always return a response from your root called method, whether it's actual data or a redirect. In case of middlewares it can be the request itself via Closure.
In case of controllers it should be:
public function index()
{
return response() || redirect()->back();
}
In case of middlewares:
public function handle($request, Closure $next)
{
return $next($request) || response() || redirect()->back();
}
In case of route closures:
Route::get('foo/bar', function(){
return response() || redirect()->back();
});
|| means 'or'

Related

Laravel Action App\Http\Controllers\Admin\ConcursoController#store not defined

I want to save some form data, and I'm Getting error.
The Error
Action App\Http\Controllers\Admin\ConcursoController#store not defined. (0)
My Form
{!! Form::open(['action'=>'Admin\ConcursoController#store', 'method' => 'POST']) !!}
<div class="form-group">
{{Form::label('company','Entidade')}}
{{Form::text('company','',['class' => 'form-control', 'placeholder' => 'Nome da entidade aquĆ­..'])}}
</div>
{{Form::submit('submeter', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
My Route
$this->group(['middleware' => ['auth:admin'], 'namespace' => 'Admin', 'prefix' => 'admin'], function(){
$this->get('/', 'AdminController#index')->name('admin.home');
$this->resource('concursos', 'ConcursoController');
});
Controller index Method
public function index()
{
$concursos = Concurso::all();
$title = 'Concursos';
return view('admin.concursos.index',compact('title'))->with('concursos',$concursos);
}
Controller Create method
public function create()
{
return view('admin.concursos.create');
}
Controller Store Method
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required',
]);
//Criar concurso
$concurso = new Concurso;
$concurso->title = $request->input('title');
$concurso->body = $request->input('body');
$concurso->save();
return redirect('/admin/concursos')->with('Success', 'Concurso Adicionado');
}
Laravel version 5.7.14
Probably check this file: App\Http\Controllers\Admin\ConcursoController and see if you have a function/method called 'store'. The error is pretty straightforward that script can't find that function.
replace your form
['route' => ['concursos.store']
like
{!! Form::model($transactions, ['route' => ['transaction.store'], 'method' => 'POST','class'=>"form-horizontal"]) !!}

Laravel Contact form on Single Page website

I'm trying to get the correct routes for the Laravel Contact form on a single page website but I'm not sure how to apply the routes so far, since i've done it with sites that aren't single page (parallax - like website/ scrollable).
These are the routes I am creating, so everything stays on the homepage(no redirects at all because it's a one page scrollable website)
Route::get('/', 'ContactUsController#create')->name('contact.create');
Route::post('/', 'ContactUsController#store')->name('contact.store');
My Controller looks like this: Please note that the create controller returns the view to my index which of course is routed like so ('/'),
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\ContactEmail;
class ContactUsController extends Controller
{
public function create()
{
return view('index');
}
public function store(Request $request)
{
$contact = [];
$contact['name'] = $request -> get('name');
$contact['phone'] = $request -> get('phone');
$contact['email'] = $request -> get('email');
$contact['subject'] = $request -> get('subject');
$contact['message'] = $request -> get('message');
//send mail logic here
Mail::to(config('mail.support.address'))->send(new ContactEmail($contact));
flash('Your Message has been sent!) -> success();
return redirect()-> route('/');
}
}
And below is my Contact Form:
{!! Form::open(['route' => 'contact.store', 'class' => 'text-light '])!!}
{!! Form::label('name', 'Your Name', ['class' => 'text-light'])!!}
{!! Form::text('name', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('phone', 'tel', ['class' => 'text-light'])!!}
{!! Form::text('phone', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('email', 'Email', ['class' => 'text-light'])!!}
{!! Form::text('email', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('subject', 'Subject', ['class' => 'text-light'])!!}
{!! Form::text('subject', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('message', 'Your Message Here..', ['class' => 'text-light'] )!!}
{!! Form::textarea('message', null, ['class' => 'form-control text-light'])!!}
{!! Form::submit('Submit', ['class' => 'btn btn-info']) !!}
{!! Form::close() !!}
#if($errors -> any())
<div class="alert alert-danger">
<ul>
#foreach($errors -> all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#include('flash::message')
Solved problem with solution :
//routes for contact form
Route::get('/contacts', 'ContactUsController#create')->name('contact.create');
Route::post('/contacts', 'ContactUsController#store')->name('contact.store');
Everything else same.
Thanks guys for all support.
//Create the routes in web.php
Route::get('contact',['as' => 'contact, 'uses' => 'ContactController#create']);
Route::post('contact',['as' => 'contact', 'uses' => 'ContactController#store']);
// Create the ContactController using the command
php artisan make:controller ContactController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ContactRequest;
use Mail;
class ContactController extends Controller
{
public function create()
{
return view('front.contact.index');
}
public function store(ContactRequest $request)
{
\Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'user_message' => $request->get('message')
), function($message)
{
$message->from('hello#outlined.co');
$message->to('poornima#outlined.co', 'outlined')->subject('New Contact
Request from outlined');
});
return back()->with('status', 'your message has been received');
}}

can't upload audio file to database

I'm trying to upload audio file to database, but nothing happens and i don't get any errors.
controller:
public function store (Request $request)
{
$this->validate(request(), [
'title' => 'required'
]);
$muzika = new Muzika;
if ($request->hasFile('featured_muzika')) {
$daina = $request->file('featured_muzika');
$filename = time(). '.' .$daina->getClientOriginalExtension();
$location = public_path('muzika/' . $filename);
Storage::disk('local')->save($location);
$muzika->daina = $filename;
}
$muzika->daina = $filename;
$muzika->title = $request->title;
$muzika->save();
return redirect('/');
}
this is my form, at first i tried only for title, it was storing in to DB, when i added store method for file it stopped working
{!! Form::open(array('route' => 'muzika.store', 'files' => true)) !!}
{{csrf_field()}}
{!! Form::label('title', 'Title:', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
{{ Form::label('featured_muzika', 'Upload Featured mp3:')}}
{{ Form::file('featured_muzika')}}
{!! Form::submit('Post', ['class' => 'btn btn-primary']) !!}
{!! Form:: close () !!}
when i press submit it only redirects. Database stays empty
In laravel 5.5 you can do
$muzika = new Muzika();
$path = request()->file('featured_muzika')->store('/muzika');
$muzika->daina = $path;
$muzika->save();
make sure your form have enctype="multipart/form-data" set

load data to the form by the ID to update in laravel 5.2

I'm trying to populate the data to edit form. Here's my model
public function EditBatch($id,$request){
$data= DB::table('in_batch')
->where('id', $id)
->update(array(
'id'=>$request->input('id'),
'file_name' => $request->input('file_name'),
'batch_type' => $request->input('batch_type'),
'activity_type' => $request->input('activity_type'),
'schedule_time' => $request->input('schedule_time'),
'predecessor' => $request->input('predecessor'),
'priority' => $request->input('priority'),
'batch_remark'=>$request->input('batch_remark'),
'approved_by' => Auth::user()->id,
'approved_on'=>date("Y-m-d H:i:s"),
));
return $data;
}
here's my controller
public function edit($id){
$obatch = new BatchType();
$batch_type = $obatch->GetBatchTypeDropDown();
$batch = new ManageBatch();
$batch->GetBatchById($id);
return view('batch.edit', array('batch'=>$batch,'batch_type'=>$batch_type));
}
here's my view
{!! Form::open (array('url' => array('batch/update',$batch->id), 'class' => 'form-horizontal', 'method' => 'post','id'=>'editbatch')) !!}
<div class="form-group">
{!! Form::label('batch_id', 'batch_id',array('class'=>'col-md-4 control-label')) !!}
<div class="col-md-6">
{!! Form::text('batch_id',$batch->id,array('class'=>'form-control','id'=>'batch_id')) !!}
</div>
</div>
{!! Form::close() !!}
when i trying to load the data to the view as above error is displaying
Undefined property: App\Models\Batch\ManageBatch::$id (View: C:\wamp\www\hutch-in-portal\resources\views\batch\edit.blade.php)
how to solve this ?
thankyou
well i found a solution and the mistake was in the controller method
public function edit($id)
{
$obatch = new BatchType();
$batch_type = $obatch->GetBatchTypeDropDown();
$ouser = new ManageBatchUser();
$batch_user = $ouser->GetUserDropDown();
$batch = new ManageBatch();
$batch_details=$batch->GetBatchById($id);
return view('batch.edit',array('batch_details'=>$batch_details[0],'batch_type'=>$batch_type,'batch_user'=>$batch_user));
}
since i'm passing a single row to the view . i must add the index [0] in return. finally it worked

Laravel :Trying to get property of non-object on file input

Im trying to do a submit a file with a form submission however I continue to get this error:
Error:
Trying to get property of non-object
at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/Users/plastics1509moore/Desktop/elephant_gin/app/Http/Controllers/AdminController.php', '33', array('request' => object(Request), 'input' => array('_token' => 'y0ExMD4FoH3y1hRX61IOvMW520rn7AEx0UOzrc2R', 'title' => 'lol', 'description' => 'picture of gin one', 'link' => 'www.google.com', 'image' => object(UploadedFile)))) in AdminController.php line 33
I have files set to true. Is the issue the request all?
Here is the Controller function:
public function createSlider(Request $request)
{
$input = Request::all();
if (Input::hasFile('image')) {
$imageName = $input->id . '.' .
$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(
base_path() . '/public/assets/image/', $imageName
);
$input->image = $imageName;
}
Sliders::create($input);
return redirect('/admin');
}
HTML
{!!Form::open(array('url' => 'admin/new_slider', 'files' => true)) !!}
<div class = "form-group">
{!!Form::label('title', 'Title:', ['class' => 'control-label']) !!}
{!!Form::text('title', null, ['class'=> 'input-mini ina tch'])!!}
{!!Form::label('title', 'Description:', ['class' => 'control-label']) !!}
{!!Form::text('description', null, ['class'=> 'input-mini '])!!}
</div>
<div class = "form-group">
{!!Form::label('title', 'Link:', ['class' => 'control-label']) !!}
{!!Form::text('link', null, ['class'=> 'input-mini'])!!}
{!!Form::label('title', 'Image:', ['class' => 'control-label']) !!}
{!! Form::file('image', ['id' => 'imgInp', 'class' => 'prev-upload']) !!}
</div>
<div class = "form-group">
{!!Form::submit('Submit', ['class'=> 'btn btn-default'])!!}
</div>
{!! Form::close() !!}
You are trying to get the id from the input. Your form isn't passing any id so naturally, your input won't have the id.
You can create the slider first and then get the id of the slider like this:
public function createSlider(Request $request)
{
$input = Request::all();
// Create slider
$slider = Sliders::create($input);
if (Input::hasFile('image')) {
// Use the slider id
$imageName = $slider->id . '.' .
$request->file('image')->getClientOriginalExtension();
$request->file('image')->move(
base_path() . '/public/assets/image/', $imageName
);
$input->image = $imageName;
}
return redirect('/admin');
}

Categories