How to pass variable from Controller to Nova Recource? - laravel

I want to pass $defaultFrom from NewsletterController.php:
<?php
namespace App\Http\Controllers;
use App\Mail\NewsletterMail;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Mail;
class NewsletterController extends Controller
{
public function send()
{
$defaultFrom = 'newsletter#stuttard.de';
DB::table('newsletter_mails')->insert(['from' => $defaultFrom]);
$emails = DB::select('select * from newsletters order by id desc');
foreach ($emails as $email) {
Mail::to($email)->send(new NewsletterMail());
}
}
}
to NewsletterMail.php:
<?php
namespace App\Nova;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Text;
class NewsletterMail extends Resource
{
public function fields(Request $request)
{
return [
ID::make(__('ID'), 'id')->sortable(),
Text::make('From', 'from')->default($defaultFrom)->placeholder($defaultFrom),
];
}
}
I've tried to put public $defaultFrom; above the fields() function or call new NewsletterMail($defaultFrom) but this seems to be wrong syntax. Sorry, I'm a bit new to Laravel.

I assume that you have Newsletter model. Move $defaultFrom to model as public const DEFAULT_FROM = 'newsletter#stuttard.de';. After doing this, you can call it's value in both places using Newsletter::DEFAULT_FROM.

Related

Undefined variable:title

I added migrate in database, but why getting error I don't know, I am pretty new to Laravel, I don't know how can I fix this.
Error is:
Undefined variable: title
My code:
namespace App\Http\Controllers\Admin;
use App\Models\Abouts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$aboutus = new Abouts();
$aboutus->$title = $request->input('title');
$aboutus->$subtitle = $request->input('subtitle');
$aboutus->$description = $request->input('description');
$aboutus->save();
return redirect('/abouts')->with('success','nice');
}
}
you can write this way also.
namespace App\Http\Controllers\Admin;
use App\Models\Abouts;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class AboutusController extends Controller
{
public function index(){
return view('admin.aboutus');
}
public function store(Request $request){
$input = $request->all();
Abouts::create($input); //here About us your model
return redirect('/abouts')->with('success','nice');
}
}

Laravel: Class not found if it is called from a Trait

After creating several Apps with Laravel and using softDelete properties I realized that methods like destroy(), restore() and kill() are exactly the same among several controllers. Therefore I am trying to put themn in a trait and use it from diferent Controllers.
My code is as follows:
ProfilesController.php
<?php
namespace App\Http\Controllers;
use App\Profile;
class ProfilesController extends Controller
{
public function destroy(Profile $profile)
{
Profile::del($profile, 'profiles');
return redirect()->route('profiles.index');
}
public function trashed()
{
Profile::trash('Profile');
}
}
Profile.php (model)
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Profile extends Model
{
protected $fillable = ['user_id', 'role_id', 'title', 'subtitle', 'slug', 'birthday', 'about'];
use SoftDeletes, Helpers, commonMethods;
public function getRouteKeyName()
{
return 'slug';
}
// ... more code here
}
trait file: commonMethods.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use App\Profile;
use Session;
trait commonMethods
{
public static function del($element, $page_name)
{
$element->delete();
Session::flash('success', $element . ' successfully deleted!');
}
public static function trash($model)
{
$total = $model::onlyTrashed()->get();
$total_tr = count($total);
$all_tr = $model::all();
return view('partials.templates.trashed', compact('total', 'total_tr', 'all_tr'));
}
// ...more code here
}
The problem:
I try to visit the view "Trashed" that will list all elements "softdeleted" but not "killed", the method.
I pass the $model variable with the method trash($model)
I get the following error:
Class App/Profile does not found. Try to call App/Profile
I have debugged and the $model variable contains exactly what I need, the string 'Profile' which is what I need to build the Query:
$total = Profile::onlyTrashed()->get();
This query works while in the ProfilesController, but does not work while in a trait, since the model class is not found.
Any idea how could I make it work?
I am using Laravel 6.
If you need to use a class as a string you will want to use its full name. 'App\Profile' instead of 'Profile'.
$model = 'Profile';
new $model; // will use `\Profile`
$model = 'App\Profile';
new $model; // will use '\App\Profile';
In your controller( ProfilesController ) write :
use App\Profile;
In your model write :
use App\commonMethods;

