I am getting this error: undefined variable. I read a lot of posts about it but, none of them helped with the problem i am facing. (Why I get "Undefined variable" in Laravel view? )
This is Project_Controller :
class Project_Controller extends Controller
{
public function create()
{
$arrondissement = Arrondissements::pluck('arrondissement', 'id');
return view::make('projets.create', compact('arrondissement'));
}
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'intitule' => 'required|max:255',
'code' => 'required|max:255',
'dateDebut' => 'required|max:255',
'dateFin' => 'required|max:255',
'estimation' => 'required|max:255',
'arrondissement' => $request->arrondissement,
]);
if ($validator->fails()) {
return back()
->withInput()
->with(['arrondissement'=>$arrondissement])
->withErrors($validator);
}
$projet = new Projet;
$projet->intitule = $request->intitule;
$projet->code = $request->code;
$projet->dateDebut = $request->dateDebut;
$projet->dateFin = $request->dateFin;
$projet->estimation = $request->estimation;
$projet->arrondissement = $request->arrondissement;
$projet->save();
return view('/submit', compact('arrondissement'));
}
}
submit.blade.php :
<select name="arrondissement_id">
#if (!empty($arrondissement))
Whoops! Something went wrong
#else
#foreach($arrondissement as $id => $arrondissement)
<option value="{{$id}}">{{$arrondissement}}</option>
#endforeach
#endif
</select>
and this is routes.php :
Auth::routes();
Route::get('/home', 'HomeController#index');
Route::get('/', function () {
$projets = \App\Projet::all();
return view('welcome', compact('projets'));
});
Route::get('/submit', function () {
return view('submit');
});
Route::post('submit/projects', 'Project_Controller#store');
I can't see what's causing this error ??
I am using 'arrondissement' as a foreign key of table 'arrondissements'
When returning the view, you should also pass the variable with data:
$arrondissement = ....
return view('/submit', compact('arrondissement'));
I solved the problem. It's simple, I had to remove exclamation mark. Because, I need to test if the value is empty not unempty.
$arrondissement = Arrondissements::pluck('arrondissement', 'id');
You should also add this line into the store function
Related
Hi I'm saving information from blade. The form goes to store function. After saving data I have to send push info using GCM. But this function can not return to view. How can solve this?
public function store(Request $request)
{
$request->validate([
'title_uz' => 'required',
'desc_uz' => 'required',
'url_uz' => 'required',
'company_id' => 'required',
]);
News::create($request->all());
$this->versionUpdate();
$this->sendpush($request);
}
And next function
public function sendpush (Request $request)
{
$fcmUrl = 'https://fcm.googleapis.com/fcm/send';
$notification = [
'title' => $request->title_uz,
'text' => $request->desc_uz,
];
***** here is some functions *******
$result = curl_exec($ch);
curl_close($ch);
$result_to = json_decode($result);
if ($result_to === null) {
return redirect()->route('news.index')
->with('success','DIQQAT!!! Yangilik qo`shildi ammo push-xabar yuborilmadidi.');
}
else {
return redirect()->route('news.index')
->with('success','Yangilik qo`shildi va push-xabar muvoffaqiyatli yuborildi.');
}
}
$result_to returns value but the browser holds at blank screen. It seems the store function holds at the end.
Try this line return $this->sendpush($request);instead of this $this->sendpush($request);
you have redirect from this method so you can try like these
$result_to = $this->sendpush($request);;
if ($result_to === null) {
return redirect()->route('news.index')
->with('success','DIQQAT!!! Yangilik qo`shildi ammo push-xabar yuborilmadidi.');
}
else {
return redirect()->route('news.index')
->with('success','Yangilik qo`shildi va push-xabar muvoffaqiyatli yuborildi.');
}
the user id is existed Before doing create. so it causes an error in the first one.
I made it the other way. the second one below works correctly.
I would like to know why the first one is wrong and it's gone.
//Error
public function store(ContactRequest $request)
{
$request->user_id = $request->user()->id;
Log::debug($request->user()->id);
Log::debug($request);
Contact::create($request->all());
}
//OK
public function store(ContactRequest $request,Contact $contact)
{
$request->user_id = $request->user()->id;
$contact->title = $request->title;
$contact->body = $request->body;
$contact->user_id = $request->user()->id;
$contact->save();
}
the log of the first one is here.
What happened to the user_id!?
[2020-05-30 15:59:10] local.DEBUG: 59
[2020-05-30 15:59:10] local.DEBUG: array (
'_token' => 'gGWuxW6C2JRSCYDuCAC9HauynGclKQEQB7qUh6Rw',
'title' => 'TITLE',
'body' => 'MESSAGE',
'action' => 'SEND',
)
Contact is model class.
ContactRequest is here.
class ContactRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'title' => 'required|max:100',
'body' => 'required|max:1000'
];
}
public function attributes() {
return [
'title' => 'title',
'body' => 'CONTENTS'
];
}
}
You will have to use $request->merge(['user_id'=>$request->user()->id]).
Another tips is that you can simply use Auth::user()->id which also return the user id of current user.
What if you do this:
Auth::user() - >contact($request->all()) - >save() ;
Or also as an experiment:
$contact = new Contact($request->all()) ;
$contact->user_id = Auth::user() - >id;
$contact->save() ;
Actually the second snippet will surely work. The first one I did not test though it looks nice. :)
I have a URL used in blade template as:
href="{{ route('download', ['year' => $year, 'month' => $month, 'file' => $file_path]) }}"
when I am running my code then it is giving me an error as:
Undefined variable: year (View: C:\wamp64\www\Blog\employee-portal\resources\views\finance\invoice\edit.blade.php)
How can i define this $year variable in my controller?
In my controller the function is written as:
public function download($year, $month, $file, $inline = true)
{
$headers = [
'content-type' => 'application/pdf',
];
$file_path = FileHelper::getFilePath($year, $month, $file);
if (!$file_path) {
return false;
}
if ($inline) {
return Response::make(Storage::get($file_path), 200, $headers);
}
return Storage::download($file_path);
}
}
Edit function is written as:
public function edit(Invoice $invoice)
{
$projectStageBillings = $invoice->projectStageBillings;
$projectStageBilling = $projectStageBillings->first();
$client = $projectStageBilling->projectStage->project->client;
$client->load('projects', 'projects.stages', 'projects.stages.billings');
$billings = [];
foreach ($projectStageBillings as $key => $billing) {
$billing->load('projectStage', 'projectStage.project');
$billings[] = $billing;
}
return view('finance.invoice.edit')->with([
'invoice' => $invoice,
'clients' => Client::select('id', 'name')->get(),
'invoice_client' => $client,
'invoice_billings' => $billings,
]);
}
This error states that the view finance\invoice\edit.blade.php is missing the variable $year. And it is true, take a look at the return of your edit function:
return view('finance.invoice.edit')->with([
'invoice' => $invoice,
'clients' => Client::select('id', 'name')->get(),
'invoice_client' => $client,
'invoice_billings' => $billings,
]);
You are not sending any $year variable to the view here (the variables sent to the view are invoice,clients,invoice_client and invoice_billings.
To solve your problem, just send a $year variable to the view and you'll be ok :)
That's what i'm trying to do without any success:
In welcome.blade I have a foreach with some boards and subboards(random generated by user) where you can click on subboard and go something like this /subboardOne. I got this on my routes.php
Route::get('/{subboaName}', 'ThreadController#index');
Route::post('/{subboaName}', 'ThreadController#store');
then you can post a thread on this subboard via form but since i really don't know how laravel knows where he is, the form is something like this:
<form class="form col-md-12 center-block" role="form" method="POST" action="/{{$subboardcoll->id}}">
this $subboardcoll->id comes from the controller, where it sends via the index function the collection:
public function index($subboard)
{
$subboardcoll = Subboard::where('subboaName', $subboard)->first();
$threads = Thread::where('subboaId', $subboardcoll->id)
->orderBy('created_at', 'desc')
->get();
return view('threads.thread', compact('threads', 'subboardcoll'));
}
then i'm trying to send my form and store the thread autoinserting the subboardId but laravel doesn't recognize subboards method:
public function store(Request $request)
{
$this->validate($request, [
'comentario' => 'required|max:2000',
//'g-recaptcha-response' => 'required|recaptcha',
//'imagen' => 'required',
]);
$request->subboards()->threads()->create([
'thrName' => $request->nombre,
'thrComment' => $request->comentario,
'thrImg' => $request->imagen,
'thrSubject' => $request->tema,
]);
return redirect()->back();
}
And gives me this erorr:
BadMethodCallException in Macroable.php line 81: Method subboards does not exist.
Can you guys helpme to know why? also is there better form to do what i'm trying? im newbie on laravel, thanks
EDIT:
Thread.php
public function subboard()
{
return $this->belongsTo(Subboard::class, 'subboaId');
}
Subboard.php
public function thread()
{
return $this->hasMany(Thread::class);
}
The method subboards do not exist in a request object. Consider doing this
public function store($id, Request $request)
{
$this->validate($request, [
'comentario' => 'required|max:2000',
//'g-recaptcha-response' => 'required|recaptcha',
//'imagen' => 'required',
]);
Subboard::find($id)->threads()->create([
'thrName' => $request->nombre,
'thrComment' => $request->comentario,
'thrImg' => $request->imagen,
'thrSubject' => $request->tema,
]);
//Alternative query statement
Subboard::where('id', $id)->first()->threads()->create([.....
return redirect()->back();
}
problems with returning inputs. after form submitted, if validator fails it doesnt return 'old' inputs. here's my controller
$validator = Validator::make($request->all(), [
'user_rate' => 'required|integer|between:1,5',
'user_comment' => 'required',
], [
"user_rate.integer" => trans('errors.rate-required'),
"user_rate.between" => trans('errors.rate-required'),
"user_rate.required" => trans('errors.rate-required'),
"user_comment.required" => trans('errors.com-required'),
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput($request->input());
//return redirect()->back()->with(Input::all());
}
i have tried several ways(also $request->flash(), but it doesnt return 'old' inputs
In your controller
public function someFunction(Request $request)
{
//Validation Logic
if($v->fails())
{
return redirect()->back()->withInput();
}
}
In your view
<input type="text" name="some_name" value="{{old('some_name')}}">
Hope this helps.
Don't pass any arguments to the withInput function
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
//return redirect()->back()->with(Input::all());
}
and how are you getting the old inputs in your form?