I want to pass the request parameter to the method, but I fail every time. I am beginner in PHP.
public function changeModel(Request $request)
{
$u1 = $request->U;
Schema::table('planspieldatas', function (Blueprint $table) {
$table->renameColumn('U1', $u1);
});
Artisan::call('migrate');
return redirect('/pssettings')->with('success-upload', 'Daten für Markt'.$u1);
}
I am not able to pass the $u1 variable to the Schema::table method. Is there any option to change the column names in SQL from the front end.
You are using Closure method so you need to use use() method to pass variables like
Schema::table('planspieldatas', function (Blueprint $table) use ($u1) {
$table->renameColumn('U1', $u1);
});
Related
Hi am trying to use a many to many relationship but I cannot see what im doing wrong. This:
$meeting = new Meeting();
$meeting->attach($user);
causes: BadMethodCallException: Call to undefined method App\Models\Meeting::attach()
User model
public function meetings(): BelongsToMany
{
return $this->belongsToMany(Meeting::class);
}
Meeting Model
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
Migration
Schema::create('meeting_user', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('meeting_id');
$table->unsignedBigInteger('user_id');
$table->timestamps();
});
Attach is called in the relationship, not in the model. You should use it like this:
$meeting->users()->attach($user);
Take a look at https://laravel.com/docs/7.x/eloquent-relationships#updating-many-to-many-relationships.
You have to use like this
$meeting->users()->attach($request->user); // user may have input field name
I'm creating a report table for each user in my database that logs dates/times. Here's my controller:
public function create()
{
$user = auth()->user();
return view('report.create', compact('user'));
}
public function show($id)
{
$data = Time::findOrFail($id);
return view('report.show', compact('data'));
}
So right now if I go to /reports/1, it'll only give me access to the times data with an id of one. However, I want it so if I go to reports/1, i'll have access to all the times data for the user with a user_id of 1.
I tried doing the following, but it didn't work and gave me a 404 on every /# page.
public function show(Time $time)
{
$data= Time::findOrFail($time->user_id)
return view('report.show', compact('data'));
}
Here's my routes for 'reports':
Route::resource('reports', 'ReportController');
And here's my times DB migration that i want to use the data from:
Schema::create('times', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->date('start_day');
$table->text('category');
$table->time('start_time');
$table->time('finish_time');
$table->time('duration');
$table->text('notes');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
hi you only need to modify the show controller like this
public function show($id)
{
$data = Time::where('user_id',$id)->get();
return view('report.show', compact('data'));
}
You need to define the route parameter. It also looks like you have some auto wiring issues.
Route::resource('reports/{id}', 'ReportController');
Change
$data= Time::findOrFail($time->user_id);
to
$data= Time::where('user_id','=',$time->user_id)->get();
I have the following relations set up:
public function notes()
{
return $this->belongsToMany(Note::class);
}
public function tags()
{
return $this->belongsToMany(Tag::class);
}
and pivot table like this:
Schema::create('note_tag', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->integer('note_id')->unsigned()->index();
$table->foreign('note_id')->references('id')->on('tags')->onDelete('cascade')->onUpdate('cascade');
$table->integer('tag_id')->unsigned()->index();
$table->foreign('tag_id')->references('id')->on('notes')->onDelete('cascade')->onUpdate('cascade');
});
now, i used Attach() method:
$note->tags()->attach($tagsIds);
but that doesn't work and I get this error:
[Symfony\Component\Debug\Exception\FatalErrorException]
Cannot instantiate interface phpDocumentor\Reflection\DocBlock\Tag
Looks like you've imported the wrong class that corresponds to Tag::class.
It should probably be something like this:
use App\Tag;
instead of:
use phpDocumentor\Reflection\DocBlock\Tag;
I'm trying to redirect users that go to blog/{id} to blog/{slug} with the slug being the slug of the id they entered.
What I am doing is not working. I dd{$article->slug} and get the correct string, yet when I try to redirect I get "Trying to get property of non-object" error.
I'm using route/model binding. This is my RouteServiceProvider:
$router->bind('blog', function($slug)
{
if ($slug) {
if(is_numeric($slug)) {
$article = Article::findOrFail($slug);
return redirect('blog/' . $article->slug);
}
return Article::with('images')->where('slug', $slug)->first();
}
});
This is my show method in controller:
public function show(Article $article)
{
return view('blog.show', compact('article'));
}
Any ideas would be greatly appreciated!
articles migration file:
class CreateArticlesTable extends Migration
{
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->string('slug');
$table->text('body');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
public function down()
{
Schema::drop('articles');
}
}
Your controller is expecting an instance of Article. So, if you're returning a RedirectResponse from your closure, it won't work.
At the moment, I can only think of 2 ways of achieving what you want with route-model binding.
The first method is simpler. Just don't type-hint the Article. Instead, check if it's a RedirectResponse. If so, return it. If not, return the view. Something like this:
public function show($article)
{
if ($article instanceof \Illuminate\Http\RedirectResponse)
{
return $article;
}
return view('blog.show', compact('article'));
}
You could also check if it's an instance of an Article just to be absolutely certain since you're no longer type-hinting.
The second method is overly complex in my opinion for a simple check. You would have to throw a custom exception, catch it from within app/Exceptions/Handler.php, and then redirect from there.
I have a coloumn in my database named using camelCase convention how should i put it in the mutator naming to work ?
migration
public function up()
{
Schema::create('skillTypes', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->boolean('needRating');
$table->timestamps();
});
}
I named it like that but didnt work and also named it setNeedRatingAttribute and didnt work either
the mutator
public function setneedRatingAttribute($input){
if($input==null){
$input=false;
}
else{
$input=true;
}
$this->attributes['needRating']=$input;
}
Add this property to your Model
public static $snakeAttributes = false;
and rename method from setneedRatingAttribute to setNeedRatingAttribute
First of all, the mutator you provided works. You could rename it to setNeedRatingAttribute for readability.
Secondly, you can replace the if in your setter with:
$this->attributes['needRating'] = boolvar($input);