Unable to show data in Laravel

I am adding a feature in chatter package and now am unable to show data on my view
this is the code of controller ChatterreplyController.php
<?php
namespace DevDojo\Chatter\Controllers;
use Illuminate\Http\Request;
use DevDojo\Chatter\Models\Chatterreply;
use Auth;
use Carbon\Carbon;
use DevDojo\Chatter\Events\ChatterAfterNewDiscussion;
use DevDojo\Chatter\Events\ChatterBeforeNewDiscussion;
use DevDojo\Chatter\Models\Models;
use Illuminate\Routing\Controller as Controller;
use Event;
use Validator;
class ChatterreplyController extends Controller
{
public function store(Request $request)
{
$chatterreply = new Chatterreply;
$chatterreply->reply = $request->body;
$chatterreply->chatter_post_id = $request->chatter_post_id;
$chatterreply->chatter_discussion_id = $request->chatter_discussion_id;
$chatterreply->save();
return back()->with('chatter_alert','Add Comment Successfully');
}
public function show(Chatterreply $chatterreply ,$id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
return view('chatter::discussion', compact('chatterreplies'));
echo "<pre>"; print_r('$chatterreplies'); die;
}
}
this is the view page discussion.blade.php
#foreach($chatterreplies as $chatterreply)
{{$chatterreply->reply}}
#endforeach
try this:
public function store(Request $request)
{
$chatterreply = new Chatterreply;
$chatterreply->reply = $request->body;
$chatterreply->chatter_post_id = $request->chatter_post_id;
$chatterreply->chatter_discussion_id = $request->chatter_discussion_id;
$chatterreply->save();
return redirect('/discussion')->with('chatter_alert','Add Comment Successfully');
}

how to define multidimentional array globally in laravel controller?

I want to define multidimentional array in laravel controller globally.
I am defining it like this
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Communication_link;
use App\Contact;
use DateTime;
use App\Resource_status;
use App\Inquiry;
use App\Contact_communication;
use App\Pincode;
use App\City;
use App\User;
class createInquiryController extends Controller
{
public $response;
$map = array(
array("contact","id"),
array("communication_link", "id"),
array("contact_communication","id")
);
public function contact_select(Request $request){
return $map;
}
}
but this is throwing a error "undefined map".
Define it and assign data in constructor:
protected $map;
public function __construct()
{
$this->map = array(
array("contact","id"),
array("communication_link", "id"),
array("contact_communication","id")
);
}
Then you'll have access to this variable from any method in this controller:
public function index()
{
$data = $this->map;
}

Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model

I am pretty new to Laravel and I am trying to add the post, create by a user into the database. But when I do so, following error comes:
Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save()
must be an
instance of Illuminate\Database\Eloquent\Model, string given,
called in C:\xampp\htdocs\lar\app\Http\Controllers\PostController.php on line
25 and defined
User model:
<?php
namespace App;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
class User extends Model implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('App\Post');
}
}
Post Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function user()
{
return $this->belongsTo('App\User') ;
}
}
PostController:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
// Validation
$post = new Post();
$post->$request['body'];
$request->user()->posts()->save('$post');
return redirect()->route('dashboard');
}
}
Post Route:
Route::post('/createpost',[
'uses' => 'PostController#postCreatePost',
'as'=>'post.create'
]);
Form action:
<form action="{{route('post.create')}}" method="post">
Please tell me how to fix this.. How to fix this?
Thank you in advance.. :)
I think what you want is this:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class postController extends Controller
{
public function postCreatePost(Request $request){
// Validation
$post = new Post();
// here you set the body of the post like that
$post->body = $request->body;
// here you pass the $post object not as string
$request->user()->posts()->save($post);
return redirect()->route('dashboard');
}
}
You need to pass the $post object as an object to the save method. You was doing this: $user->posts()->save('$post') when you need to do this: $user->posts()->save($post).
Hope it helps.

Resources